SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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:

    Code:
    here's my code:
    
    
    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>';
    
    ?>
    i called the page bugger.php cos its just slightly annoying me! thanks for any help

  2. #2
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you have a racist problem, huh !

    Your form method is post, and you are using get method to fetch variables.

    First, why you are using this
    PHP Code:
    <input type="hidden" name="submitted" value="TRUE" /> 
    &#168;Probably, this is a bit of extra code you don't need.

    Then, change
    PHP Code:
    elseif ((isset($_GET['race']))) {
            
    $getrace $_GET['race']; 
    to
    PHP Code:
    elseif ((isset($_POST['race']))) {
            
    $getrace $_POST['race']; 
    and see what happens.

  3. #3
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your form's method is "post", so logically you'll have to retrieve any variables submitted via the $_POST array, not the $_GET array; i.e.:
    PHP Code:
    if(!isset($_POST['race'])){
    //etc

    Edit:


    Beaten to it...

  4. #4
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do u get this div with brown 1px border mudshark?

  5. #5
    SitePoint Addict khuramyz's Avatar
    Join Date
    Oct 2005
    Location
    Manchester, UK
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mudshark posted the reply and then edited it.

  6. #6
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, but by editing we don't get the div. There is a special bbcode for that I guess. People are also using this div to speak about offtopics.

    Edit:

    Is that so easy?

  7. #7
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    put "edit" and "/edit" inbetween square brackets and viola!

  8. #8
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks mudshark ... it works

    [offtopic]Offtopic also works in the same way?[/offtopic]

  9. #9
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kigoobe View Post
    So you have a racist problem, huh !

    Your form method is post, and you are using get method to fetch variables.

    First, why you are using this
    PHP Code:
    <input type="hidden" name="submitted" value="TRUE" /> 
    ¨Probably, this is a bit of extra code you don't need.

    Then, change
    PHP Code:
    elseif ((isset($_GET['race']))) {
            
    $getrace $_GET['race']; 
    to
    PHP Code:
    elseif ((isset($_POST['race']))) {
            
    $getrace $_POST['race']; 
    and see what happens.

    not sure what u meant by racist problem, that was not my intention :P

    anyway thanks for ur replies people and mudshark's

    welll i've now got the following code,

    Code:
    <?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
          {
             $row=mysql_fetch_array($res);
             echo '<form action="test2.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">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
       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>';
    ?>
    however the lines in bold seem to be causing a prob. the drop down list now shows 6of the same name instead of all the different ones, but my table has the race names listed correctly (weird!) , and u cant change from the drop down as theyre all the same and nothing changes clicking submit. before the table is displayed got lots of undefined index errors relating to the lines in bold above, e.g.

    Notice: Undefined index: last_name in c:\program files\easyphp1-8\www\test2.php on line 44

    etc , they repeat themselves, but the only line in the bold section above that it DOES NOT show this error for is the race_name one. what the hell is going on?! :O

  10. #10
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you use a while loop to get the select options, as you have done later? See what happens then.

    PHP Code:
    while($row=mysql_fetch_array($res)) {
    print 
    "<option value = '".$row[race_name]."' >".$row[race_name]."</option>";

    Better copy and paste from here ...

  11. #11
    SitePoint Enthusiast csjc2662's Avatar
    Join Date
    Feb 2007
    Location
    Texas, USA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Edit:


    Well I got beat to it any way and accidentally hit submit. Oh yeah ".$this." is better. I'm getting sleepy

    PHP Code:
    echo '<form action="test2.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 
             
    // endof for
             
    echo'</select><input type="submit" name="submit" value="submit" />
             <input type="hidden" name="submitted" value="TRUE" /></form>'
    ;
          } 
    James
    New links coming soon....

  12. #12
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well ive got this now;

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

    did before you edited - should i change echo to print then?

    got a bit further - the drop down now has the correct names in, whoop! but, when you choose any of them and hit submit, got a different complaint..

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


    (this refers to the second time (further down) that there is the line
    Code:
    if(mysql_num_rows($res)==0) {

    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

    (this refers to the line
    Code:
    while($row = mysql_fetch_array($res,MYSQL_ASSOC)) {
    , again the second while loop where trying to fetch all of the records to print them in table).


    thanks guys for your help so far!

  13. #13
    SitePoint Enthusiast csjc2662's Avatar
    Join Date
    Feb 2007
    Location
    Texas, USA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heres an example that works, as long as I haven't hit a button or anything on accident. LOL

    PHP Code:
    function wto_printSites()
     {
      
    //open db here or before
      
    $wto_Query "SELECT * FROM UsrSites ORDER BY sitename ASC";
      
    $wto_Queryresult mysql_query($wto_Query) or die ("<font face=verdana>Error: ".mysql_error());
      while (
    $Row mysql_fetch_array($wto_Queryresult))
       {
        
    $Sitename $Row["sitename"];
        
    $Siteurl $Row["siteurl"];
        
    $Buttonurl $Row["buttonurl"];
        
    $Inhits $Row["inhits"];
        
    $Out $Row["out"];
        
    $Editsitename str_replace(" ""+"$Sitename);
        
    $Listsites = ("<tr class='ListLink_Style'>");
        
    $Listsites .= ("<td>$Sitename</TD>");
        
    $Listsites .= ("<td><A HREF='$Siteurl' TARGET='_BLANK'>$Siteurl</A></TD>");
        
    $Listsites .= ("<td>$Buttonurl</TD>");
        
    $Listsites .= ("<td style='text-align: center'>$Inhits</TD>");
        
    $Listsites .= ("<td style='text-align: center'>$Out</TD>");
        
    $Listsites .= ("</TR>");
        PRINT 
    $Listsites;
       }
      
    mysql_close();
     } 
    Last edited by csjc2662; Apr 10, 2007 at 04:23. Reason: only three edits and fell asleep twice
    James
    New links coming soon....

  14. #14
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay, well i tried echoing out the contents of the $getrace variable. all my race names are made up of two words e.g. london marathon, nottingham half etc etc, and it seems the variable is only taking in the first word e.g. london, nottingham and not the second word which would explain why it isnt finding any data in the query.

    why is it chopping off the second word in the race? is it cos theres a space or something? how can i get round this?

  15. #15
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thunderstorm, u didn't read my last post, did u?

  16. #16
    SitePoint Enthusiast csjc2662's Avatar
    Join Date
    Feb 2007
    Location
    Texas, USA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    print "<option value = '".$row[race_name]."' >".$row[race_name]."</option>"
    its the quotes 'and' the '". $operator ."' that helps
    James
    New links coming soon....

  17. #17
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kigoobe View Post
    thunderstorm, u didn't read my last post, did u?
    lol, okay, now its printing out correctly with both words of the race names but i still have the same errors

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:\program files\easyphp1-8\www\test3.php on line 26
    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 41


    for the same lines as i said earlier, theyre only one on cos i added the bit to print out contents of the variable

    this is really getting annoying, what exactly do those errors mean? is my query wrong or something if its not fetching any data from the table?

    thanks everyone again, very helpful forum here

  18. #18
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think there's a flaw in your if/elseif logic. See if this does the job (untested obviously):
    PHP Code:
    <?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="test2.php" method="post"><select name="race">'
             while(
    $row mysql_fetch_array($res)) 
             {
                echo 
    "<option value=".$row['race_name'].">".$row['race_name']."</option>";
             } 
    // endof for
             
    echo'</select><input type="submit" name="submit" value="submit" />
             <input type="hidden" name="submitted" value="TRUE" /></form>'
    ;
          }
    }
    else{
          
    $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">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>'
    ;
       
    // fetch and print all the records
       
    while($row mysql_fetch_array($res)) {
                   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>';
       }
    }
    ?>

  19. #19
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    They both means there's some problem in the query. Do one thing. Print the query on your page, and try to figure out where's the problem. If that doesn't help, paste that in your phpmyadmin and see what you are getting there.

    Before, try changing these lines
    PHP Code:
    while($row mysql_fetch_array($res,MYSQL_ASSOC)) { 
    to
    PHP Code:
    while($row mysql_fetch_array($res)) { 
    and see what happens.

    Edit:

    yes, u do have a problem in ur if / else if logic


    Also, I did a mistake. This line
    PHP Code:
    print "<option value = '".$row[race_name]."' >".$row[race_name]."</option>"
    has an error. Try changing that to
    PHP Code:
    print '<option value = "'.$row['race_name'].'" >'.$row['race_name'].'</option>'
    I didn't notice before, sorry about that.

  20. #20
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wooooft finally its all working! thanks to your suggestions i had a problem with my query as it was repeating the data loads, so i stuck in cross checking the id numbers and it works now.. only thing is of course i still have questions :P

    is there any way i can still display the drop down box above the table? at the moment it just disppears on hitting submit and isnt displayed again.

    other thing is i realised a slight niggle, that two of the races have the same names, but have different lengths- so it looks like they appear twice but its actually a seperate race
    not sure if its getting to technical but could i possibly print the race length next to the race name in the drop down (!!!) such as 'Forest 8 - 5 miles' and then 'Forest 8 - 10 miles' for the other one? race_length is also stored in the race table too.

    heres the current working code (very excited lol!)

    Code:
    <?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="race_results.php" method="post"><select name="race">'; 
             while($row = mysql_fetch_array($res)) 
             {
                print '<option value = "'.$row['race_name'].'" >'.$row['race_name'].'</option>'; 
             } // endof for
             echo'</select><input type="submit" name="submit" value="submit" />
             <input type="hidden" name="submitted" value="TRUE" /></form>';
          }
    }
    else{
          $getrace = $_POST['race'];
          $res = mysql_query("select member.first_name,member.last_name,race.race_name,race.race_date,results.time from member,race,results WHERE member.member_no = results.member_no AND race.race_no = results.race_no AND 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">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>';
       // fetch and print all the records
       while($row = mysql_fetch_array($res)) {
                   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 everybody, again


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •