Possible help with php/ajax call? – PHP

  php

Q(Question):

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."’)

>";

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

A(Answer):

Bob M escribiĆ³:

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 šŸ˜›

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."’)

>";

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

A(Answer):

"ƃĀlvaro G. Vicario" wrote:

Bob M escribiƃĀ³:

>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 šŸ˜›

>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."’)

>>";

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!

A(Answer):

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”)

>";

A(Answer):

Bob M escribiĆ³:

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

$_SERVER[‘SERVER_NAME’]."/cnl0d2.php?_selName=_gameid&_stcd="
+document.getElementById(‘_stcd’).options[document.getElementById(‘_stcd’).selectedIndex].value”)

>";

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

A(Answer):

Bob M wrote:

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.

A(Answer):

Hugh Oxford wrote:

Bob M wrote:

>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.
js*******@attglobal.net
==================

LEAVE A COMMENT