Menu returns blank data

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
hotblooded
Forum Newbie
Posts: 19
Joined: Wed Sep 24, 2008 10:14 am

Menu returns blank data

Post by hotblooded »

I'm having an issue with a drop-down menu. I select an option and when I submit the form, it doesn't submit the data I selected, it submits blank data. I'm not sure why it's doing this because I compared the code to other drop-downs that I have that work and they look the same. It may have to do with the php statements I have within them, but I'm not sure. Here's the code:

Code: Select all

<tr valign="baseline">
      <td nowrap="nowrap" align="right">Fac_Dept:</td>
      <td>
      <select name="dept" id="dept">
      <?php
do {  
?>
      <option value="<?php echo $row_dept['Fac_Dept']?>"<?php if (!(strcmp($row_dept['Fac_Dept'], $row_facstaff_edit['Fac_Dept']))) {echo "selected=\"selected\"";} ?>><?php echo $row_dept['Fac_Dept']?></option>
      <?php
} while ($row_dept = mysql_fetch_assoc($dept));
  $rows = mysql_num_rows($dept);
  if($rows > 0) {
      mysql_data_seek($dept, 0);
      $row_dept = mysql_fetch_assoc($dept);
  } 
?>
    </select>
      </td>
    </tr>
Any thoughts?

Thanks
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Menu returns blank data

Post by califdon »

Well, if your php inserts don't result in finding a value, then there would be no value in the html for the dropdown menu, so of course it wouldn't result in anything when that selection is made. A better practice would be to assign all your database derived data to variables before you start echoing the choices. Test each one to see if it found a value, then decide what you want to do if there's no value (like maybe skip that selection). Then in your code that builds the selection syntax, use the variables rather than blindly assuming that the $row array always has values in it. That will also reduce your code clutter and make your script easier to read.
hotblooded
Forum Newbie
Posts: 19
Joined: Wed Sep 24, 2008 10:14 am

Re: Menu returns blank data

Post by hotblooded »

The weird thing about it is that the labels are populated, but seem to carry not value. In other words, I can choose which dept i want from the menu, but when i submit, it doesn't submit the value i wanted, it submits a blank value.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Menu returns blank data

Post by califdon »

hotblooded wrote:The weird thing about it is that the labels are populated, but seem to carry not value. In other words, I can choose which dept i want from the menu, but when i submit, it doesn't submit the value i wanted, it submits a blank value.
So take a look at the HTML source in your browser. Are the values in there? If they are, then the problem would seem to be in the way you are recovering the values from the Form, which would be in code that you haven't shown.
hotblooded
Forum Newbie
Posts: 19
Joined: Wed Sep 24, 2008 10:14 am

Re: Menu returns blank data

Post by hotblooded »

In the source code, it lists the values properly in both places but it still isn't updating right...what code would you need to see to figure it out?

Thanks
hotblooded
Forum Newbie
Posts: 19
Joined: Wed Sep 24, 2008 10:14 am

Re: Menu returns blank data

Post by hotblooded »

also, it doesn't seem to be an issue with the entire form because if i edit information in another field, it records properly into the database...i really don't know what's going on


Any thoughts?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Menu returns blank data

Post by califdon »

hotblooded wrote:In the source code, it lists the values properly in both places but it still isn't updating right...what code would you need to see to figure it out?

Thanks
So then the part of the script you showed, which generates the dropdown box syntax, is working properly. What you need to show is the part that reads the values in the $_POST variables after the Submit button is clicked. Somewhere, you should find code that looks roughly like this:

Code: Select all

$dept = $_POST['dept'];
hotblooded
Forum Newbie
Posts: 19
Joined: Wed Sep 24, 2008 10:14 am

Re: Menu returns blank data

Post by hotblooded »

I'm actually not seeing that variable in any of my pages (which could be the problem)...where should that be found? In the page that comes after clicking 'submit'?
hotblooded
Forum Newbie
Posts: 19
Joined: Wed Sep 24, 2008 10:14 am

Re: Menu returns blank data

Post by hotblooded »

Scratch that, I found it...it actually says exactly what you typed. What is the 'dept' inside the _POST brackets referring to?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Menu returns blank data

Post by califdon »

hotblooded wrote:Scratch that, I found it...it actually says exactly what you typed. What is the 'dept' inside the _POST brackets referring to?
It's the name of the element in the Form. In other words, if you have a Form that a user fills in, part of which looks like this in the html:

Code: Select all

 <form method='POST' action='[i]script that will handle this form[/i]'>
  Enter your Name here: <input type='text' [color=#008040] name='employee' [/color]/><br />
  Enter your Dept. here: <input type='text' [color=#008040] name='dept' [/color]/><br />
  <input type='submit' value='Submit Form'> </form>
then in the script that handles the submitted form (could be the same script or a different one), you would recover the values the user entered like this:

Code: Select all

$empl = $_POST['employee'];
$dept = $_POST['dept'];
In your case, instead of a simple <input type='text'...> element, it's a select element (a dropdown selection) that is created dynamically in php. So I suspect there may be a problem there. Your code is written with a do loop, which I almost never use, so I would rewrite the section that now looks like this:

Code: Select all

<select name="dept" id="dept">
       <?php
 do {  
 ?>
      <option value="<?php echo $row_dept['Fac_Dept']?>"<?php if (!(strcmp($row_dept['Fac_Dept'], $row_facstaff_edit['Fac_Dept']))) {echo "selected=\"selected\"";} ?>><?php echo $row_dept['Fac_Dept']?></option>
       <?php
 } while ($row_dept = mysql_fetch_assoc($dept));
   $rows = mysql_num_rows($dept);
   if($rows > 0) {
       mysql_data_seek($dept, 0);
       $row_dept = mysql_fetch_assoc($dept);
   }
 ?>
to look like this:

Code: Select all

<?php
echo "<select name='dept' id='dept'> ";
while ($row_dept = mysql_fetch_assoc($dept)); {
    $d = $row_dept['Fac_Dept'];
    echo "<option value='$d'";
    if (!(strcmp($d, $row_facstaff_edit['Fac_Dept']))) {
        echo " selected";
    }
    echo ">$d</option>";
}
echo "</select>";
?>
I'm not sure that will solve your original problem, but I found it difficult to read the code that you now have, so I rewrote it as I would have done.
hotblooded
Forum Newbie
Posts: 19
Joined: Wed Sep 24, 2008 10:14 am

Re: Menu returns blank data

Post by hotblooded »

I actually didn't write the code...I know very little about PHP and have been using DW to make the code, and I would just go in and edit it to do what I wanted it to...I actually got it working...

Thanks
Post Reply