Jump to content

Need help with dropdown menu displaying results from mysql please


Recommended Posts

Hello everyone,

 

So what I'm trying to do is have a dropdown menu displaying a number of <options> for people to select and to update that selection to the database, easy enough right? But I want that option to be displayed as the "selected" option when the page is revisited or refreshed and I just can't figure it out!!! (Permission to bang head on desk?) It would seem like it sould be a really basic thing to do but it's got me completely and a lot of menus around the site are going to rely on this so I came to you guys for help.

 

A simple example would be like the facebook edit profile page, the user selects whether they are Male or Female, the database gets updated and when you return the option you selected before is the one that appears as if selected="selected" had been done. I've tried everything I can think of (all be it from a learners perspective) with no joy, ive managed to get the database connection sorted, the tables done, the login with unique id $_SESSION, logout etc... so then when I got to this I thought... easy LOL yeah right.

 

Some of this probably doesnt even make sense but I'll show you the kind of things I've tried...

 

 

            <select name="gender" size="1" id="gender">

              <option value="male" <?php if ($gender == "male") {echo 'selected="selected"';} ;?>>Male</option>

              <option value="female" <?php if ($gender == "female") {echo 'selected="selected"';} ;?>>Female</option>

            </select>

 

 

 

OR

 

 

    <select name="gender" id="gender">

              <option value="" selected="<?php if (!isset($gender)) {echo "selected";} ;?>">Select</option>

              <option value="male" selected="<?php if ($gender == "male") {echo "selected";} else {echo "";} ;?>">Male</option>

              <option value="female" selected="<?php if ($gender == "female") {echo "selected";} else {echo "";} ;?>">Female</option>

            </select>

 

 

OR

 

 

 

            <select name="gender" size="1" id="gender">

              <option selected="<?php if (!isset($gender)) {echo "selected";} ;?>">Select</option>

              <option value="<?php if ($gender == "Male") {echo "selected";} else {echo "male";} ;?>">Male</option>

              <option value="<?php if ($gender == "Female") {echo "selected";} else {echo "female";} ;?>">Female</option>

            </select>

 

 

OR

 

            <select name="gender" id="gender">

              <option value="male"><?php if ($gender == "male") {echo "Male";} ;?></option>

              <option value="female"><?php if ($gender == "female") {echo "Female";} ;?></option>

            </select>

 

 

Honestly man, I've got no idea.

 

 

The other thing is, I have more than 1 dropdown menu in the same form (5 in total) and if I use 2 or more selecting different options as I go I get a blank screen.  And one more, if I have selected Male and it updates the users row and I resubmit Male again it's blank screen time again, lol.

 

Any help would be tremendous and greatly appreciated.

 

Thanks very much, Learner

 

P.S  :wtf: Man!

 

This is what I did for my website.

 

<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']?>">
  <select name="course" id="course" tabindex="30">
  <option value="0">
<?php
$result = mysql_query("SELECT * FROM course");
while ($row = mysql_fetch_array($result)) {
$courseId = $row["courseId"];
        $courseName = $row["courseName"];
        $option="<OPTION VALUE=\"$courseId\">" . $courseName;
echo $option;
} ?>

Well thanks for your help so far, indeed echoing the variables back is a good idea to check that they have the value from the database or $_POST after the form is submitted (which they do).  Not sure I understand your example madjack, but thanks yeah.  I got the relivent pieces of the code together and thought I'd post it here so you can see.

 


<?php session_start(); ?>

<?php if (isset($_SESSION['id']))

$id = $_SESSION['id'];
$email = $_SESSION['email'];

// Connect to database
    include("../includes/connect.php");

// Select Database
$sql = mysql_query("SELECT gender FROM members WHERE id='$id' AND email='$email'");

if (!$sql) {
die(mysql_error());
}

// Use returned data
while ($row = mysql_fetch_array($sql)) {
$gender = $row['gender'];
}


//Get the POST results from the form 
if (isset($_POST['submit'])) {
$gender = $_POST['gender'];
}

// Add user info into the database table for the main site table

mysql_query("UPDATE members SET gender='$gender' WHERE id='$id' AND email='$email' LIMIT 1");


if (!mysql_affected_rows() == 1) {
die(mysql_error());
}


} // Close if (isset($_POST['submit']))

?>

