Jump to content

can't get form with drop down box working.. confused with $_GET and $_POST!


thunderstorm654

Recommended Posts

Hi, im trying to use a drop down box to select data from my tables in my database that relate to just one column, which populates the drop down. I'm trying to display a drop down box full of race names pulled from a race table in my database, and this works fine, they're all listed correctly.

 

I want to then allow the user to select one, hit submit or whatever, then below, (OR on another page, I don't mind) in a table, display the member first and last names (from member table), the race date, length (from race table) and the result (from result table).

 

At the moment I'm stuck as whatever u select nothing happens. I'm really confused about how to get the race name from the submitted drop down, do you use get or post or what?? Need a simple explanation of what ive done wrong, I KNOW its wrong but just cant figure out how to fix it, getting muddled  ??? !

 

Thanks, and here's my code:

 

<?php
require_once('connection.php');
if (!isset($_GET['race'])) {
$res=mysql_query("select race_name from race");
	if(mysql_num_rows($res)==0) {
		echo "there is no data in table..";
	}
	else {
	echo'<form action="bugger.php" method="post"><select name="categories">';
		for($i=0;$i<mysql_num_rows($res);$i++) {
		$row=mysql_fetch_assoc($res);
		echo"<option>$row[race_name]</option>";
		} // endof for
	echo'</select><input type="submit" name="submit" value="race" />
	<input type="hidden" name="submitted" value="TRUE" /></form>';
	}
}
elseif ((isset($_GET['race']))) {
    	$getrace = $_GET['race'];
	$res=mysql_query("select member.first_name,member.last_name,race.race_name,race.race_date,results.time from member,race,result WHERE race.race_name='".$getrace."'");

	}
    
if(mysql_num_rows($res)==0) {
	echo "there is no data in table..";
}

else {
	//table header
	echo'<table align="center" cellspacing="10" cellpadding="5">
	<tr><td align="left"><b>First Name</b></td>
	<td align="left"><b>Last Name</b></td>
	<td align="left"><b>Race Name</b></td>
	<td align="left"><b>Race Date</b></td>
	<td align="left"><b>Race Time</b></td></tr>';
    	echo'</select>';
    }
// fetch and print all the records
while($row = mysql_fetch_array($res,MYSQL_ASSOC)) {
		echo'<tr><td align="left">'.$row['first_name'].'</td>
		<td align="left">'.$row['last_name'].'</td>
		<td align="left">'.$row['race_name'].'</td>
		<td align="left">'.$row['race_date'].'</td> 
		<td align="left">'.$row['time'].'</td>
		</tr>
		';}echo'</table>';

?>

 

called the page bugger.php cos i can't get it working ;)

Link to comment
Share on other sites

do something like this:

<?php
if($HTTP_GET_VARS)
{
//get the info from the url using $_GET
}
//that means tha
elseif($HTTP_POST_VARS)
{
//get the info from the server using $_POST
display the info with the tables
}
else
{
//some error message
}
?>

if you don't understand what i'm saying, please elaborate on the processing of this thing, the steps i mean, because i dont fully understand

Link to comment
Share on other sites

ah didnt see ur bit there.

 

well what im trying to do is (whether its done right or not, this is what the idea is!)

 

1) have a form with option box for race names.

2) check if the form has been submitted/ or that a race name has been selected and submit button pressed

3) if it has, display table below on same page with all the required data (thats where the long select query and table bits are)

4) if it hasn't (i.e. when the page loads), just display the drop down box by itself.

 

hope that makes sense :S

would i need to take the form out the first if and put it say at the bottom of the page on its own, then do the if bits above it?

Link to comment
Share on other sites

Hi...

 

Your form data posting method was POST and you are using GET method to retrieve the data.

field names are wrong.

 

try  this  updated code..

 

require_once('connection.php');

if (!isset($_POST['submit']))#IT should be $_POST bcoz form method is POST.

{

    $res=mysql_query("select race_name from race");

if(mysql_num_rows($res)==0) {

echo "there is no data in table..";

}

else

{

$row=mysql_fetch_array($res);

echo '<form action="bugger.php" method="post"><select name="race">'; # this is the vlaue after posting we are using to retrieve values from DB.

for($i=0;$i<mysql_num_rows($res);$i++)

{

echo"<option value = $row[race_name] >$row[race_name]</option>"; # Here you missed value part in options ;)

} // endof for

echo'</select><input type="submit" name="submit" value="submit" />

<input type="hidden" name="submitted" value="TRUE" /></form>';

}

}

