Jump to content

How should I do this select field in a form?


DavidPr
 Share

Recommended Posts

<b>State:</b><br><select name="state" value=""><option value="" selected>Select a State</option><?phpinclude('includes/dbstates.php');$query = "SELECT * FROM states";$result = mysql_query($query) or die('Error, query failed');while($row = mysql_fetch_array($result, MYSQL_ASSOC)){$postal = $row['postal'];$statename = $row['statename'];echo "<option value='$postal'>$statename</option>";}?></select>

postal is the 2 or 3 digit abbreviation for the state.I'm using this in the rest of the form:

value="<?php if (isset($_POST['state'])) echo $_POST['state']; ?>">

Would the appropriate postal code be forwarded if I put this in the value="" in - <select name="state" value="">And I'm doing this to check the entry:

// Check for a state.if (eregi ('^[[:alpha:]\.\' \-]{2,3}$', stripslashes(trim($_POST['state'])))) {$s = escape_data($_POST['state']);} else {$s = FALSE;echo '<p><font color="red" size="+1">Please enter your state!</font></p>';}

Look OK?

Link to comment
Share on other sites

The select tag should not have a value attribute, the value of a select element comes from the value of the selected option. If you want to display the select list and have a certain option selected, you would use the selected attribute on that option.<select name="test"><option value="1">one</option><option value="2">two</option><option value="3" selected="selected">three</option></select>

Link to comment
Share on other sites

This is on the registration form and the regular text input fields had it this way in their value tags. I think it was done this way in case there was an error in the form and you had to fix something, the fields where the information was entered correctly wouldn't have to be re-entered.But if it's wrong I'll remove it and leave it blank.

<b>State:</b><br><select name="state" value="<?php if (isset($_POST['state'])) echo $_POST['state']; ?>"><option value="" selected>Select a State</option><?phpinclude('includes/dbstates.php');$query = "SELECT * FROM states";$result = mysql_query($query) or die('Error, query failed');while($row = mysql_fetch_array($result, MYSQL_ASSOC)){$postal = $row['postal'];$statename = $row['statename'];echo "<option value='$postal'>$statename</option>";}?></select>

Is this the correct way of doing the below. The echo tag uses double quotes and the tags inside use single quotes, but what about the single quotes in the session call?echo "<option value'{$_SESSION['state']}' selected='selected'>$statename</option>";

Link to comment
Share on other sites

My session variable for state isn't showing up right. It's in the users table correctly, but when displayed it shows up as Ala when it should be AL. What would cause this?The login script uses this to authenticate the user and set session variables from the users database table:

// Query the database.$query = "SELECT * FROM users WHERE (email='$e' AND pass=SHA('$p')) AND active IS NULL";$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());if (@mysql_num_rows($result) == 1) { // A match was made.// Register the values & redirect.$row = mysql_fetch_array ($result, MYSQL_NUM);mysql_free_result($result);mysql_close(); // Close the database connection.$_SESSION['first_name'] = $row[3];$_SESSION['user_id'] = $row[0];$_SESSION['last_name'] = $row[4];$_SESSION['company_name'] = $row[5];$_SESSION['city'] = $row[7];$_SESSION['state'] = $row[8];

OK I had to change the state in the users table to stateid and now the correct AL is showing up. However, in the jobs table in the state_id field it is still Ala. I have an edit script with this on it:

State:  (Selected = Current State)<br><select name="state_id" size="10"><?php$query  = "SELECT statename FROM states where postal = '{$_SESSION['stateid']}'";$result = mysql_query($query) or die(mysql_error());while($row = mysql_fetch_array($result, MYSQL_ASSOC)){$statename = stripslashes($row['statename']);echo "<option value'{$_SESSION['stateid']}' selected='selected'>$statename</option>";}$query = "SELECT * FROM states";$result = mysql_query($query) or die('Error, query failed');while($row = mysql_fetch_array($result, MYSQL_ASSOC)){$postal = $row['postal'];$statename = $row['statename'];echo "<option value='$postal'>$statename</option>";}?></select>

The update portion:

<?phpif ($_POST["submit"]) {$job_position = mysql_real_escape_string($_POST['job_position']);$city = mysql_real_escape_string($_POST['city']);$display_name = mysql_real_escape_string($_POST['display_name']);$cat_id = mysql_real_escape_string($_POST['cat_id']);$state_id = mysql_real_escape_string($_POST['state_id']);$status = mysql_real_escape_string($_POST['status']);$content = mysql_real_escape_string($_POST['content']);$sql = "UPDATE jobs SET display_name='$display_name', cat_id='$cat_id', state_id='$state_id', job_position='$job_position', status='$status', city='$city', content='$content' WHERE id=$id"; $result = mysql_query($sql);if($result){echo "<br><br><p align='center'><span style='color:red'>Job ad updated.</span><br><br><a href='edit_jobs.php'>Edit another job ad</a></p>";}else{echo "<br><br><p align='center'><span style='color:red'>There was an error! Job ad did not update.</p>";}}}?>

THe last curly bracket closes one farther up in the script.The content field will update but not the state_id field. Any ideas as to why?

Link to comment
Share on other sites

This is on the registration form and the regular text input fields had it this way in their value tags.
Input tags have a value attribute, select tags don't.
Is this the correct way of doing the below. The echo tag uses double quotes and the tags inside use single quotes, but what about the single quotes in the session call?echo "<option value'{$_SESSION['state']}' selected='selected'>$statename</option>";
You missed an equal sign, and you can use double quotes in the tag as well.echo "<option value=\"{$_SESSION['state']}\" selected=\"selected\">$statename</option>";
However, in the jobs table in the state_id field it is still Ala.The content field will update but not the state_id field.
It sounds like you're storing the wrong data in the state_id field. Open up the table in phpMyAdmin and check the data in each field to make sure it is what you intended. All fields in the update statement are updating, it would never be the case that one field in an update statement would update but not another one, that's not how databases work. Again, you might be using the wrong value for state_id.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...