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,

Community
  • 1
  • 1
dimazaid
  • 1,673
  • 3
  • 22
  • 24
  • can you show what you tried, and what part didn't work? – Anurag Mar 02 '11 at 22:22
  • 1
    Setting a string should just be `var str = "";`. 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? – Felix Kling Mar 02 '11 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) – dimazaid Mar 02 '11 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? – Felix Kling Mar 02 '11 at 22:30
  • @FelixKling: `var str = ;` , outputting strings without escaping them is a bad idea. – DCoder Apr 09 '12 at 09:49

2 Answers2

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

?>
jacoz
  • 3,508
  • 5
  • 26
  • 42
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.

TonyTheJet
  • 517
  • 1
  • 6
  • 15