-2

I have a php file. I am trying to run Javascript code inside it. My code is:

<form id = "form" action= "form.php" method = "post">

Name: <input type = "text" id = "FirstName" name="name"/>
<input type="submit" id="click" value ="Enter name"/>

</form>

<?php
$name = $_POST['name'];
if ($name == "sam"){

echo "
<script type = 'text/javascript'>
var a = document.getElementbyId('name');
alert(a);
</script>
";
}
else {

  echo "Wrong name";
}

?>

However javascript does not work. Any idea why my script is not running?

  • Because PHP runs on server-side, while JavaScript runs on the client side. Also, there is nothing to trigger the script you have provided – Russ J Jun 17 '20 at 01:08
  • Hi I know. But there seems to be countless examples where Javascript is run in PHP. – Programming noob Jun 17 '20 at 01:10
  • Can you link to some of these examples? I honestly don't think you need any PHP for what you are trying to do here. – Russ J Jun 17 '20 at 01:11
  • Maybe you're confusing when php prints out javascript. But not the other way around. – choz Jun 17 '20 at 01:18
  • @RussJ For example: https://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php – Programming noob Jun 17 '20 at 01:20
  • @RussJ For simplicity purpose I made a simple program to see if it is possible to run javascript in php. – Programming noob Jun 17 '20 at 01:21
  • Technically, you can run JavaScript in PHP(for instance: https://www.php.net/manual/en/book.v8js.php) but this is obviously not the question. The requester wants PHP to execute JavaScript _in the browser_. – akenion Jun 17 '20 at 01:29
  • @Programmingnoob All this is doing is writing JavaScript to PHP's output(and hence the resultant page). What is the output HTML when executing this? I don't see any reason the alert shouldn't be firing. I do think the ID should be 'FirstName' rather than 'name', but calling alert with a null parameter should still trigger a message. – akenion Jun 17 '20 at 01:32

1 Answers1

1

Javascript does not run inside PHP, however it will be echoed by php to your browser and will be interpreted as an usual javascript code. You have to pass the values from php to javascript as static text.


<?php
$name = $_POST['name'];
if ($name == "sam"){

echo '
<script>
var a = "'.$name.'";
alert(a);
</script>';
}
else {

  echo "Wrong name";
}

?>

Éder Rocha
  • 1,538
  • 11
  • 29
  • I forgot a pair of " at var declaration. Try it now. – Éder Rocha Jun 17 '20 at 01:28
  • This is vulnerable to cross-site scripting. Wrap `$name` with `htmlentities` to escape it. – akenion Jun 17 '20 at 01:33
  • @akenion I think he just started... but yes, should search and study more about these questions in future. – Éder Rocha Jun 17 '20 at 01:36
  • I agree he should do more research, but I would caution handing faulty code to someone who is inexperienced without pointing out the risks. – akenion Jun 17 '20 at 01:40
  • Also, I was mistaken on saying `htmlentities` since this is JavaScript. `json_encode` would be more appropriate. `'var a='.json_encode($name).';'`. JSON is valid JavaScript so this is a trick I use regularly. – akenion Jun 17 '20 at 01:42