Forum Moderators: open

Message Too Old, No Replies

Ajax Dropdown Lists

         

LinusIT

10:01 pm on Jan 16, 2011 (gmt 0)

10+ Year Member



I'm not sure if this should be posted here or under PHP, so apologise if in the wrong place.

I've got a drop down list of types, when a user selects a type another drop down appears with sizes. The data is grabbed from a MySQL database. It's working exactly as it should be except I can't do anything with it at present. I'd like a submit/go button to process what the user has selected but have no idea on doing this.

HTML:


<html>
<head>
<script type="text/javascript">
function showSizes(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getsizes.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="Type" onchange="showSizes(this.value)">
<option value="">Choose a Type:</option>
<option value="1">Muay Thai</option>
<option value="2">Srivichai</option>
<option value="3">MMA</option>
<option value="4">K1</option>
<option value="5">Kickboxing Trousers</option>
</select>
</form>
<br />
<div id="txtHint"><b>Sizes will be listed here.</b></div>

</body>
</html>


PHP

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'danny', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("db_test", $con);

$sql="SELECT id,size FROM sizes WHERE typeid = '".$q."'";

$result = mysql_query($sql);
?>
<select name="size">
<option>Select Size</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value="<?=$row['id']?>"><?=$row['size']?></option>
<? } ?>
</select>
<?php
mysql_close($con);
?>


Any help would be fantastic :)

daveVk

4:57 am on Jan 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add submit button prior to </form>

<input type="submit" value="Submit" />

move </form> to after ajax div

add action to form

<form name="myform" action="submit.php">

I would also change

<b>Sizes will be listed here.</b>

to

<select name="size">
<option>Sizes will be listed here.</option>
</select>

to pre allocate a size form field.

LinusIT

1:42 pm on Jan 18, 2011 (gmt 0)

10+ Year Member



When I read your reply daveVk I thought, how stupid I was! haha.

I've made the changes in that file, Here's what I have now:


<form name="order" action="order.php">
<select name="type" class="select165" onchange="showSizes(this.value)">
<option value="">Choose a Type:</option>
<option value="1">Muay Thai</option>
<option value="2">Srivichai</option>
<option value="3">MMA</option>
<option value="4">K1</option>
<option value="5">Kickboxing Trousers</option>
</select>
<div id="txtHint"><select name="size" class="select165">
<option>Choose Type First.</option>
</select></div>
<p><input type="submit" value="Go.." class="button" /></p>
</form>


So when I select a value in both selects and press Go I get order.php?type=x&size=y. X & Y depending on what they have selected, all good, thanks.