<html>
<body>

<form action="same_page.php" method="post" enctype="multipart/form-data">

    <select name="gender" id="gender">
              <option selected="selected">Please Select</option>
              <option value="Male">Male</option>
              <option value="Female">Female</option>
            </select>
            
<input type="submit" name="submit" id="profile_submit" value=" Save and Update! " />

</form>


<?php echo "{$gender}" ;?>

</body>
</html>

 

See now this works fine, it returns data from the database or from the POST but each time the page is refreshed the "selected" option (or default) is set to 'Please Select' instead of using the new value from the database.

 

I have also tried putting

<?php echo "{$gender}" ;?>

in the value and/or the selectable output text which also works, like so ->>


<form action="same_page.php" method="post" enctype="multipart/form-data">

    <select name="gender" id="gender">
              <option value"<?php echo "{$gender}" ;?>"><?php echo "{$gender}" ;?></option>
              <option value="Male">Male</option>
              <option value="Female">Female</option>
            </select>

            
<input type="submit" name="submit" id="profile_submit" value=" Save and Update! " />

</form>

 

This does grab the data from the member row and appear as the value previously submitted, but, if you go to change the selection to something else when you click the dropdown you have 2 entries which are the same.  I.e, if Male was selected it would show Male twice and Female once.  I hope you understand what I mean, if not I'll post a .GIF

 

Actually here it is anyway :shrug:

 

6136271569

 

Ok so I dont know how to print the img here but it's attached, up in the top right corner is the echo return variable from the datebase. All the other things I posted before were my attempts to get around this seemingly easy problem, LOL.

 

Cheers guys, Learner

 

[attachment deleted by admin]

The selected attribute's value doesn't matter, and it seems like you assume it does here:

<select name="gender" id="gender">
              <option value="" selected="<?php if (!isset($gender)) {echo "selected";} ;?>">Select</option>
              <option value="male" selected="<?php if ($gender == "male") {echo "selected";} else {echo "";} ;?>">Male</option>
              <option value="female" selected="<?php if ($gender == "female") {echo "selected";} else {echo "";} ;?>">Female</option>
            </select>

 

However, even if you do selected="false" or selected="NOT SELECTED" or anything, the attribute is still there and thus your mark it to be selected. When you have multiple things marked for selection, the first tag you have is the one selected. Perhaps you should try

 

<select name="gender" id="gender">
              <option value="" <?php if (!isset($gender)) {echo "selected='selected'";} ;?>>Select</option>
              <option value="male" <?php if ($gender == "male") {echo "selected='selected'";} else {echo "";} ;?>>Male</option>
              <option value="female" <?php if ($gender == "female") {echo "selected='selected'";} else {echo "";} ;?>>Female</option>
            </select>

 

this ensures that only the previously selected option has the selected attribute

Thanks for the effort mikesta but the problem still remains, I copied the code and planted it straight into the page but it still returns 'Select' as the selected option :-\.  It appears (the way DW writes it anyway) that selected="selected" is the way it defines it although even if you are indeed correct the result is the same.  With the way it's written, the selected value should only appear in one of the <options> right? either there is nothing in the DB for it to  echo and thus Select is used or it's either Male or Female, but alas no joy.

 

Thanks again, Learner

Ok update, I came back with a clear head and wrote it again.  It's only marginally different to the code you wrote mikesta but it is working.  It could either be that I used Capitals or the apostraphes are the other way round perhaps, either way it's DONE lol.  Just so you can see, the code I put was as follows...

 

	<select name="gender" id="gender">
              <option value="" <?php if (!isset($gender)) {echo 'selected="selected"';} else {echo "";} ;?>>Select</option>
              <option value="Male" <?php if ($gender == "Male") {echo 'selected="selected"';} ;?>>Male</option>
              <option value="Female" <?php if ($gender == "Female") {echo 'selected="selected"';} ;?>>Female</option>
            </select>

 

As for the next part of the conundrum if that's how you spell it.  If the selected item is submitted twice I get a blank screen?!?!? Any thoughts, anybody? I think I need to find a way to cancel the update to the database if it's gonna do that.  The other thing still is the multiple dropdowns, if I make selections on more than one menu (say the second was a date (day) seletor and the third a month selector I get the blank screen again. 

 

Appreciate the help guys, Learner

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.