elseif ((isset($_POST['submit']))) {

    $getrace = $_POST['race'];

$res=mysql_query("select member.first_name,member.last_name,race.race_name,race.race_date,results.time from member,race,result WHERE race.race_name='".$getrace."'");

 

}

   

if(mysql_num_rows($res)==0) {

echo "there is no data in table..";

}

 

else {

//table header

echo'<table align="center" cellspacing="10" cellpadding="5">

<tr><td align="left"><b>First Name</b></td>

<td align="left"><b>Last Name</b></td>

<td align="left"><b>Race Name</b></td>

<td align="left"><b>Race Date</b></td>

<td align="left"><b>Race Time</b></td></tr>';

    echo'</select>';

    }

// fetch and print all the records

while($row = mysql_fetch_array($res,MYSQL_ASSOC)) {

echo'<tr><td align="left">'.$row['first_name'].'</td>

<td align="left">'.$row['last_name'].'</td>

<td align="left">'.$row['race_name'].'</td>

<td align="left">'.$row['race_date'].'</td>

<td align="left">'.$row['time'].'</td>

</tr>

';}echo'</table>';

Link to comment
Share on other sites

$_GET is used to get info/watever you would call it from the url, eg. if the page url was: www.phpgreaks.com/index.php?name=thunder, then you would use this syntax to get it:

$name = $_GET['name'];

//dispays thuder
echo $name;

and post is posting info behind, where the user will not see the things to be processed

Link to comment
Share on other sites

Hi...

 

Your form data posting method was POST and you are using GET method to retrieve the data.

field names are wrong.

 

try  this  updated code..

 

require_once('connection.php');

if (!isset($_POST['submit']))#IT should be $_POST bcoz form method is POST.

{

    $res=mysql_query("select race_name from race");

if(mysql_num_rows($res)==0) {

echo "there is no data in table..";

}

else

{

$row=mysql_fetch_array($res);

echo '<form action="bugger.php" method="post"><select name="race">'; # this is the vlaue after posting we are using to retrieve values from DB.

for($i=0;$i<mysql_num_rows($res);$i++)

{

echo"<option value = $row[race_name] >$row[race_name]</option>"; # Here you missed value part in options ;)

} // endof for

echo'</select><input type="submit" name="submit" value="submit" />

<input type="hidden" name="submitted" value="TRUE" /></form>';

}

}

elseif ((isset($_POST['submit']))) {

    $getrace = $_POST['race'];

$res=mysql_query("select member.first_name,member.last_name,race.race_name,race.race_date,results.time from member,race,result WHERE race.race_name='".$getrace."'");

 

}

   

if(mysql_num_rows($res)==0) {

echo "there is no data in table..";

}

 

else {

//table header

echo'<table align="center" cellspacing="10" cellpadding="5">

<tr><td align="left"><b>First Name</b></td>

<td align="left"><b>Last Name</b></td>

<td align="left"><b>Race Name</b></td>

<td align="left"><b>Race Date</b></td>

<td align="left"><b>Race Time</b></td></tr>';

    echo'</select>';

    }

// fetch and print all the records

while($row = mysql_fetch_array($res,MYSQL_ASSOC)) {

echo'<tr><td align="left">'.$row['first_name'].'</td>

<td align="left">'.$row['last_name'].'</td>

<td align="left">'.$row['race_name'].'</td>

<td align="left">'.$row['race_date'].'</td>

<td align="left">'.$row['time'].'</td>

</tr>

';}echo'</table>';

 

 

thanks for that, tried that, the drop down now just has one race name in it repeated 6 times, and the table has got that same race name printed on 6 rows, and before that there loads of php errors saying Notice: Undefined index: last_name in c:\program files\easyphp1-8\www\test2.php on line 44 and so on, basically for every single line in this bit:

 

         echo'<tr><td align="left">'.$row['first_name'].'</td>
         <td align="left">'.$row['last_name'].'</td>
         <td align="left">'.$row['race_name'].'</td>
         <td align="left">'.$row['race_date'].'</td> 
         <td align="left">'.$row['time'].'</td>

 

im guessing ive reffered to my fields wrong or something? cos theyre from different tables do i need to take that into account here?

 

thanks for help:)

Link to comment
Share on other sites

 

 

Sorry! here is the mistake..

 

else

      {

        $row=mysql_fetch_array($res);

        echo '<form action="bugger.php" method="post"><select name="race">'; # this is the vlaue after posting we are using to retrieve values from DB.

        for($i=0;$i<mysql_num_rows($res);$i++)

        {

            echo"<option value = $row[race_name] >$row[race_name]</option>"; # Here you missed value part in options Wink

        } // endof for

        echo'</select><input type="submit" name="submit" value="submit" />

        <input type="hidden" name="submitted" value="TRUE" /></form>';

      }

 

 

 

Replace this code with this

 

 

 

else

      {

       

 

        echo '<form action="bugger.php" method="post"><select name="race">'; # this is the vlaue after posting we are using to retrieve values from DB.

        while ($row=mysql_fetch_array($res));

        {

            echo"<option value = $row[race_name] >$row[race_name]</option>"; # Here you missed value part in options Wink

        } // endof for

        echo'</select><input type="submit" name="submit" value="submit" />

        <input type="hidden" name="submitted" value="TRUE" /></form>';

      }

 

 

Let me know if still you are getting errors with this..

 

Link to comment
Share on other sites

hi, yea still got problems, tried what you saidbut that didnt work either, drop down was empty. atm have the drop down populated correctly, but if u select anything, it gives erorrs

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:\program files\easyphp1-8\www\test3.php on line 25

there is no data in table..

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\program files\easyphp1-8\www\test3.php on line 40

 

 

heres all my code with lines 25 and 40 highlighted bold ,

 

<?php
require_once('connection.php');
if (!isset($_POST['submit']))#IT should be $_POST bcoz form method is POST.
{
       $res=mysql_query("select race_name from race");
      if(mysql_num_rows($res)==0) {
         echo "there is no data in table..";
      }
      else
      {
         echo '<form action="test3.php" method="post"><select name="race">'; # this is the vlaue after posting we are using to retrieve values from DB.
         while($row = mysql_fetch_array($res,MYSQL_ASSOC)) {
            echo"<option value = $row[race_name] >$row[race_name]</option>"; # Here you missed value part in options 
         } // endof while
         echo'</select><input type="submit" name="submit" value="submit" />
         <input type="hidden" name="submitted" value="TRUE" /></form>';
      }
}
elseif ((isset($_POST['submit']))) {
       $getrace = $_POST['race'];
      $res=mysql_query("select member.first_name,member.last_name,race.race_name,race.race_date,results.time from member,race,result WHERE race.race_name='".$getrace."'");

      }
    
   [b]if(mysql_num_rows($res)==0) {[/b]
      echo "there is no data in table..";
   }
   
   else {
      //table header
      echo'<table align="center" cellspacing="10" cellpadding="5">
      <tr><td align="left">First Name</td>
      <td align="left">Last Name</td>
      <td align="left">Race Name</td>
      <td align="left">Race Date</td>
      <td align="left">Race Time</td></tr>';
       echo'</select>';
    }
   // fetch and print all the records
   [b]while($row = mysql_fetch_array($res,MYSQL_ASSOC)) {[/b]
         echo'<tr><td align="left">'.$row['first_name'].'</td>
         <td align="left">'.$row['last_name'].'</td>
         <td align="left">'.$row['race_name'].'</td>
         <td align="left">'.$row['race_date'].'</td> 
         <td align="left">'.$row['time'].'</td>
         </tr>
         ';}echo'</table>';
?>

 

 

any ideas? also the table headings are showing on the page before selecting anything and submitting from the box, they shouldnt be should they?

Link to comment
Share on other sites

***SOLVED***

***SOLVED***

***SOLVED***

 

HI i have a similar problem heres my coding of my different pages.

 

<html>
<body>
<form name="form" id="form" action="set.php" method="post">
Title: 
<select name="ts" id="ts" multiple>
<option> </option>
<option value="ABC">ABC</option>
<option value="DEF">DEF</option>
</select>
<INPUT type="submit" value="Submit"> 
</form>
</body>
</html>

 

Code for set.php

<?PHP


$abc= $_POST["ts"];
// will p$post input boxes but not drop down menus
echo $abc;

?>

 

any help would be great.

Thanks in advance

Saj

 

 

***SOLVED***

***SOLVED***

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