Jump to content

Can this be done in PHP


coolphpdude

Recommended Posts

hi,

 

just wondering if this can be done in php...

 

say i wanted to create a form with 2 drop down boxes where the data in the second box was determined by the users choice for the first box. For example on the autotrader website you choose your make of car, then you can access the second drop down box and choose a model of that make. So if you choose Ford for the first box the second box would contain all cars ford make (focus, fiesta, etc...) but if you selected vauxhall the second drop down box would contain vauxhalls model (astra, vectra, etc...).

 

Im doing something similar to this for subjects in for a school system so i'd appreciate your help.

Link to comment
Share on other sites

see thing is i was going to use a table of year groups and what subjects would be available for the year (in a heirachy if you get what i mean) so going back to the order trader example you could have massive amounts of data so i was hoping to use php and mysql. if someone would recommend which way would be the best to produce what i need i would appreciate it. Or even just give me some suggestions of what to search for on google to research that kind of drop down box.

Link to comment
Share on other sites

I haven't gotten in to ajax much, but that should be exactly what you want to use to get this job done. Can work with php/mysql to change forms and update your table/fields on the spot without loading. Which is pretty nice when pulling information from a database, where you are browsing through and grabbing information quickly. This page might help you get started: http://www.tizag.com/ajaxTutorial/ajaxform.php

Link to comment
Share on other sites

because I was bored I mocked up a quick AJAX example for dynamic select lists. This does not include a lot of validation, but will work. I leave it to you to optimize:

 

The user page:

<html>
<head>

<script type="text/javascript">

var xmlHttp

function updateModels(makeVal)
{
  //Disable the dependant list until it is updated
  document.getElementById('model').disabled = true;

  xmlHttp=GetXmlHttpObject();
  if (xmlHttp==null)
  {
    alert ("Your browser does not support AJAX!");
    return;
  }

  var url="get_models.php";
  url=url+"?make="+makeVal;
  url=url+"&sid="+Math.random();
  xmlHttp.onreadystatechange=populateModels;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}


function populateModels()
{
  if (xmlHttp.readyState==4)
  {
    //Create obj ref to the select list
    var selectObj = document.getElementById('model');
    //Clear values from select list
    selectObj.options.length = 0
    //Get the response from the server-side page
    var modelListStr=xmlHttp.responseText;

    //Split the results into an array
    var modelListAry = modelListStr.split(',');

    //Repopulate the list
    for ($i=0; $i<modelListAry.length; $i++)
    {
      var modelVals = modelListAry[$i].split('-');
      selectObj.options[selectObj.length] = new Option(modelVals[1], modelVals[0]);
    }

    //re-enable the select list
    selectObj.disabled = false;
  }
}

</script>

</head>

<body onload="">

<form name="test" method="POST" action="processingpage.php">

Make: 
<select name="make" id="make" onchange="updateModels(this.value);">
  <option value="Dodge">Dodge</option>
  <option value="Ford">Ford</option>
  <option value="Honda">Honda</option>
  <option value="Toyota">Toyota</option>
</select>
<br>

Model: 
<select name="model" id="model">
  <option value="">Not Applicable</option>
</select>
<br>

</form>
</body>
</html>

 

The server-side page

<?php

  $returnAry = array();

  //Get the make from the query string
  $make = $_GET['make'];

  //Find the models for the selected make
  $query = "SELECT model FROM cars WHERE make='$make'";
  $result = mysql_query($query);

  //Create the return data
  while($model = mysql_fetch_assoc($result))
  {
    $returnAry[] = $model['id'] . '-' . $model['name'];
  }

  echo implode(',', $returnAry);

  //The return string will be inthis format
  // "id1-value1,id2-value2,id3-value3, etc"

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.