Jump to content

Drop down boxes


scarlson

Recommended Posts

Ok, I have 2 drop down boxes that pre-load the states and then corresponding cities in another drop down box for users to select.  My question is, how do I save what they select to mySQL database I have?  Also, once they select something, how do I go about pre-loading this data in the drop down boxes once they return to the page again to edit?

 

Here is the code I have :

 

        <td><script type="text/javascript">
function createRequestObject() { 

   var req; 

   if(window.XMLHttpRequest){ 
      // Firefox, Safari, Opera... 
      req = new XMLHttpRequest(); 
   } else if(window.ActiveXObject) { 
      // Internet Explorer 5+ 
      req = new ActiveXObject("Microsoft.XMLHTTP"); 
   } else { 
      // There is an error creating the object, 
      // just as an old browser is being used. 
      alert('Problem creating the XMLHttpRequest object'); 
   } 

   return req; 

} 

// Make the XMLHttpRequest object 
var http = createRequestObject(); 

function sendRequest(id) { 

   // Open PHP script for requests 
   http.open('get', 'getcities.php?id='+id); 
   http.onreadystatechange = handleResponse; 
   http.send(null); 

} 

function handleResponse() { 

   if(http.readyState == 4 && http.status == 200){ 

      // Text returned FROM the PHP script 
      var response = http.responseText; 

      if(response) { 
         // UPDATE ajaxTest content 
         document.getElementById("lcontainer").innerHTML = response; 
      } 

   } 

} 
</script>
<?php
//connect to database
$sql = "SELECT `id`,`state` FROM `states`";//select all our states
$result = mysql_query($sql) or die(mysql_error());
echo '<select name="state" onChange="sendRequest(this.value)">';//the select box with the javascript function to change the cities
echo '<option value="none">Please select a state</option>'; //please select a state none

while(list($id,$state) = mysql_fetch_row($result)){
echo '<option value="'.$id.'"'.'>'.$state.'</option>';
}

 

Any ideas would be great.

 

Thanks in advance,

 

Scott

Link to comment
Share on other sites

Use $_POST['state'] (the name you picked) to get it from the form to do with what you like, assuming you are using POST method in your form.

 

I posted code on how to populate a option box from a DB, I'll see if I can find the post.

Link to comment
Share on other sites

I guess this could bring up a different question.  Is having a populated state and city drop down boxes for users to choose from the best thing to do?  I thought at first that this is a good idea but not so sure.  I know not all the cities will be in the list for one.

 

I just not sure what to do with bad spellings or just wrong data in general if I allow users to input this info on their own. 

 

On my main page there will be an area for others to do a search by city and state to find items in that area. 

 

Any other ideas of what I can do?

Link to comment
Share on other sites

Depends on what you want to do with the data.  If the data needs to be the same because something depends on the spelling, then use a drop down.  If you don't care if they don't know how to spell a city or state, then let them enter it.

 

I guess this could bring up a different question.  Is having a populated state and city drop down boxes for users to choose from the best thing to do?  I thought at first that this is a good idea but not so sure.  I know not all the cities will be in the list for one.

 

I just not sure what to do with bad spellings or just wrong data in general if I allow users to input this info on their own. 

 

On my main page there will be an area for others to do a search by city and state to find items in that area. 

 

Any other ideas of what I can do?

Link to comment
Share on other sites

i have a code for a dynamic (updates from a database) triple menu utilizing javascript if you'd like it..?

 

That would be great.  It almost sounds like the one I am using right now though.  My drop down boxes work fine, I just don't know for sure how to save what a user choose and then how to take what the user choose and then update the drop down box with the selection the user had if he/she goes back to edit.

Link to comment
Share on other sites

Can someone explain to me what this code is doing?

 

<?php
//connect to database
$sql = "SELECT `id`,`state` FROM `states`";//select all our states
$result = mysql_query($sql) or die(mysql_error());
echo '<select name="state" onChange="sendRequest(this.value)">';//the select box with the javascript function to change the cities
echo '<option value="none">Please select a state</option>'; //please select a state none

while(list($id,$state) = mysql_fetch_row($result)){
echo '<option value="'.$id.'"'.'>'.$state.'</option>';
}
echo '</select>';
?>

 

I don't understand where the $id and $state is coming from, it's not initialized any where.  This all has to do with my drop down boxes for States and Cities.  I am still having a very hard time understanding how to work this.

 

I have a $stateid (which is equal to the id stored in the database for that user - manually placed this in the database for now) and a $select_state (which is equal to the name of the State stored in the database for that user - manually placed this in the database as well).

 

http.open('get', 'getcities.php?id='+id); 

 

I have this bit of code that is used to select the cities.  I am not sure what the 'getcities.php?id ='+id) is doing.  I see that it's using the getcities.php file but not sure what the ?id='+id is for.

 

<?php
include "dbconnect.php";
//connect to database
$id = $_GET['id'];
$sql = "SELECT `id`,`city` FROM `cities` WHERE `state_id`=$id";//select the cities from the given state
$result = mysql_query($sql) or die(mysql_error());
//display the contents of our div tag
echo '<select name="cities">';
while(list($id,$city) = mysql_fetch_row($result)){
echo '<option value="'.$id.'">'.$city.'</option>';
}
echo '</select>';
?>

This is the getcities.php

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.