Jump to content

drop down list form helper...?


jamichelli

Recommended Posts

I'm working on getting my forms to properly validate.  I already have the text and textarea fields where they repopulate with the previous input should some other part of the form fail to validate.  The code for that goes like this:

 

// if there were errors, re-populate the form fields

  if (!empty($errors))

  { 

    $fields = $_POST;

  }

 

and then:

 

<input type="text" size="60" name="user_name" id="user_name"tabindex="1" value="<?=$fields['user_name']?>" />

 

Again, no problems with the text parts of the form repopulating upon validation failure...I'm just having a hard time doing the same thing with the select part of the form.  Any help here?  I'm trying to keep it as simple as possible.  Thanks!

Link to comment
Share on other sites

I'm just off to bed but one last post doesn't hurt!

 

I'd gather the contents from the form regardless of whether the form validates or not. That way you can post the data the user entered straight back in the form should they need to edit anything.

 

As for the select part...

$intSelectID=intval($_POST['myselect']);

We can get the option they've chosen from our select box.

 

Now to build the select box (from contents from the database) and make their selection the default one - we'll use a city selection box as an example.

$htmCities='';
$query=-mysql_query("SELECT id,city FROM cities ORDER BY city ASC");
while ($row=mysql_fetch_assoc($query)) {
  $htmCities='<option valud="'.$row['id'].'"'.($row['id']==$intSelectID ? ' selected="selected"' : '').'>'.$row['city'].'</option>';
}

 

Then we use that in our form:

<select name="myselect"><?php echo $htmCities; ?></select>

 

Hope that helps.

Link to comment
Share on other sites

Ok...here's what I have.  I'm actually doing this for a state list.  The problem that I'm having is that it only brings up the last state in my db...any clues?

 

code:

 

<?php

$con = mysql_connect("localhost","dbname","dbpassword");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("dbname", $con);

 

$state='';

$query= mysql_query("SELECT id,state FROM `states` ORDER BY state ASC");

 

while($row = mysql_fetch_assoc($query)) {

 

  $state='<option value="'.$row['id'].'"'.($row['id']==$intSelectID ? ' selected="selected"' : '').'>'.

$row['state'].'</option>'; }

 

?>

 

<select name=”state”><?php echo $state; ?></select>

 

 

 

Any help on this would be greatly appreciated! 

Link to comment
Share on other sites

The reason you are getting one thing and one thing only is because you are always overwriting your container:

 

$state='<option value="'.$row['id'].'"'.($row['id']==$intSelectID ? ' selected="selected"' : '').'>'.

$row['state'].'</option>'

 

should be:

 

$state.='<option value="'.$row['id'].'"'.($row['id']==$intSelectID ? ' selected="selected"' : '').'>'.

$row['state'].'</option>'

 

notice the dot after $state now you concatenate your strings, i used the shorthand version though the long version is:

 

 

$state= $state . '<option value="'.$row['id'].'"'.($row['id']==$intSelectID ? ' selected="selected"' : '').'>'.

$row['state'].'</optio

 

a mistake both Yesideez and you made!

Link to comment
Share on other sites

Thanks for the help ignace...now, what about this:

 

$intSelectID=intval($_POST['myselect']);

 

Now that I have this corrected and have the full list available, I just need to put this last part in...where ever it may go...and then be done with it.  I may try to figure that part out after a good night's...or day's...sleep.

 

Thanks again Yesideez and ignace!

Link to comment
Share on other sites

Ok...still having trouble and need a little help.  This is for a drop down list.  Where would I place the code snippet at the end of this post so that the selected value remains whenever the form fails validation and the user needs to fill in other parts of the form before re-submitting?

 

[code:

 

<?php

$con = mysql_connect("localhost","dbname","dbpassword");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("dbname", $con);

 

$state=';

$query= mysql_query("SELECT id,state FROM `states` ORDER BY state ASC");

 

while($row = mysql_fetch_assoc($query)) {

 

  $state.='<option value="'.$row['id].'"'.($row['id']==$intSelectID ? ' selected="selected"' : '').'>'.

$row['state'].'</option>'; }

 

?>

 

<select name=”state”><?php echo $state; ?></select>

 

code]

 

Where does this go???:

 

$intSelectID=intval($_POST['state']);

Link to comment
Share on other sites

before the while loop

 

$state.='<option value="'.$row['id'].'"'.($row['id']==$intSelectID ? ' selected="selected"' : '').'>'.

This code inside the while loop is telling it to basically:

if the current row in the database(while building the selects options) has an id value that matches the $intSelectID value (which you gave it from $_POST['state']), then add the text 'selected="selected"' otherwise add nothing, making it retain it's value.

 

I think that's accurate, it's kinda jumbled together ..

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.