Jump to content

Form Session + dropdown menu


xXREDXIIIXx

Recommended Posts

OK its my first day here,

I usually like to figure these problems out by myself but i been looking at the same code for a while now all I see are colours :D.

 

This is the code that is not working, no matter what input i give it, it always returns as the last result due to it saying everything is the default 'selected' this is from registration.php

$genders = array('Select Gender', 'Male', 'Female');
                   
echo '<select name="gender" class="text_form_field">';
foreach($genders as $gender){
    echo '<option value="'.$gender.'"'.($_SESSION['gender'] == $gender ? ' selected="selected"':'').'>'.$gender.'</option>';}
echo '</select>';
echo $_SESSION['gender_check']; unset($_SESSION['gender_check']); ?>

 

Here is the php_registration.php code

$gender = $_POST['gender'];
$_SESSION['gender'] = $gender;
if ($gender == 'Select Gender'){
    $_SESSION['gender_check'] = "<br><span class=\"text_error\">Gender is required.</span>";}

 

If anyone can see my typo or sommit plz tell me I can only learn from my mistakes ;)

Link to comment
https://forums.phpfreaks.com/topic/157291-form-session-dropdown-menu/
Share on other sites

yes last result is Female the output HTML looks like this:

<tr>
<td align="right" valign="top" width="30%">Gender:</td>
<td>
  <select name="gender" class="text_form_field">
      <option value="Select Gender" selected="selected">Select Gender</option>
      <option value="Male" selected="selected">Male</option>
      <option value="Female" selected="selected">Female</option>
  </select><br>
<span class="text_error">Gender is required.</span>
</td>
</tr>

Can you put this in and tell me what it outputs:

$genders = array('Select Gender', 'Male', 'Female');

var_dump($_SESSION['gender']); // added this line

echo '<select name="gender" class="text_form_field">';
foreach($genders as $gender){
    echo '<option value="'.$gender.'"'.($_SESSION['gender'] == $gender ? ' selected="selected"':'').'>'.$gender.'</option>';}
echo '</select>';
echo $_SESSION['gender_check']; unset($_SESSION['gender_check']); ?>

 

But try using 3 equals instead of 2 before you do that.

I didn't get any errors on Gender and that was the only thing I did fill in. :D

 

But I do get the Female even though I selected Male and I do confirm that the source has everything selected.

 

In the foreach loop, can you add this:

echo $_SESSION['gender'] . ' & ' . $gender . ' = ' . ($_SESSION['gender'] == $gender);

I didn't get any errors on Gender and that was the only thing I did fill in. :D

 

But I do get the Female even though I selected Male and I do confirm that the source has everything selected.

 

In the foreach loop, can you add this:

echo $_SESSION['gender'] . ' & ' . $gender . ' = ' . ($_SESSION['gender'] == $gender);

 

foreach($genders as $gender){
                         echo '<option value="'.$gender.'"'.($_SESSION['gender'] == $gender ? ' selected="selected"':'').'>'.$gender.'</option>';
					 echo $_SESSION['gender'] . ' & ' . $gender . ' = ' . ($_SESSION['gender'] == $gender);}

 

if you ment like that its added and online; no change

 

here is the language code that works:

 

<?php // LOCATION
$locations = array('Select Location', 'United Kingdom', 'United States', 'Region 3');
                    
echo '<select name="location" class="text_form_field">';
foreach($locations as $location){
    echo '<option value="'.$location.'"'.($_SESSION['location'] == $location ? ' selected="selected"':'').'>'.$location.'</option>';}
echo '</select>';
echo $_SESSION['location_check']; unset($_SESSION['location_check']); ?>

Is it live? I don't see the display anywhere. :(

 

<?php // GENDER
$genders = array('Select Gender', 'Male', 'Female');
                   
echo '<select name="gender" class="text_form_field">';
foreach($genders as $gender){
    echo '<option value="'.$gender.'"'.($_SESSION['gender'] == $gender ? ' selected="selected"':'').'>'.$gender.'</option>';
echo $_SESSION['gender'] . ' & ' . $gender . ' = ' . ($_SESSION['gender'] == $gender);}
echo '</select>';
echo $_SESSION['gender_check']; unset($_SESSION['gender_check']); ?>

 

ALL Live  :-\

Did you take down gender?

 

yeah I was getting a internal error when that was up(when form was submitted completely empty), so I took that off I hav copyed all the code onto another page called reg.php I changed something on the month section of birthday and now it seems to be working (I have stripped all of the errors from the personal section - wanted to localize the error). I am going to add the error codes on and see what happens will report back tonight.

OK Reporting back,

 

All I done was shortened the variable names,

 

So this:

<?php // GENDER
$genders = array('Select Gender', 'Male', 'Female');
                   
echo '<select name="gender" class="text_form_field">';
foreach($genders as $gender){
    echo '<option value="'.$gender.'"'.($_SESSION['gender'] == $gender ? ' selected="selected"':'').'>'.$gender.'</option>';
echo $_SESSION['gender'] . ' & ' . $gender . ' = ' . ($_SESSION['gender'] == $gender);}
echo '</select>';
echo $_SESSION['gender_check']; unset($_SESSION['gender_check']); ?>

 

Now looks like this:

<?php // GENDER
$gs = array('Select Gender', 'Male', 'Female');
                   
echo '<select name="gender" class="text_form_field">';
foreach($gs as $g){
    echo '<option value="'.$g.'"'.($_SESSION['gender'] == $g ? ' selected="selected"':'').'>'.$g.'</option>';
echo $_SESSION['gender'] . ' & ' . $gender . ' = ' . ($_SESSION['gender'] == $gender);}
echo '</select>';
echo $_SESSION['gender_check']; unset($_SESSION['gender_check']); ?>

 

That was it, Most sillest thing I have come accross to date (and yes I check there was no variable clashes)

 

Think this can be classed as solved?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.