Keep Option Selected after form submission

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts: 282
Location: Holland Rotterdam

Keep Option Selected after form submission

I have this code to update one from your scripts
But if i select the option Other it does not stay in de admin on this same line
I hope you could help me out

Code: Select all

<?
if($object->user_facebook=="Male"){
$ga = "selected";
}
elseif($object->user_facebook=="Female"){
$gb = "selected";
}
else{
$gc = "selected";
}
?>
  <select size="1" name="user_facebook">
  <option value="Male"<? echo $ga ?>>Male</option>
  <option value="Female"<? echo $gb ?>>Female</option>
  <option value="Other"<? echo $gc ?>>Other</option>
  </select><br>
I Searching few hours for an answer but still nothing
Hope you realy could help me out.
Keep values selected after form submission ???

Admin Posts: 805
Hello
I did not understand what you mean with: "it does not stay in de admin on this same line".
To keep option selected after form submission in your code, try this;

Code: Select all

<?php
$ga = $gb = $gc ='';
if(isset($_POST['user_facebook'])){
  if($_POST['user_facebook']=='Male') $ga =' selected';
  else if($_POST['user_facebook']=='Female') $gb =' selected';
  else if($_POST['user_facebook']=='Other') $gc =' selected';
}
?>
<select size="1" name="user_facebook">
<option value="Male"<? echo $ga; ?>>Male</option>
<option value="Female"<? echo $gb; ?>>Female</option>
<option value="Other"<? echo $gc; ?>>Other</option>
</select><br>

JanMolendijk Posts: 282
Thanks for the Quick answer i found also a way to do it like this

Code: Select all

 Gender <select name="user_facebook" class="footerlink" value ="<? echo $user_facebook ?>" >



<option value="<?php echo $rws['user_facebook'];?>" <?php if($_POST['user_facebook'] == '') echo 'selected="selected"' ?>><?php echo $rws['user_facebook'];?></option>
<option value=""></option>
<option value="Male" <?php if($_POST['user_facebook'] == 'Male') echo 'selected="selected"' ?>>Male</option>

<option value="Female" <?php if($_POST['user_facebook'] == 'Female') echo 'selected="selected"' ?>>Female</option>

<option value="Neutral" <?php if($_POST['user_facebook'] == 'Neutral') echo 'selected="selected"' ?>>Neutral</option>

</select>

JanMolendijk Posts: 282
I hope you able to understand this problem
my English is only a little :D

I have a other problem now... I wanna make a on off switch,
but when i use this code my e-mail adress is comming into
option selected... But i would like to have only the words on or off
into the option selected with the switch on off from my e-mail-adress

Code: Select all

E-mail (on-off)<?php
$ga = $gb = $gc ='';
if(isset($_POST['user_youtube'])){
  if($_POST['user_youtube']=='On') $ga =' selected';
  else if($_POST['user_youtube']=='Off') $gb =' selected';

}
?>
<select size="1" name="user_youtube">
<option value="<?php echo $rws["user_youtube"];?>"><?php echo $rws["user_youtube"];?></option>
<option value=""></option>
<option value="<?php echo $rws["user_email"];?>"<? echo $ga; ?>>On</option>
<option value="Off"<? echo $gb; ?>>Off</option>

</select><br>

Admin Posts: 805
Just delete the <option> that shows the e-mail address, and keep only the On /Off <option>s.
Something like this;

Code: Select all

<?php
$ga = $gb ='';
if(isset($_POST['user_youtube']) && strlen($_POST['user_youtube'])>0){
  if($_POST['user_youtube']=='Off') $gb =' selected';
  else $ga =' selected';

}
?>
<select size="1" name="user_youtube">
<option value=""></option>
<option value="<?php echo $rws["user_email"];?>"<? echo $ga; ?>>On</option>
<option value="Off"<? echo $gb; ?>>Off</option>
</select>

JanMolendijk Posts: 282
Thank you Admin & tomorow i go
on with it first a good sleep :roll:

JanMolendijk Posts: 282
The problem is the option on off does not stay selected in my script
do you known if their is an other posibility ?

Admin Posts: 805
The code from the previous answer should work. I not know way in your script it not works.
The code adds the "selected" attribute, after form submission, at the selected option.
Check the page source to see how it is the resulted html code.

JanMolendijk Posts: 282
Thank you Admin for the support