How can an onChange event (of a dropdown) call php?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Betty_S
Forum Newbie
Posts: 20
Joined: Tue Dec 05, 2006 3:40 am

How can an onChange event (of a dropdown) call php?

Post 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?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
timclaason
Forum Commoner
Posts: 77
Joined: Tue Dec 16, 2003 9:06 am
Location: WI

OnChange

Post 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.
Betty_S
Forum Newbie
Posts: 20
Joined: Tue Dec 05, 2006 3:40 am

How to get or set html tags (input types) by php code?

Post 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?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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).
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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...
Betty_S
Forum Newbie
Posts: 20
Joined: Tue Dec 05, 2006 3:40 am

Thanks, it was all very helpful!

Post by Betty_S »

Thanks, it was all very helpful!
Post Reply