0

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; 
?>
1
  • 4
    PHP executes on the server, Javascript executes on the client. You can NOT mix the languages like that. Commented Nov 22, 2013 at 15:24

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.