Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP Self Submitting Form

  • 30-03-2009 12:20pm
    #1
    Registered Users, Registered Users 2 Posts: 139 ✭✭


    Hi there,

    I am having a problem with a PHP self-submitting form which I can't find a solution for. The setup is basically this:
    I have a form which INSERTs player details/scores to a table on mysql database for a given match. When I get to the form in the first instance, it knows the id because it's in the URL, e.g. /match_team.php?id=21, however, whenever I then submit the form, it reloads empty, but the id is lost.

    This is my code:
    <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    <input type="hidden" name="match" size="3" value="<? echo $_GET['id']; ?>">
    <?php
    #Grabs all active players and list them in a drop-menu
    	include("cnn/connect.php");
    
    	$sql = "SELECT * FROM players ORDER BY surname, forename";
    
    	$result = mysql_query($sql);
    
    	echo "<select name=player><option value=0>Select Player</option>";
    	while ($row = mysql_fetch_assoc($result)) {
    		$name = $row[forename] . " " . $row[surname];
    		echo "<option value=$row[id]>$name</option>";
    	}
    	echo "</select>";
    	mysql_free_result($result);
    
    	echo "<select name=number>";
    	echo " <option value=0>#</option>";
    	 for ( $jersey = 1; $jersey <= 25; $jersey += 1) {
    		echo " <option value=".$jersey.">".$jersey."</option>";
    	}
    	echo "</select>";	
     ?>
    <input type="text" name="goals" size="3">
    <input type="text" name="points" size="3">
    <br>
    <input type="submit" name="submit" value="Submit">
    </form>
    
    <!-- THE PHP PART -->
     <?  
    $matchid = $_GET['id'];
    include("cnn/connect.php");
    
    if(isset($_POST['submit'])) { 
    	$matchid = $_POST['match'];
        $playerid = $_POST['player']; 
            $number = $_POST['number']; 
            $goals = $_POST['goals']; 
    		$points = $_POST['points'];
    
    	$sql = "INSERT INTO players_match (id, matchid, playerid, number, goals, points) VALUES (NULL, $matchid, $playerid, $number, $goals, $points)";     
    	mysql_query($sql) or die(mysql_error());  
    }
    
    #Show uptodate list of players added to match id
    
    echo "<table width=\"510\"  cellspacing=\"3\" cellpadding=\"0\">\n";
    
    include("cnn/connect.php"); 
    
    # Send SQL query
    $sql = "SELECT players_match.id As pmid, number, forename, surname, goals, points FROM players_match JOIN players ON players.id = players_match.playerid WHERE matchid=$matchid ORDER BY number";
    $result = mysql_query($sql) or die(mysql_error());
    
    # Fetch table rows, one by one, and display as HTML rows
    while ($row = mysql_fetch_assoc($result)) {
    echo "<tr align=\"left\" valign=\"middle\" bgcolor=\"#F5F5F5\">\n";
    echo "    <td>", $row['number'], "</td>\n";
    echo "    <td>", $row['forename'], " ", $row['surname'], "</td>\n";
    echo "    <td>", $row['goals'], "</td>\n";  
    echo "    <td>", $row['points'], "</td>\n";
    echo "	  <td><a href=\"delete.php?id=", $row['pmid'], "\">Delete</a></td></tr>\n";
    }
    echo "</table>\n";
    ?>
    

    The form is working just fine, i.e. it does exactly as required (for the first entry), however, the second and subsequent players cannot be added because the hidden form field called match cannot get a value from the url.

    Is there any way to get the self-submitting form to retain the id value for the next entry???

    Please and thanks :confused:

    BOS


Comments

  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    <?php
    echo "<input type=\"hidden\" name=\"match\" size=\"3\" value=\"";
    if(isset($_GET['id']) {
           echo $_GET['id];
    }
    else {
           echo $_GET['match'];
    }
    echo "\" />";
    
    #Grabs all active players and list them in a drop-menu
    	include("cnn/connect.php");
    
    	$sql = "SELECT * FROM players ORDER BY surname, forename";
    
    	$result = mysql_query($sql);
    
    	echo "<select name=player><option value=0>Select Player</option>";
    	while ($row = mysql_fetch_assoc($result)) {
    		$name = $row[forename] . " " . $row[surname];
    		echo "<option value=$row[id]>$name</option>";
    	}
    	echo "</select>";
    	mysql_free_result($result);
    
    	echo "<select name=number>";
    	echo " <option value=0>#</option>";
    	 for ( $jersey = 1; $jersey <= 25; $jersey += 1) {
    		echo " <option value=".$jersey.">".$jersey."</option>";
    	}
    	echo "</select>";	
     ?>
    <input type="text" name="goals" size="3">
    <input type="text" name="points" size="3">
    <br>
    <input type="submit" name="submit" value="Submit">
    </form>
    
    <!-- THE PHP PART -->
     <?  
    $matchid = $_GET['id'];
    include("cnn/connect.php");
    
    if(isset($_POST['submit'])) { 
    	$matchid = $_POST['match'];
        $playerid = $_POST['player']; 
            $number = $_POST['number']; 
            $goals = $_POST['goals']; 
    		$points = $_POST['points'];
    
    	$sql = "INSERT INTO players_match (id, matchid, playerid, number, goals, points) VALUES (NULL, $matchid, $playerid, $number, $goals, $points)";     
    	mysql_query($sql) or die(mysql_error());  
    }
    
    #Show uptodate list of players added to match id
    
    echo "<table width=\"510\"  cellspacing=\"3\" cellpadding=\"0\">\n";
    
    include("cnn/connect.php"); 
    
    # Send SQL query
    $sql = "SELECT players_match.id As pmid, number, forename, surname, goals, points FROM players_match JOIN players ON players.id = players_match.playerid WHERE matchid=$matchid ORDER BY number";
    $result = mysql_query($sql) or die(mysql_error());
    
    # Fetch table rows, one by one, and display as HTML rows
    while ($row = mysql_fetch_assoc($result)) {
    echo "<tr align=\"left\" valign=\"middle\" bgcolor=\"#F5F5F5\">\n";
    echo "    <td>", $row['number'], "</td>\n";
    echo "    <td>", $row['forename'], " ", $row['surname'], "</td>\n";
    echo "    <td>", $row['goals'], "</td>\n";  
    echo "    <td>", $row['points'], "</td>\n";
    echo "	  <td><a href=\"delete.php?id=", $row['pmid'], "\">Delete</a></td></tr>\n";
    }
    echo "</table>\n";
    ?>
    


  • Registered Users, Registered Users 2 Posts: 139 ✭✭Bald? er, dash!


    Thanks for the speedy reply...

    ... unfortunately it doesn't do the trick. $_GET does not have any value once the form reloads...


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Well since it is in a hidden field in a form that uses the POST method, it will be in the $_POST array:

    $id = $_POST;


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    Jeez thanks webmonkey - I shoulda spotted that one :P


  • Registered Users, Registered Users 2 Posts: 139 ✭✭Bald? er, dash!


    Winner!

    Thanks for your helps peoples... :)


  • Advertisement
Advertisement