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

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?

the onChange event is happening client side in javascript. your PHP script lives on the server. You can code up javascript to make a call to the server and fetch information, and this technique is usually referred to by the acronym AJAX.

If you can give more information about what you are asking for, this can probably be moved to javascript forum if more appropriate.

I used to have similar “probleme” with combining JS and PHP, and succeded to “build” a select menu for users to choice among different languages (without cookies), something like:

<select onChange="window.location='<? print ($this_page); ?>.php?language='+this.value">

where $this_page refers to the name of the loaded file “$this_page.php”
I’m not sure if Betty_s’s probleme is similar to that, it’s just an example…

So, the html calls the JavaScript and the JavaScript calls the php?
Sound interesting, can u give an example code?


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?

ASP.NET has an abstraction, which makes you think that that’s what happening, even though it’s not. Such abstractions are commonly called frameworks. Try PRADO, which is a PHP framework, that uses the same type of abstraction as ASP.NET.

Here’s a short example of a drop-down list (select) that is combining html, JS and PHP:

<table border="0" cellpadding="0" cellspacing="0" width=100%>
<td width = "10%" valign="center" align="center"><p>Product</p></td>
<td width = "10%" valign="top" align="center">
$item = $_GET["product"];
if ($item == "")
	$item = 1;
<select onChange="window.location='ban.php?product='+this.value" >
$product = array(
for ($i = 1; $i <= 4; $i++)
<option value="<?=$i?>" <? if ($item == $i) { print "SELECTED";}?>> <?=$product[$i]?></option>
<td width= "50" valign="middle"><img src = "<?=$item?>.gif" width="32" height="20">

You can create some images to display and name them 1.gif, 2.gif, etc… or whatever you want

Thanks, it was all very helpful!

I wrote a simple js function:
echo “<script type=\“text/javascript\”>”;
echo “function alertBtn()”;
echo “</script>”;

It refuses to fire, did I miss some important point or should I just recheck my code?

Additionally, is there a special php command for conetcting the php file to a js file or should I just add “<script src=…></script>”?

How are you calling alertBtn() ?

You will be connecting the html which PHP outputs to the JS file. Get it working without PHP first then convert bits to PHP slowly.

You are using only double quotes, I’d advise on settling on a policy of single / double quotes as soon as you can - it can make your code more readable, and help stop some errors too.

(outputting a string from PHP, to a string in JS inside a confirm box etc, can get real tricky…)

I too Face Same kinda prob…
When i type a value in textbox it should check
whether there is a table with same name exists…
if exists it will return false else new table with that name is created
can anyone help me