1

I was looking for a way to pass a string(variable saved in a form of $x) from php to Java Script and I found so many codes to solve that, but my question is : does those strings have to be declared global?!
i did declare it as a global variable but still no response ..!
any other suggestions?!

Pass a PHP string to a JavaScript variable (and escape newlines)

I tried most of these codes, none of them worked,

5
  • can you show what you tried, and what part didn't work? Commented Mar 2, 2011 at 22:22
  • 1
    Setting a string should just be var str = "<?php echo $x; ?>";. But you have to show your code and which value $x holds to help you better. What exactly means none of them worked? What did not work? Commented Mar 2, 2011 at 22:23
  • I mean they worked with either normal integers or strings being sent directly like function("hello") but i couldn't pass a predefined variable like $x (x contains a string) Commented Mar 2, 2011 at 22:28
  • 4
    @user635614: Please show your code. Otherwise we cannot say more than Then you are doing something wrong. Does this help you? Commented Mar 2, 2011 at 22:30
  • @FelixKling: var str = <?php echo json_encode($x); ?>; , outputting strings without escaping them is a bad idea. Commented Apr 9, 2012 at 9:49

2 Answers 2

0

As the others have said, all we can say is that you are doing something wrong. You can place PHP variable values, strings or otherwise, wherever you want in your JavaScript code, since PHP is server-side and can do whatever you like on the client side.

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

Comments

0

This will help you to solved the issue of passing string variable value by calling the javascript function within php scrpt. :)

<?php

$testStrFileName = "test.jpg";

$file_name = "<script>". $testStrFileName ."</script>";

//<sample tags/>

echo '<a href="javascript:deleteGalleryImage('.$file_name.');">Delete</a>';

?>

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.