Jump to content

Form Dropdown Issue


neverforget98

Recommended Posts

Hey everyone, I've made several posts in the past 36 hours...anyways. I'm making a system called Integrated Services for the Charitable Organization that I run and I ran into another roadblock with some of the snippets I'm using. Here is the issue...

 

I have a table page (index.php) that shows all of the data in the table, I have a row adding page (add.php) that adds volunteers, I have a row deletion page (delete.php) which deletes volunteers, and I have a row editor page (viewer.php) which updates volunteers profiles - so to say.

On the Volunteer Profile, we have about 12 drop-down menus that have Yes/No and Preferred Contact options. This works perfectly fine on the viewing and adding page, however, when you try to update, I can't get it to work. The updating page calls to the database for ALL of the information and I know that is working, because my text fields I have added value="<?php echo $VARIABLE?>" (replacing VARIABLE, obviously) to make sure that the information form the database goes to the text fields. It shows up on the page when the page finds the ID in the address and sends it to the database and calls back the information. NOW, with the dropdown menus (select) I don't know how to make it so the option that is automatically selected is the option that is in the database. It's different for every volunteer so I need to know what I would use.

 

Thanks and I hope someone can help me. :) If you need any more information, lemme know!

 

-B

Link to comment
Share on other sites

So if I understood you well, you are trying to get the selected value of each dropdown list and update the database, right?

 

Assuming that you have a select option in your form, example

 

<select name="myColor">
<option selected="selected">- color -</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="black">Black</option>
</select>

 

Then, in your PHP script, you can reference this field and get its value this way:

$mySelectedColor = $_POST['myColor']

 

Now in order to reflect what is in the database when you show you form again, you can do something like this:

 

Assuming that the field $mySelectedColor has your data from the database:

 

<select name="myVar">

foreach($colors as $color){

if($mySelectedColor == $color){

 

}else{

}

 

}

</select>

Link to comment
Share on other sites

So if I understood you well, you are trying to get the selected value of each dropdown list and update the database, right?

 

Assuming that you have a select option in your form, example

 

<select name="myColor">
<option selected="selected">- color -</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="black">Black</option>
</select>

 

Then, in your PHP script, you can reference this field and get its value this way:

$mySelectedColor = $_POST['myColor']

 

Now in order to reflect what is in the database when you show you form again, you can do something like this:

 

Assuming that the field $mySelectedColor has your data from the database:

 

<select name="myVar">

<?php foreach($colors as $color){
if($mySelectedColor == $color){
$selected = 'selected="selected"';
}else{
$selected = '';
}?>
<option <?php echo $selected?> value="<?php echo $color?>"><?php echo $color?></option>
<?php } ?>
</select>

 

There might be other better solution but this is what i can think of at this moment

Edited by hell_yeah
Link to comment
Share on other sites

I'm using this code:

<tr>
 <td>Preferred Contact:</td>
 <td><select name="pcontact">
 <?php foreach($pcontact as $pcontact) {
 if($pcontact == $pcontact {
 $selected = 'selected="selected"';
 }else{
 $selected = '';
 } ?>
 <option <?php echo $selected?> value="</php echo $pcontact?>"><?php echo $pcontact?></option>
 <?php } ?>
 </select></td>
</tr>

 

And I'm getting an error saying unexpected ; on line 150 which is this line:

$selected = 'selected="selected"';

 

Help. /:

Link to comment
Share on other sites

Sorry, you have a missing parenthesis after the if statement, it should be this way

 

if($databasePcontact == $pcontact ) {

 

and try to have different variables name as shown in my example as your statement will be always true.

Edited by hell_yeah
Link to comment
Share on other sites

That's because foreach expect an array as argument and you are just passing a normal variable. The above code I have given was supposed to be an example that you can follow in order to fix your issue. If your dropdawn lists have only yes/no values, then you can follow the below code and modify it in order to fit your needs:

 

<?php
// This value is supposed to have whatever stored in the database. I am just using it here to
// set it to the value I need in order to demonstrate the concept for you
$yesNoValue = "no";
?>
<select name="test">
<option value="yes" <?php echo ($yesNoValue=="yes") ? 'selected="selected"': '' ?> >yes</option>
<option value="no" <?php echo ($yesNoValue=="no") ? 'selected="selected"': '' ?> >no</option>
</select>

Edited by hell_yeah
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.