Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HELP: How To Keep Selected Option When Submit Button Is Clicked Using Php
#1
I have a select input in my site, which i want users to use to select their gender but the problem I do face is that when any option is been selected and then the submit button is been clicked, I will like the selected value to remain selected and when the page is been refreshed, my php code will check my database for the option submitted and select it (this is been done and acheived by the $set_gender variable so I have no problem with this). 
Below is my php code and html which I use;

Code:
<?php
// db
include 'db.php';
// gender
   $gender_query = mysqli_query($db, "SELECT * FROM users_gender WHERE id = '$id'");
   while ($row = mysqli_fetch_array($gender_query, MYSQLI_ASSOC)) {
       $set_gender = $row["gender"]; // male or female
   }
?>





Code:
<div class="form-group">
    <b>Gender</b>
   <select class="form-control custom-select" name="gender" id="gender" required>
       <option value="male" <?php if (isset($_POST['gender']) && $_POST['gender'] == "male") { echo "selected"; } elseif ($set_gender == "male") { echo "selected"; } ?>>Male</option>
       <option value="female" <?php if (isset($_POST['gender']) && $_POST['gender'] == "female") { echo "selected"; } elseif ($set_gender == "female") { echo "selected"; } ?>>Female</option>
  </select>
</div>

How the code above works;

If a user selects the male option and click on my form's submit button, the option automatically changes to the female (option) and if a user selects the female option and click on my form's submit button, the option doesn't change to any but keeps the female (option) selected. This works perfectly for the second option and not for the first. 
The `$set_gender` variable is to select the option been inserted in my database so that when ever any user access the page, he/she will be able to know/see the current gender he/she is using (this also works perfect). 

Am only having a single problem with the male option not been selected when the submit button is been clicked but being selected after the page is been refreshed (because `$set_gender == "male"` which means a user has used male as his gender and has saved it into my database so therefore, it will be selected).

I will like to know how to fix this because I also have another select option with more than 200 options (in order not to stress a user).
@Laive @Deelino @dealwap @Dynamo @abcheats @devHammed
I am a proud member of NIgerian Coders Team
#2
You can simply do something like below:


Code:
<option value="male" selected="<? echo($gender=="male"); ?>" > Male </option>

It takes so long to learn simplicity.
Reply
#3
(09-29-2018, 09:28 AM)dealwap Wrote:
You can simply do something like below:


Code:
<option value="male" selected="<? echo($gender=="male"); ?>" > Male </option>


when the submit button is been clicked, only the first option is been selected and if the second option is been selected and the submit button is been clicked, the first option is been selected instead of selecting the second option after submit because the second option was the one been selected (Except the page is been refreshed then $set_gender selects the selected option been inserted into my database).

Your code doesn't solves my question.
I am a proud member of NIgerian Coders Team
Reply
#4
You do not understand what I wrote there.
It takes so long to learn simplicity.
Reply
#5
(09-29-2018, 07:43 PM)dealwap Wrote:
You do not understand what I wrote there.


hm :/ I think so too...
I am a proud member of NIgerian Coders Team
Reply
#6
@cheatex
I am a proud member of NIgerian Coders Team
Reply
#7
The issue probably lies within your elseif part... Where did you define the $gender variable you are using in the elseif part of the conditional???
Always try not to spend tomorrow regretting what you should have done today! Smile
Reply
#8
(09-30-2018, 07:08 PM)Laive Wrote:
The issue probably lies within your elseif part... Where did you define the $gender variable you are using in the elseif part of the conditional???


sorry, that '$gender' is to be '$set_gender'...
I am a proud member of NIgerian Coders Team
Reply
#9
(10-01-2018, 08:22 AM)Aaron Wrote:
sorry, that '$gender' is to be '$set_gender'...

Then correct it and try again bro...
Always try not to spend tomorrow regretting what you should have done today! Smile
Reply
#10
(10-02-2018, 08:01 PM)Laive Wrote:
Then correct it and try again bro...


Wow, I just did and I decided to use this code which works for me...
My current code...

Code:
<?php if(isset($_POST['gender']) && $_POST['gender'] == 'Male') { echo 'selected'; } ?>


and for me to add my `$set_gender` to work, all I need to do is just to add an elseif statement in my current code like this;

Code:
elseif ($set_gender == 'Male') { echo 'selected'; }



Thanks @Laive
I am a proud member of NIgerian Coders Team
[-] The following 1 user Likes Aaron's post:
  • Laive
Reply
#11
(10-03-2018, 08:04 AM)Aaron Wrote:
Wow, I just did and I decided to use this code which works for me...
My current code...

Code:
<?php if(isset($_POST['gender']) && $_POST['gender'] == 'Male') { echo 'selected'; } ?>


and for me to add my `$set_gender` to work, all I need to do is just to add an elseif statement in my current code like this;

Code:
elseif ($set_gender == 'Male') { echo 'selected'; }



Thanks @Laive

You're welcome bro! But let others see the [full] new version of your code... Smile
Always try not to spend tomorrow regretting what you should have done today! Smile
[-] The following 1 user Likes Laive's post:
  • Aaron
Reply
#12
(10-04-2018, 11:26 AM)Laive Wrote:
You're welcome bro! But let others see the [full] new version of your code... Smile


is there in my last post u quoted (my php code)...
Anyway, here it is;

Code:
<?php if(isset($_POST['gender']) && $_POST['gender'] == 'Male') { echo 'selected'; } elseif ($set_gender == 'Male') { echo 'selected'; } ?>

I am a proud member of NIgerian Coders Team
[-] The following 1 user Likes Aaron's post:
  • Laive
Reply




Users browsing this thread: 1 Guest(s)

About Nigeria's Creative Talents

Nigeria's Creative Talents is an online community where every creative Nigerians can share ideas with themselves.

For any more information, please use our contact form.

              User Links

              Advertise