This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I'm trying to pass multiple values from PHP into my javascript function. The function is simple right now, just trying to show a popup with the values:
<script type="text/javascript">
function showMapsInfo(name, ctr) {
alert('Info = '+name +' '+ctr);
}//function showMapsInfo
</script>
When I just pass in one value, name or ctr, it works fine. However when passing in two or more values no alert occurs. Is there a reason this isn't working? I'm guessing if this is impossible I'll have to use AJAX to get the job done?
Here is the relevant PHP code. I am making multiple forms on the page, every id is unique via a ctr. I read in the $maps_name from a database. This I can output to the screen fine, so no issue there.
echo('<button type="button" id="button'.$ctr.'" onClick="showMapsInfo('.$maps_name.', '.$ctr.');"><img src="img/maps_logo.gif"></button><br/>');
I'm guessing you just need to quote $maps_name and $ctr beforehand (since they're most likely strings:
echo('<button type="button" id="button'.$ctr.'" onClick="showMapsInfo(\''.str_replace("'", "\\'", $maps_name).'\', \''.str_replace("'", "\\'", $ctr).'\');"><img src="img/maps_logo.gif"></button><br/>');
Related
This question already has answers here:
what does a question mark mean before a php form action
(4 answers)
Closed 2 years ago.
Can someone explain to me what is the use of ?action=add&code= and what they do in the code below? I have tried to search it on Google but they gave me HTML action atribute instead.
<form method="post" action="index.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
Sorry for the noob question.Thanks for the reply.
Those are called query string values or parameters, they are one of several potential parts of a URL. Each key/value pair provides information that the server-side code can use when constructing the response to send back to the client. (Or the server-side code could even simply ignore them, they have no harmful effect.)
For example, given this key/value pair on the query string:
action=add
In the server-side code you can get the value "add" by fetching it from the query string by its key:
$action = $_GET["action"];
// $action now contains the string "add"
Presumably the logic in the code would then do something based on that value.
action is the name of a "normal" GET variable $_GET['action'].
You must look in the further code to see where it appears and what it is used for.
There is no standard for that
In the url after ? we can pass the values onto another webpage which can be used further.
This question already has answers here:
What's the best way to pass a PHP variable to Javascript? [duplicate]
(4 answers)
Closed 7 years ago.
I want to set background color based on user preference that's in a database.
I set a variable named $bc from the session variable I saved from the row.
If I echo the either the session variable ($_SESSION['backcolr']) or the new variable ($bc), the correct value of yellow appears. However, if I try to use either variable with jQuery, it doesn't work. Not sure what I'm doing wrong.
<script type="text/javascript">
// $('#body').css(background-color','yellow') -- this works
$("body").css("background-color",'$bc'); --- this doesn't work
</script>
Hello you should modify your code like below;
<script type="text/javascript">
// $('#body').css(background-color','yellow') -- this works
$("body").css("background-color",'<?PHP echo $bc; ?>');
</script>
Thats because you need to tell PHP when it should be parsing PHP code.
Replace that line with:
$("body").css("background-color",'<?php echo $bc; ?>');
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I'm trying to call a javascript function with one argument being a variable gotten from a drop box. This script works fine if only passed the value from the current drop box using "this.value", however when trying to pass the variable the code doesn't work. The variable is properly being populated from the value in the drop box when I use echo statements. I think the problem is with actually passing the variable to the javascript function. The function showSection(q, r) is never being called as the write statement is never executing. Any help would be appreciated. Here is my php and javascript code
echo "<select name=\"course\" onchange=\"showSection($q, this.value)\">";
If the $q or this.value are string values, you have to pass it within quotes.
echo "<select name='course' onchange='showSection(\"$q\", \"this.value\")'>";
You need to make sure inserting the value of $q doesn't produce javascript syntax errors. The reasonable way to do that is to use json_encode on the value.
After that you need to make sure both single and double quotes are escaped in that value, to keep the html correct. htmlspecialchars is used for that. In my opinion, converting both single and double quotes always (ENT_QUOTES) is the best choice.
And the end result is (I'm using heredoc syntax here, because I find it more readable):
$escaped = htmlspecialchars(json_encode($q), ENT_QUOTES);
echo <<<HTML
<select name="course" onchange="showSection($escaped, this.value);">
HTML;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Pass a PHP string to a Javascript variable (including escaping newlines)
Access a JavaScript variable from PHP
I have a Javascript function that takes in one variable. The function has some php code inside it. I want to use this variable inside the php section of the function. I couldn't get it to work. How is it done?
The issue is that your PHP code is being rendered on the server before being served to the client. I would recommend either converting the PHP code into Javascript code or creating an AJAX call to the PHP function.
Start reading about AJAX! You will likely need to rewrite some of the code you have written but what you are attempting to accomplish is not really possible otherwise.
try with it
<script>
var whatever = "<?php echo $phpVar ?>";
</script>
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
<button class="addToPlaylist" onclick="javascript:myPopup(<?php echo $videos[$counter]?>);
return false;">+</button>
I have a button on an image as a html hyperlink. I want to perform different actions on hyperlink and button. The above code works whenever I do not pass the PHP variable using echo. When i pass PHP variable, the button also performs the same action as of the hyperlink, that means return false does not work.
Any idea why the return false; does not work when i pass PHP variable?
This should be:
<button class="addToPlaylist" onclick="javascript:myPopup('<?php echo $videos[$counter];?>');return false;">+</button>
Note the single quotes in myPopup. As you pass a string to myPopup, you will need to enclose it with single quotes. (Double won't work as there is already double quotes for the onclick)
I am quite sure $videos[$counter] is not numeric, but a string. In this case you have to write the quotes:
onclick="javascript:myPopup('<?php echo $videos[$counter]?>');
And make sure, $videos[$counter] doesn't contain any, something like
onclick="javascript:myPopup('<?php echo addslashes($videos[$counter])?>');
comes to mind.
onclick="javascript:myPopup("";return false;" . This should work and i think it's more clear where you have javascript code and php code.