Page 1 of 1

newbie ** jquery help getting form value to a php var

Posted: Sat Aug 13, 2011 10:37 am
by glennnall
i'm a php guy, NOT jquery or AJAX -

i'm in real need of some help getting a select option value into a php variable within the same page. I know that i have to send the js to the server and back to the php file, i just don't know how to do this...

i'd be very grateful if someone could show me how. Once i see it work, i'll understand it. I've been searching for a while and am apparently unable to google the correct query terms... :o)

very grateful,
gn

Re: newbie ** jquery help getting form value to a php var

Posted: Tue Aug 16, 2011 10:30 pm
by danwguy
To the best of my knowledge you can't do that, once the php page is run all the variables are set. The only thing that might, and I stress MIGHT be possible is using and onchange event on the select box, sending that variable through an xmlhttpresponse using ajax to another php script that can then return something like

Code: Select all

$variable = $_GET['variable'];
if($variable != '') {
echo "<?php $newvar = ".$variable."; ?>";
}
and using ajax in your original page fill in the innerHTML of a div with the respose, which would be your variable assignment, then you can try to access $newvar, however I highly doubt it will work since php will look for the definition of that variable when the page first loads, not when it's presented.

Re: newbie ** jquery help getting form value to a php var

Posted: Tue Aug 16, 2011 11:23 pm
by califdon
glennnall wrote:i'm a php guy, NOT jquery or AJAX -

i'm in real need of some help getting a select option value into a php variable within the same page. I know that i have to send the js to the server and back to the php file, i just don't know how to do this...

i'd be very grateful if someone could show me how. Once i see it work, i'll understand it. I've been searching for a while and am apparently unable to google the correct query terms... :o)

very grateful,
gn
You can't "send the js to the server", nor would you want to. All you can send is one or more values. You can use AJAX to request the server to run another php script to look up some information and send the values back to the js function to change the select option value. To learn how to do that, just read any AJAX tutorial.