In this program similar_text function do not work but echo successfully print $var_1 and $var_2. What is the exact error?
<script>
var j=prompt('1st name','Name')
var l=prompt('2nd name','Name')
</script>
<?php
$var_1 = '<script>document.write(j)</script>';
$var_2 = '<script>document.write(l)</script>';
similar_text($var_1, $var_2, $percent);
echo $var_1, $var_2;
echo $percent;
?>
Harry
90.1k26 gold badges216 silver badges224 bronze badges
-
4PHP executes on the server, Javascript executes on the client. You can NOT mix the languages like that.Marc B– Marc B2013年11月22日 15:24:54 +00:00Commented Nov 22, 2013 at 15:24
2 Answers 2
PHP is executed first, on the server, then it gets served and then only javascript is executed on the client-side. so the variables you are using in php part are not set at that moment.
If you need to interact with a php script from javascript, you odd to use an ajax request.
answered Nov 22, 2013 at 15:26
Headshota
21.4k13 gold badges63 silver badges82 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
you need close php and open again
<script>
var j=prompt('1st name','Name')
var l=prompt('2nd name','Name')
</script>
<?php
$var_1 = '<script>document.write(?>j<?php)</script>';
$var_2 = '<script>document.write(?>l<?php)</script>';
similar_text($var_1, $var_2, $percent);
echo $var_1, $var_2;
echo $percent;
?>
or same.
Comments
default