This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 2 years ago.
I am trying to call a javascript function from php. The function takes one argument.
This is the call:
<?php
function welcomeBackMessage($user, $password){
echo "<h1>BACK: " . $user . "pwd " . $password . "</h1>";
echo "<script> welcomeBack({$user})</script>";
}
?>
This is the JS function; declared before the php functions calls it:
<script>
//Called form php to welcome back a guest
function welcomeBack(user){
print("JS WELCOME BACK CALLED");
var login = document.getElementById(loginbutton);
login.parentNode.removeChild(login);
}
The JS function never executes, and debuting the console I get the following:
<h1>BACK: guest2pwd 1234</h1><script> welcomeBack(guest2)</script>
Can't find variable: guest1.
The idea is to pass the variable to a JS function so I get update a DOM element with variable.
Any input appreciated.
You need quotes around the string.
echo "<script> welcomeBack('{$user}')</script>";
Related
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 2 years ago.
It is necessary in PHP code where there is a js code to output, for example, through an alert variable. Does not work. Please help, tell me how to do it.
And if anyone can how to use this as a URL to the picture
function setLocation($url_image){
$locationName = R::load('locationsdb', $url_image);
echo '<script type="text/javascript">',
'alert("'<?php echo $locationName; ?>'");',
'var img = "<?php echo $locationName ?>";',
'document.getElementById("image_location").src = img;',
'</script>';
}
alert("{"id":"3","location_name":"\u0410\u043b\u0445\u0438\u043c\u0438\u043a\r\n","location_description":"\u0423 \u044d\u0442\u043e\u0433\u043e \u0441\u0442\u0430\u0440\u0446\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0435\u0441\u0442\u0438 \u043d\u0430\u0441\u0442\u043e\u0439\u043a\u0438, \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u044b\u0435 \u043f\u0440\u0438\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0438\u043b \u0433\u0435\u0440\u043e\u044f\u043c, \u0438\u0441\u0446\u0435\u043b\u044f\u0442\u044c \u0438\u0445 \u043f\u043e\u0441\u043b\u0435 \u043e\u0436\u0435\u0441\u0442\u043e\u0447\u0435\u043d\u043d\u044b\u0445 \u0431\u043e\u0435\u0432 \u0438 \u043c\u043d\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u0438\u043b\u044c\u043d\u043e \u043f\u043e\u043c\u043e\u0447\u044c \u0432 \u0442\u044f\u0436\u043a\u043e\u0439 \u0436\u0438\u0437\u043d\u0438 \u0438\u0441\u043a\u0430\u0442\u0435\u043b\u044f \u043f\u0440\u0438\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439.\r\n","location_travel":"2","location_lut":"-","location_mobs":"-","location_image":"../images/locationsBackgrounds/backgroundAlchemistImage.png","location_runs":"-","location_characters":"-"}");
You can't use <?php tags inside each other. It makes no sense anyway - you're already in the PHP block, you don't need to open it again. And for similar reasons you can't use echo inside another echo command. And again it makes no sense anyway, you're already echoing, so why repeat the same instruction before you've finished the first one?
To generate the text you want, just use string concatenation with the . (dot) operator:
echo '<script type="text/javascript">
alert("'.$locationName.'");
var img = "'.$locationName.'";
document.getElementById("image_location").src = img;
</script>';
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
PHP echo in echo
(1 answer)
Closed 2 years ago.
I'm trying to get PHP variables into javascript however I'm having some issues. In testing, I'm trying to verify that it works. I've used variations of the following code and either got an alert with the actual PHP call or got an error.
Tried this: (an alert is generated but of the actual PHP call as a string basically, not the actual naming variable or moo)
function jvtest()
{
$naming = "moo";
echo '<script type="text/javascript">
var x = "<?php echo $naming ?>";
alert(x);
</script>';
}
And this. Generates a PHP error.
function jvtest()
{
$naming = "moo";
echo '<script type="text/javascript">
var x = "'<?php echo $naming ?>'"; <-- error on this line
alert(x);
</script>';
}
Any idea of how to resolve this? Thanks
This question already has answers here:
Calling Javascript function in PHP while passing PHP variables into the function
(2 answers)
Closed 4 years ago.
I have tried all the post of stack overflow and from google but none of those seem to work for me.
This is my php code:
echo $firstPar;
echo $secondPar;
$tab_str.="<td width=\"20%\">‹";
This is the Javascript code:
function functionCall(firstPar, secondPar) {
alert(firstPar);
alert(secondPar);
}
If I am passing string values directly on the php call rather than passing the variables, I am getting the values inside the function. But the variable values are coming as null. Whereas the echo displays the variable values.
There should be quotes around parameters. Try using this:
$tab_str .= "<td width=\"20%\">‹";
Try like this
echo $firstPar;
echo $secondPar;
$tab_str .= '<td width="20%">‹';
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I am trying to join three languages with no luck.
I need to call javascript function to add clicked text to textarea, which is inside php with html.
This is what I tried as my best with no luck:
$hint .= "<br>" . "<a href='#' onclick='addtext(". $name .")'> " . $name . " </a> ";
I think I need somehow to echo that $name inside addtext() function, but don't know how, because I can't write statement inside another. I think so, because when I debug code, I see that this is recognized not as string but as attribute property:
JS function:
function addtext(str) {
var newtext = str;
document.getElementById("msg").value += newtext;
}
Please suggest a working solution to this problem.
Put your string inside function with quotation marks ;)
This question already has answers here:
Call php function from JavaScript
(5 answers)
Closed 8 years ago.
I am trying to call a JavaScript function with parameter as php script using the following code
submit
any suggestions will be appreciated
Use something like this
submit
OR
var job ='';
job = <?php echo $job; ?>;
submit
If $job is anything but a number, your code will fail.
To drop a PHP variable into a JavaScript context, you must use json_encode. Since you're in an HTML attribute, you must also use htmlspecialchars.
So:
<a href="javascript:submitform(<?php echo htmlspecialchars(json_encode($job));?>);">
You can write in php directly
<?php
echo 'submit';
?>
You can also use any of this two
submit
submit