Connecting Tech Pros Worldwide Forums | Help | Site Map

Possible help with php/ajax call?

Bob M
Guest
 
Posts: n/a
#1: Nov 3 '08
Fellow php'ers;

I am trying to make a call to a js function from within a php function based
on the selected value of an HTML select statement (whew!).

While the server side php program is getting called the variable being
passed is always empty. The following is my select statement from within
my php program which itself falls within a php function:

print "
<select name=\"_stcd\" id=\"_stcd\"
onchange=\"ajaxCallServer('http://".
$_SERVER['SERVER_NAME']."/cnl0d2.php?_selName=_gameid&_stcd=".$_stcd."')
Quote:
>";
The valut of $_stcd SHOULD contain the value of the select item the user
selects. But its always blank.

The select option lines are generated from an array of values immediately
following this line.

As I've been searching, researching, re-reading my php books, javascript
books, etc for two days now, I thought it best to ask someone else.

Any ideas as to why this value is always blank?

Thanks all (my mind is fried!)

Bob

=?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=
Guest
 
Posts: n/a
#2: Nov 3 '08

re: Possible help with php/ajax call?


Bob M escribió:
Quote:
I am trying to make a call to a js function from within a php function based
on the selected value of an HTML select statement (whew!).
You cannot. This his how all this works: PHP runs on the web server, it
outputs a regular HTML document with possibly some bits of JavaScript
and it delivers this document to your web browser, which then parses the
HTML and runs the JavaScript code. Whatever PHP tutorial you read, this
is explained in the first chapter :-P
Quote:
While the server side php program is getting called the variable being
passed is always empty. The following is my select statement from within
my php program which itself falls within a php function:
>
print "
<select name=\"_stcd\" id=\"_stcd\"
onchange=\"ajaxCallServer('http://".
$_SERVER['SERVER_NAME']."/cnl0d2.php?_selName=_gameid&_stcd=".$_stcd."')
Quote:
>";
It's simple. This code runs in your computer, not in the server, thus
you cannot use PHP. You must use JavaScript to fetch the current value
of the <selectfield.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Bob M
Guest
 
Posts: n/a
#3: Nov 3 '08

re: Possible help with php/ajax call?


"Ãlvaro G. Vicario" wrote:
Quote:
Bob M escribió:
Quote:
>I am trying to make a call to a js function from within a php function
>based on the selected value of an HTML select statement (whew!).
>
You cannot. This his how all this works: PHP runs on the web server, it
outputs a regular HTML document with possibly some bits of JavaScript
and it delivers this document to your web browser, which then parses the
HTML and runs the JavaScript code. Whatever PHP tutorial you read, this
is explained in the first chapter :-P
>
Quote:
>While the server side php program is getting called the variable being
>passed is always empty. The following is my select statement from within
>my php program which itself falls within a php function:
>>
>print "
><select name=\"_stcd\" id=\"_stcd\"
>onchange=\"ajaxCallServer('http://".
>$_SERVER['SERVER_NAME']."/cnl0d2.php?_selName=_gameid&_stcd=".$_stcd."')
Quote:
>>";
>
It's simple. This code runs in your computer, not in the server, thus
you cannot use PHP. You must use JavaScript to fetch the current value
of the <selectfield.
>
>
Thanks - Got It! Duh, me!
Bob M
Guest
 
Posts: n/a
#4: Nov 3 '08

re: Possible help with php/ajax call?


Thanks - Got It! Duh, me!

So, does this NOW seem like it "should" be correct? I am getting a php
syntax error on the unexpected character of "["

print "
onchange=\"ajaxCallServer('http://"
$_SERVER['SERVER_NAME']."/cnl0d2.php?_selName=_gameid&_stcd="
+document.getElementById('_stcd').options[document.getElementById('_stcd').selectedIndex].value'')
Quote:
>";


=?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=
Guest
 
Posts: n/a
#5: Nov 3 '08

re: Possible help with php/ajax call?


Bob M escribió:
Quote:
So, does this NOW seem like it "should" be correct? I am getting a php
syntax error on the unexpected character of "["
>
print "
onchange=\"ajaxCallServer('http://"
There's a concatenation operator missing (aka dot) and some other typos.

It's very difficult to debug code written this way. Sometimes it's
easier to just end PHP mode and write HTML as is:

// ... some PHP
?>
<select name="foo" onclick="alert('Bar')">
<?php
// More PHP

Quote:
$_SERVER['SERVER_NAME']."/cnl0d2.php?_selName=_gameid&_stcd="
+document.getElementById('_stcd').options[document.getElementById('_stcd').selectedIndex].value'')
Quote:
>";
A couple of JavaScript tips:

* Don't write all your code in one line inside the onchange attribute:
use functions

* Your code is executed when printed, thus _stcd will not change. Try
something like:

onchange="fetchData(this)"

function fetchData(field){
var url = "/cnl0d2.php?_selName=_gameid&_stcd=" +
field.options[field.selectedIndex].value;
...
}



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Hugh Oxford
Guest
 
Posts: n/a
#6: Nov 3 '08

re: Possible help with php/ajax call?


Bob M wrote:
Quote:
Fellow php'ers;
>
I am trying to make a call to a js function from within a php function based
on the selected value of an HTML select statement (whew!).
>
Can I suggest Xajax (http://xajaxproject.org/)? You'll never have to
think about these things again.
Jerry Stuckle
Guest
 
Posts: n/a
#7: Nov 4 '08

re: Possible help with php/ajax call?


Hugh Oxford wrote:
Quote:
Bob M wrote:
Quote:
>Fellow php'ers;
>>
>I am trying to make a call to a js function from within a php function
>based
>on the selected value of an HTML select statement (whew!).
>>
Can I suggest Xajax (http://xajaxproject.org/)? You'll never have to
think about these things again.
>
It won't help in this case. His problem is not the Ajax call, but how
he was expecting the PHP code to run.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Closed Thread