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
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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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']); ?>

Link to comment
Share on other sites

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  :-\

Link to comment
Share on other sites

I see it. Apparently $_SESSION['gender'] is either '' or NULL. I don't know which. Can you put var_dump($_SESSION['gender']) in the foreach instead so we can see which it is.

 

Live, starts as NULL obviously then changes to what ever ya selected.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

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.