This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 2 years ago.
php file
<button id='submit' name='submit' ><h1>Schedule</h1></button>
<?php
include 'dbres.php';
if (isset($_POST['submit'])){
$sql = "UPDATE reserved SET reserved = false WHERE reserved = true;";
$res = mysqli_query($conn,$sql);
echo'document.getElementById("submit").onclick = function() {
this.textContent = "reserved";
}';
}
?>
I want the javascript code inside the php code to changes the text from submit to reserved. I have tested the javascript code and it works by itself, but doesn't seem to work in php.
You cannot echo JS inside PHP, they are two separate languages. If you want to have the client run the JS, you can use the script tag:
echo '<script>
document.getElementById("submit").onclick = function() {
this.textContent = "reserved";
}
</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:
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>";
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
Suppose you are in a php file and you want to run a javascript function that has both javascript and php inside of it. How would you go about doing this?
Ex:
<?php
<script>
function myFunction(){
// php code
document.write("Hello World!");
}
</script>
?>
I know I can easily make a php function for this particular code, but just for future reference, I would like to know how to do this.
I apologize if this has been answered before but it seemed difficult to word in google search.
The PHP code has to output text that is valid inside the javascript. Something like this.
<?php
<script>
function myFunction(){
// php code
var message = "<?php echo "My alert message"; ?>";
window.alert(message);
}
</script>
?>
This question already has answers here:
How to access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?> [duplicate]
(6 answers)
Closed 9 years ago.
In my HTML file, I'm using a string variable called {$var} which is passed from a PHP file. How could I use {$var} in a javascript function within the same html file? I would like to display this variable using the js function. This is what I have so far:
<span id="printHere"></span>
<script type="text/javascript">
var php_var = {$production};
$('#printHere').html(php_var);
</script>
For PHP
You can echo out the variable directly into your JavaScript. Just be sure to json_encode() it so that data types and escaping are all done automatically.
var php_var = <?php echo json_encode($production) ?>;
For Smarty
If you are using Smarty for your templating engine, you want this instead:
var php_var = {$production|json_encode nofilter};
What this does is disable the HTML escaping of Smarty (with nofilter) and passes the value through json_encode().
Make sure $production is set. If it is a (javascript) String then use:
var php_var = '<?php echo addslashes($production); ?>';
If it is a Number then use
var php_var = <?php echo $production; ?>;