473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function cal($x)
  3. {
  4. echo "<br>";
  5. echo "x value =".$x;
  6. $calculation= "<script type=\"text/javascript\">"."document.write(eval($x));"."</script>"; 
  7. echo "<br>";
  8. echo "calculated value".$calculation;
  9. echo "<br>";
  10. return $calculation;
  11. }
  12.  
  13. $x="10";
  14. $a= cal("$x+5");
  15. echo "<br>";
  16. echo "a value now".$a;
  17. echo "<br>";
  18.  
  19. $b="$a+5";
  20. echo "<br>";
  21. echo "now b value=".$b;
  22.  
  23. echo "<br>";
  24. $c= cal("$a+5");
  25. ?>
  26.  
  27.  

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.
Sep 2 '10 #1
5 1121
JKing
1,206 Expert 1GB
You are directly setting your values to $b without passing them through your function.

Expand|Select|Wrap|Line Numbers
  1. $b="$a+5";
  2.  
  3. //Should be
  4. $b = cal("$a+5");
  5.  
Sep 2 '10 #2
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
Sep 2 '10 #3
JKing
1,206 Expert 1GB
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.
Sep 2 '10 #4
JKing
1,206 Expert 1GB
You can try php's eval()
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     function cal($x)
  3.     {
  4.         echo "<br>";
  5.         echo "x value =".$x;
  6.         eval("\$calculation=$x;");
  7.         echo "<br>";
  8.         echo "calculated value".$calculation;
  9.         echo "<br>";
  10.         return $calculation;
  11.     }
  12.  
  13.     $x="10";
  14.     $a= cal("$x+5");
  15.     echo "<br>";
  16.     echo "a value now".$a;
  17.     echo "<br>";
  18.  
  19.     $b=cal("$a+5");
  20.     echo "<br>";
  21.     echo "now b value=".$b;
  22.  
  23.     echo "<br>";
  24.     $c= cal("$b+5");
  25.     echo "<br>";
  26.     echo "now c value=".$c;
  27. ?>
Sep 2 '10 #5
kovik
1,044 Expert 1GB
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
Sep 2 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ben Kial | last post by:
I need to write a program to "copy" files and preserved the files' atime, mtime, ctime, file permissions. I also need to have this program working in Unix and Windows, where the "Archive"...
12
by: Gaurav | last post by:
Hello I have a program that basically inverts the contents of files except first line. It compiles fine but gives me core dump on running. If i comment temp.clear() it runs fine, but i need...
7
by: ruthless | last post by:
hello I've got a question how can I write a program(computing program) working in OS background(Win or Linux)? E.g. I'm starting my program for computing and it works in system backgraound,...
2
by: Milk | last post by:
Hi, i got a program working half part and my program is suppose to do 4*4 matrix can any one help me to check it? i dunno whic part i did wrong thx~~ Here is the program ::::: .data arrayA:...
2
by: iwdu15 | last post by:
hi, i got this erro when i tried to use the code from http://www.windowsformsdatagridhelp.com/default.aspx?ID=59135549-e5dd-4501-9526-343ac05a7617 to make sure i only had one instance of my program...
4
by: qbproger | last post by:
I'm developing a plugin for some software. The previous version of the software didn't require a start in directory to be set. This allowed me to leave the working directory to the default in the...
7
by: misha | last post by:
Hello. I was wandering if someone could explain to me (or point to some manual) the process of mapping the addresses of host variables by DB2. Especially I would like to know when DB2 decides to...
9
by: jvdb | last post by:
Hi all, I've created a program that receives files and opens the corresponding program (for example adobe acrobat). However, when started, i would like to see nothing of the running program. I...
10
by: lvl 1 monk | last post by:
Hi all, I don't really have the money for the formal route so I went to the library and picked up an older c++ book and it seems I run into "antiquated headers" over and over again. The problem...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.