0

how can i make comparison of string which will getting from JavaScript to PHP

<body>
<input type="text" name="potype" id="potype" value="Regular">

<?php 

// Declaration of strings 
$name1 = "<script>document.write(document.getElementById('potype').value); 
 </script>";
$name2 = "Regular"; 
echo var_dump($name1);
echo var_dump($name2);

// Use == operator 
if ($name1 == $name2) { 
    echo 'Both strings are equal'; 
} 
else { 
    echo 'Both strings are not equal'; 
} 
?> 

</body>

Output: string(73) "Regular" string(7) "Regular" Both strings are not equal

Manju H
  • 7
  • 1
  • 1
    You are not getting your input value in the PHP variable at all. PHP executes, and the result is litterally `string(73) "" string(7) "Regular" Both strings are not equal`. Then this result is sent to your browser, which parses the script tag and adds "Regular" at the place of the script tag. What are you trying to achieve exactly? Remember that JS is not parsed by PHP. The result of PHP is sent to the client's browser, which will parse JS. There is no communication between the 2 code executions. – Kaddath Nov 03 '20 at 13:28
  • I want both values are equal how can I get that or how can I get the input value without using JavaScript – Manju H Nov 03 '20 at 13:38
  • To be precise, are you trying to check if the value entered by a user on a browser's input is equal to a fixed value? If yes, are there reasons you don't want to do this check in JS? – Kaddath Nov 03 '20 at 13:48
  • Yes I want to check input value to fixed value – Manju H Nov 03 '20 at 14:13
  • The easiest solution will still be to do this check in JS in client's browser. But if you want to avoid it for a **precise reason** (eg security), you need to remember that PHP and JS are separate code executions: PHP runs in the server's machine, and the result is usually some text (HTML) sent to the Client's browser, which independantly will run the JS that is in it. [See this link](https://stackoverflow.com/questions/23740548/how-do-i-pass-variables-and-data-from-php-to-javascript) to find how you can pass data from one to another (Ajax particularly) – Kaddath Nov 03 '20 at 14:28

0 Answers0