Click to See Complete Forum and Search --> : Newbie: how to call PHP script from javascript?


registering
07-14-2003, 10:54 PM
Hello all,

I'm new to both javascript and PHP and am having trouble seeing the "big
picture". I thought I had it down but I added some javascript and am now
lost conceptually.

I'm building menus dynamically via javascript, and would like to pass on
the current values of these menus to a PHP script which creates an image
for me (a graph of the values).

Normally I would do something like this:


<form action="graph1.php?$query_string" name=form7 method=post>
<input type="submit" value="Send form">
</form>


which works fine when the page is first loaded ($query_string is given
default values). However the intent is for the user to change the menus'
values, hit "submit" again and the image will be recreated.

How do I do that so the image is recreated in the correct place when the
user hits the "submit" button or changes a menu selection? That is, I can
do something like this:


<form action="stacked.php" name=form7 method=post onSubmit="return false;">
<select name="Table" onchange="CallGraph()">
<option selected=true value="spacekeeper">Table</option>
</select>
</form>


so that when the user changes a selection, "CallGraph()" is called which
is a javascript function that builds the $query_string to pass to graph1.php.
However I'm lost as to what to do next. How do I simply call graph1.php from
within CallGraph() so that the image will appear in the proper place??

If there was a way I could "reload" the page without $query_string being
initialized to its default values, but rather take the menus values, that's
probably best, however I don't know how to do that. Since the menus only get
changed via javascript, the PHP code has already finished executing, which is
why I thought I could tie a button to call graph1.php explicitly, but I'm not
sure how best to do that.

You can view the code at www.mindspring.com/~jc416975/stacked.html for reference
or www.mindspring.com/~jc416975/stacked.php (although it won't execute). Other
applications this script depends on aren't running currently so you get errors
if you hit the actual button).

Thanks for any pointers, it's greatly appreciated.

pyro
07-15-2003, 08:15 AM
You'll have to reload the page with the query string set. PHP and javascript can't interact with each other, so to get javascript variables into your PHP script, you have to reload the page with the query string set. You can then use PHP to read these values.

registering
07-15-2003, 02:09 PM
Thanks for the help pyro. That would explain why I was having so much trouble. :)

So I simplified my code and have just 1 big form in jtest10_home.php declared like this:


echo "\n<form action=\"jtest10_home.php?<?php EchoScript.php ?>\" name=\"mainForm\" method=\"get\">";


and my graph image declared like this:


echo "\n <img src=\"jtest10_graph.php?<?php EchoScript.php ?>\" alt=\"Graph_" . $counter . "\" name=\"Graph_" . $counter . "\" border=\"0\">";


My ENTIRE EchoScript.php is this:

<?php
error_log("\n\nhello", 3, "/nerr/html/php.debugfile");
?>


When I change my menus and hit "submit" the URL changes with the real values, like this:
http://192.168.0.120/jtest10_home.php?host=ACE&site=FS&table= etc....

However EchoScript.php never gets called and I'm lost as to why. :( There are no spaces or blanks before the "<?" in EchoScript.php.....php.debugfile exists and has write permissions for everyone..

The intent is to simply have EchoScript.php $_GET all of the variables in the URL and echo them back, so when I call jtest10_*, the correct string will be passed to them....