Page 1 of 1
How can an onChange event (of a dropdown) call php?
Posted: Tue Dec 05, 2006 5:18 am
by Betty_S
How can an onChange event (of a dropdown) call php?
I want to do an action according to the user selection on my dropdown list (the <select> tag). I don't know how can I get the selected item id or name? And how can I set the onChange event of the dropdown?
Posted: Tue Dec 05, 2006 6:26 am
by neophyte
Yuppers.. you'll need ajax/xmlhttp object whatere.
http://jpspan.sourceforge.net/examples
There's examples there on how to do just about everything.
OnChange
Posted: Tue Dec 05, 2006 8:51 am
by timclaason
If the onChange directs to a different page with a querystring, you can have the PHP behave differently.
For instance, say you have a couple radio buttons:
Code: Select all
print("<INPUT TYPE='RADIO' NAME='site' onChange=");?>"window.location.href='updateranks.php?action=inactivate&subact=0';"<? print(" CHECKED>Active");
print("<INPUT TYPE='RADIO' NAME='site' onChange=");?>"window.location.href='updateranks.php?action=inactivate&subact=1';"<? print(">Inactive");
The onChange is setup to redirect to the page updateranks.php, with querystrings for each option. So, you could do:
Code: Select all
$GSubact = $_GET['subact'];
if(!$GSubact)
$myClass->doSomething(1);
elseif($GSubact == 1)
$myClass->doSomething(2);
Hope it makes sense.
How to get or set html tags (input types) by php code?
Posted: Tue Dec 05, 2006 9:03 am
by Betty_S
I'm adding a function a php file. It supposed to display html and php code.
I discovered that there is no way to get or set html tags by php code within the same page or function.
The only way I seen is by $_REQUEST but it's not suitable for me.
I'm trying to rebuild a table by the selection on the dropdown. But how can get the selected item id or name? And how can I set the onChange event of the dropdown?
In ASP.NET the input type run at the server side so there can be interaction between asp and html tags. Is there anything like in php?
Posted: Wed Dec 06, 2006 2:29 am
by CoderGoblin
As already mentioned you can use something called AJAX.
The forum tutorial
XMLHttp tutorial (who's online example) gives a quick intro into the method although you may want to look at
Painless JavaScript Using Prototype and the section on "Get your Web 2.0 On". The javascript library it uses is
here.
In your case the Javacript would simply call the AJAX command with the necessary parameters which in turn would load a PHP page in the background. The resulting information would be passed back and the Javascript would replace the current content with the new. It should be noted that there are some disadvantages of using this method. For example... Informing the user that something is happening if the php takes a long time and the disruption of the browser back button (the new php information is not stored in the browser history).
Posted: Wed Dec 06, 2006 3:20 am
by CoderGoblin
I've included a sample Ajax program in the coding Critique area
here. While this deals with dual selects you should be able to adapt it fairly easily...
Thanks, it was all very helpful!
Posted: Fri Dec 08, 2006 7:05 am
by Betty_S
Thanks, it was all very helpful!