user3022256
user3022256

Reputation: 1

passing value to javascript to php variable: It do not work?

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; 
?>

Upvotes: 0

Views: 86

Answers (2)

diegomvsc2
diegomvsc2

Reputation: 11

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.

Upvotes: 1

Headshota
Headshota

Reputation: 21449

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.

Upvotes: 1

Related Questions