Forum Moderators: coopster

Message Too Old, No Replies

Passing value in php variable to JScript

Hardcoding works, but when passed in php variable, it does not,. Why?

         

aakk9999

12:27 am on May 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The $js_property_price variable contains either "123456+" or "123456 "

I have the following call to JScript in my php:

echo "<script type=\"text/javascript\">var eurprice_0" . $cnt . " = $js_property_price;</script>";

The above does not work. However, if I hard-code the price

echo "<script type=\"text/javascript\">var eurprice_0" . $cnt . " = '123456+';</script>";

then the script works and returns the property value in selected currency with or without + sign at the end depending on + sign being passed in the string in the first place.

I have echoed $js_property_price before passing it to JS and I can see that it contains the correct value (i.e. the same value which, when hard-coded, works.)

Any ideas? Or are there any rules I should be aware of?

Thanks

bubbasheeko

2:43 am on May 3, 2008 (gmt 0)

10+ Year Member



hey aakk9999,

Try this:

echo "<script type=\"text/javascript\">var eurprice_0" . $cnt . " = " . $js_property_price . ";</script>";

mehh

1:27 pm on May 3, 2008 (gmt 0)

10+ Year Member



If that fails try this

echo "<script type=\"text/javascript\">var eurprice_0" . $cnt . " = '".$js_property_price."';</script>";

aakk9999

5:55 pm on May 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thank you both on the reply. In a meantime I have solved the problem by creating another variable and changing the table to hold a number in it where 0 means space and 1 means +, then changing js script to translate the second variable into appropriate sign before returning the variable back.

It is a bit frustrating as I neither know PHP nor JS and where some things should work, they don't so I am finding long way around to achieve what I want to do.

For example, in JS the If/else statement did not seem to work and 'else' part got executed every time even the code first went through 'true' part of the statement. I solved this by replacing IF with SWITCH statement. Now, although the JS function works and does what I want it to do, I am wondering WHY the IF statement did not work.

So, for example, in my original code, after the IF statement below was executed, the plusSign variable was always set to " +" (hence gone through 'else' path) even if the nSign is 0:

If (nSign == 0)
{
plusSign = " ";
}
else
{
plusSign = " +";
}

However, when I replaced this with switch statement shown below, then the function did what it supposed to do. So, although problem solved, I am wondering why the IF statement did not work and executed both, true and false (if and else) paths (I verified that both paths are executed by putting alert statements all over the place to see where the code goes and in what order) In other languages that I know the two paths in IF statement are mutually exclusive.

Here is what did work:

switch(nSign)
{
case 0:
plusSign = " ";
break;
case 1:
plusSign = " +";
break;
default:
alert ("Case else: Invalid value in db");
}

Thanks on any clarifications anyone wishes to offer.