I'm trying to initialize inputs with maps api autocomplete -the number of inputs comes from database- , but I'm not able to execute a simple javascript function in a while loop.
The initialazing is working fine with window.onload, but I can't execute the function in this while loop...
No errors appearing in Console, 1 result is coming from database.
$limit = $bdd->prepare('MySQL query');
$limit->execute();
while ($city = $limit->fetch()) {
echo 'result'; ?>
<script type="text/javascript">
function initializeLimitCity() {
alert("Hello World");
};
initializeLimitCity();
</script>
<?php
}
You cant declare same function more times... Change your code to this:
<script type="text/javascript">
function initializeLimitCity() {
alert("Hello World");
};
</script>
<?
$limit = $bdd->prepare('MySQL query');
$limit->execute();
while ($city = $limit->fetch()) {
echo 'result'; ?>
<script type="text/javascript">
initializeLimitCity();
</script>
<?php
}
?>
<script type="text/javascript">
function initializeLimitCity() {
alert("Hello World");
};
</script>
<?php
$limit = $bdd->prepare('MySQL query');
$limit->execute();
while ($city = $limit->fetch()) {
echo 'result'; ?>
<script type="text/javascript">
initializeLimitCity();
</script>
<?php
}
You are generating the function with the same name several times, it won't work.
I'd not generate it this way. Let the client (Javascript) do the work, resulting in less bytes to transfer (HTML):
<?php
$cities = $limit->fetch_all();
?>
<script type="text/javascript">
function initializeLimitCity(cities) {
alert(cities);
};
initializeLimitCity(<?php echo json_encode($cities); ?>);
</script>
Related
I am trying to call
this js function that is stored on a file called default.js from a php file.
function myfunction(score) {
console.log("got here");
alert(score);
}
so this is the call for the js function on my php code but it does not work.
<!-- myfunction js file ↓ -->
<script type="text/javascript" src="../js/default.js"></script>
<?php
if ($grade=="100%"){
// not able to call
echo "myfunction($grade)";
}
else{
echo "alert(\"haha\")";
}
?>
Try this you will get the accurate result
<?php
if ($grade == "100%") {
?>
<script>
myfunction('<?=$grade?>');
</script>
<?php
} else {
?>
<script>
alert('in');
</script>
<?php } ?>
you forgot about the <script> tags
change your echo like this:
echo "<script>myfunction('$grade')</script>";
you need to wrap ' around the $grade because you're passing a string
or put <script> before the opening php tag and </script> after the end of php tag
This is your Code.
Define $grade too in PHP or you get a
<b>Warning</b>: Undefined variable $grade in <b>
<script type="text/javascript" src="../js/default.js"></script>
<script>
<?php
$grade = "10%";
if ($grade=="100%"){
echo "myfunction('$grade')";
}
else{
echo 'alert("haha");';
}
?>
</script>
Dont forget the <script></script> for javascript code inside html.
I need to echo multiple line error in JS alert and I am using below code
Working code
$str = "This is a error1\\n";
$alert = $str."This is error2";
if(!empty($alert)){
?>
<script type="text/javascript">
alert('<?php echo $alert;?>');
</script>
<?php
}
but I need to work on an old written code so this has addslashes function before echo alert like below:
$str = "This is a error1\\n";
$alert = $str."This is error2";
$alert = addslashes($alert);
if(!empty($alert)){
?>
<script type="text/javascript">
alert('<?php echo $alert;?>');
</script>
<?php
}
Output : This is a error1\nThis is error2
I need this to print in two lines I have also tried with only \n but it is not working then.
I'd make javascript do the hard work.
<script type="text/javascript">
var alertArr = ["<?php echo $string1; ?>", "<?php echo $string2; ?>"];
alert(alertArr.join("\n"));
</script>
https://jsfiddle.net/0uwmmm3n/
(Magnificent random url by the way)
EDIT: less messy version, maybe more legitimate
<?php $alertArr = [$string1, $string2]; ?>
<script type="text/javascript">
var alertArr = <?php echo json_encode($alertArr); ?>;
alert(alertArr.join("\n"));
</script>
RE-EDIT: nah, too much work for a job so simple, two conversions is way too much
$str = "This is a error1\\n";
$alert = $str."This is error2";
$alert = addslashes($alert);
if(!empty($alert)){
?>
<script type="text/javascript">
alert('<?php echo $alert;?>');
</script>
<?php
}
I have used this code and it working fine according to your need
if still issue continues then clear your cache
I'm trying to execute some PHP code that is using the WooCommerce variable to get the order ID.
add_action('add_meta_boxes', 'gen_order_meta_boxes');
function gen_order_meta_boxes()
{
add_meta_box(
'woocommerce-order-gen',
__( 'TESTNG' ),
'order_meta_box_gen',
'shop_order',
'side',
'default'
);
}
function order_meta_box_gen()
{
echo '<button class="button save_order button-primary alert-mass">Generate</button>';
}
add_action('admin_footer', 'lolcats');
function lolcats()
{
$order = new WC_Order($post_id);
?>
<script type="text/javascript" >
jQuery(document).ready(function($)
{
$(".alert-mass").click(function(e)
{
e.preventDefault();
alert('<?php echo $order->get_order_number; ?>');
});
});
</script>
<?php
}
I can't seem to get it to return anything besides either null or 0.
Using something like this obviously works perfectly: window.location="dhlgen.php?order=1234"; because I'm trying to pass it through the URL to a PHP function which uses it later.
This also seems to fail, but it returns 'false':
function lolcats($post_id)
{
$order = new WC_Order($post_id);
?>
<script type="text/javascript" >
jQuery(document).ready(function($)
{
$(".alert-mass").click(function(e)
{
e.preventDefault();
var data = <?php echo json_encode($order->shipping_first_name); ?>;
alert(data);
});
});
</script>
<?php
}
Thank you.
Okay, I solved it.
function lolcats()
{
global $post;
$order_id = $post->ID;
?>
<script type="text/javascript" >
jQuery(document).ready(function($)
{
$(".alert-mass").click(function(e)
{
e.preventDefault();
alert('<?php echo $order_id; ?>');
});
});
</script>
<?php
}
I would like to use a value from PHP in JavaScript, but it's not working. I fetch data from a database and store it in a PHP variable and for conditional formatting. I need to also use the data in JavaScript for validation.
The value required by me is $date = date('Y-m-d'); If I pass the value by json Encode the alert function is not working...
$(document).ready(function()
{
alert("Hi");
var array = php print(json_encode($date));
});
Whereas if I just alert the function without...
var array = php print(json_encode($date)); ;
...it works fine.
<script type="text/javascript" src="assests/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
alert("Hi");
var array = '<?php print(json_encode($date)); ?>';
});
</script>
</head>
<body>
<form action="majorprocess.php">
<?php
$dbconn = mysql_connect('localhost', 'root','' );
if(!$dbconn)
{
die(mysql_error());
}
else
{
$date = date('Y-m-d');
<script type="text/javascript" src="assests/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
alert("<?php echo json_encode(date('Y-m-d')); ?>");
});
</script>
What is the resulting HTML?
There could be a PHP error outputting instead of valid javascript.
Try putting the PHP section at the top maybe?
When asking a question it is always best to post any outputs you are getting so people can help.
try this one
</head>
<body>
<form action="majorprocess.php">
<?php
$dbconn = mysql_connect('localhost', 'root','' );
if(!$dbconn)
{
die(mysql_error());
}
else
{
$date = date('Y-m-d');
}
?>
<script type="text/javascript" src="assests/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var array = '<?php print(json_encode($date)); ?>';
alert(array);
});
</script>
You are using the PHP $date variable before setting its value. Make sure to move the php block before you try to output the date:
<?php
$dbconn = mysql_connect('localhost', 'root','' );
if(!$dbconn)
{
die(mysql_error());
}
else
{
$date = date('Y-m-d');
}
?>
<script type="text/javascript" src="assests/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
alert("<?php echo json_encode($date) ?>");
});
</script>
Good day to everyone:). I just want to ask how can I do php foreach and echo values inside the javascript, because I want the javascript to be a dynamic.
Here is the static js
<script type="text/javascript">
$(document).ready(function(){
$('input[type="checkbox"]').click(function(){
if($(this).attr("value")=="1"){
$(".1").toggle();
}
if($(this).attr("value")=="2"){
$(".2").toggle();
}
if($(this).attr("value")=="3"){
$(".3").toggle();
}
if($(this).attr("value")=="4"){
$(".4").toggle();
}
});
});
</script>
and this what I want to happen
<script type="text/javascript">
$(document).ready(function(){
$('input[type="checkbox"]').click(function(){
<?php foreach($data as $field): ?>
if($(this).attr("value")=="<?php echo $field->ID);?>" ){
$(".<?php echo $field->ID);?>").toggle();
}
<?php endforeach; ?>
});
});
</script>
the ID values form the database are 1,2,3,4. That would be all. Thank You!
you can push all the values to a js array and than do a loop on js array.
<script type="text/javascript">
$(document).ready(function(){
my_values = Array;
<?php foreach($data as $field): ?>
my_values.push ("<?php echo $field; ?>");
<?php endforeach; ?>
$('input[type="checkbox"]').click(function(){
// java script loop here
for ( var i in my_values)
{
if($(this).attr("value")== i )
{
$("."+this.value).toggle();
}
}
});
});
</script>