Program not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhanrajesh210002
    New Member
    • Aug 2010
    • 6

    Program not working

    hi

    I'm trying to calculate string's integer value with the help java script,
    i write java script in a function and i call it when ever i need to do that calculation, but it seems not working
    Code:
    <?php
    function cal($x)
    {
    echo "<br>";
    echo "x value =".$x;
    $calculation= "<script type=\"text/javascript\">"."document.write(eval($x));"."</script>"; 
    echo "<br>";
    echo "calculated value".$calculation;
    echo "<br>";
    return $calculation;
    }
    
    $x="10";
    $a= cal("$x+5");
    echo "<br>";
    echo "a value now".$a;
    echo "<br>";
    
    $b="$a+5";
    echo "<br>";
    echo "now b value=".$b;
    
    echo "<br>";
    $c= cal("$a+5");
    ?>

    first time ($a= cal(..)) is working but, the same function in second time ($b=cal(..)) by using previous values is not working.
    KINDLY POINT OUT THE PROBLEM and help to solve it.
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    You are directly setting your values to $b without passing them through your function.

    Code:
    $b="$a+5";
    
    //Should be
    $b = cal("$a+5");

    Comment

    • madhanrajesh210002
      New Member
      • Aug 2010
      • 6

      #3
      no its not working,

      even
      $c= cal("$a+5"); or $c= cal($b); same,

      but the out put on second time the function cal, calling, its seems out puts wrong , kindly help to solve

      Comment

      • JKing
        Recognized Expert Top Contributor
        • Jun 2007
        • 1206

        #4
        This doesn't work because the way the everything is being processed. The script doesn't know the value of $b until the value of $a is determined. Since value $a is determined client side there is no way for the server to pass the calculated value of $a.

        Comment

        • JKing
          Recognized Expert Top Contributor
          • Jun 2007
          • 1206

          #5
          You can try php's eval()
          Code:
          <?php
          	function cal($x)
          	{
          		echo "<br>";
          		echo "x value =".$x;
          		eval("\$calculation=$x;");
          		echo "<br>";
          		echo "calculated value".$calculation;
          		echo "<br>";
          		return $calculation;
          	}
          
          	$x="10";
          	$a= cal("$x+5");
          	echo "<br>";
          	echo "a value now".$a;
          	echo "<br>";
          
          	$b=cal("$a+5");
          	echo "<br>";
          	echo "now b value=".$b;
          
          	echo "<br>";
          	$c= cal("$b+5");
          	echo "<br>";
          	echo "now c value=".$c;
          ?>

          Comment

          • kovik
            Recognized Expert Top Contributor
            • Jun 2007
            • 1044

            #6
            PHP has it's own eval() function. It cannot access the values that are generated by JavaScript.

            EDIT: Didn't realize JKing posted... I've had this post up for a while and forgot about it. lol

            Comment

            Working...