/    Sign up×
Community /Pin to ProfileBookmark

Need help with my drop down box

I am creating a website that allows a user to fill out some information, some of this includes selecting a state and city from a drop down box. I am able to get this to work for the user to use but I am not sure as to how I go about saving what they select in my mySQL database. Also, if the user comes back to this page I want all their data to pre-load for them. I am unsure as to how to pre-load these drop-down boxes with what was saved in the database.

Here is my code:

[CODE]
<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>’;
}
[/CODE]

Any ideas would be greatly appreciated.

Thanks,

Scott

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@nap0leonDec 09.2007 — For setting values in a drop-down box, you can create your select box then loop through each value to find which one = the value retuned from the database.

In the code below, you would change 'alabama; in the line "var selectedState = 'alabama';" to be whatever code you need to set it to be the value from the database.

[CODE]
<form id="form1" name="form1" action="" method="post">
<select name="state">
<option value="none">Please select a state</option>
<option value="alabama">Alabama</option>
<option value="arkansas">Arkansas</option>
</select>
<script>
var i=0;
var selectedState = 'alabama';
while ((document.forms("form1").state.options[i].value != selectedState) && (i < document.forms("form1").state.options.length))
{i++;}

if (i < document.forms("form1").state.options.length)
{document.forms("form1").state.selectedIndex = i;}
</script>
</form>
[/CODE]


Here's a page I built illustrating this example:

http://www.jasondahlin.com/WebSite2/CodeSnippets/JavaScript-Set-Drop-Down-Selection.aspx
×

Success!

Help @scarlson spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...