Stu Parker Stu Parker - 6 months ago 7
PHP Question

How do i submit mulitple forms, using the same action, using a single button

So, I have looked through the forum, and not found a case similar to mine on this topic.

I am trying to create a form that gives 9 options, each option with 2 choices, which give a total of 18 possibilities. The final 9 options, I would like to then submit, via post method, to a php file, which will then use that information to update an sql database.

I have got to this point in my code, and have not been able to find, or bring together the elements I need to make it work.

<table border="2">
<tr> <td colspan="2" align="center">Game 1</td></tr>
<form> <tr height="40px"><td><input type="radio" name="Game1" value="Home"/> Home </td><td><input type="radio" name="Game1" value="Away" /> Away</td></tr></form>
<tr> <td colspan="2" align="center">Game 2</td></tr>
<form><tr height="40px"><td><input type="radio" name="Game2" value="Home" /> Home</td><td><input type="radio" name="Game2" value="Away" /> Away </td></tr></form>


Game 3
Home Away
Game 4
Home Away
Game 5
Home Away
Game 6
Home Away
Game 7
Over Under
Game 8
Over Under
Game 9
Over Under

</table>
<input type="button" value="Muli-submit" onclick="$('.form').submit()" />


Now I am not sure, that this is the correct functionality, so the idea is a one button the submits each of the 9 forms, which will be values for each of the games to something like this:

<form action="update9.php" method="post">


Thus creating a list of values that will be used in the php as such:

<?php
$Member=$_POST["member"];
$Game1=$_POST["Game1"];
$Game2=$_POST["Game2"];
$Game3=$_POST["Game3"];
$Game4=$_POST["Game4"];
$Game5=$_POST["Game5"];
$Game6=$_POST["Game6"];
$Game7=$_POST["Game7"];
$Game8=$_POST["Game8"];
$Game9=$_POST["Game9"];





// Connect to database server
mysql_connect("efootyonline.ipagemysql.com", "user", Password") or die (mysql_error ());

// Select database
mysql_select_db("web_footy1") or die(mysql_error());

// The SQL statement is built
$strSQL = "UPDATE Round_9 SET ";
$strSQL = $strSQL . "Game1= '$Game1', ";
$strSQL = $strSQL . "Game2= '$Game2', ";
$strSQL = $strSQL . "Game3= '$Game3', ";
$strSQL = $strSQL . "Game4= '$Game4', ";
$strSQL = $strSQL . "Game5= '$Game5', ";
$strSQL = $strSQL . "Game6= '$Game6', ";
$strSQL = $strSQL . "Game7= '$Game7', ";
$strSQL = $strSQL . "Game8= '$Game8', ";
$strSQL = $strSQL . "Game9= '$Game9' ";
$strSQL = $strSQL . "WHERE Member = '$Member' ";

// The SQL statement is executed
mysql_query($strSQL) or die(mysql_error()) ;

// Close the database connection
mysql_close();

?>


The database is updated!






Thanks in advance. I am self taught, about 2 months into developing and refine this site as a work in progress.

Answer

you don't need to have separate forms. Just use 1 form..!

<table border="2">
    <form>
        <tr>
            <td colspan="2" align="center">Game 1</td>
        </tr>

        <tr height="40px">
            <td><input type="radio" name="Game1" value="Home"/> Home</td>
            <td><input type="radio" name="Game1" value="Away"/> Away</td>
        </tr>
        <tr>
            <td colspan="2" align="center">Game 2</td>
        </tr>
        <tr height="40px">
            <td><input type="radio" name="Game2" value="Home"/> Home</td>
            <td><input type="radio" name="Game2" value="Away"/> Away</td>
        </tr>
    </form>
</table>

Also, it will probably be easier to work with the info if you load the values for each game into an array rather than having separate variables.

You can do this by naming the elements and specifying the index in brackets Game[1].

So using:

<table border="2">
    <form method="POST" action="http://eqtracker.com/testing.php">
        <tr>
            <td colspan="2" align="center">Game 1</td>
        </tr>

        <tr height="40px">
            <td><input type="radio" name="Game[1]" value="Home"/> Home</td>
            <td><input type="radio" name="Game[1]" value="Away"/> Away</td>
        </tr>
        <tr>
            <td colspan="2" align="center">Game 2</td>
        </tr>
        <tr height="40px">
            <td><input type="radio" name="Game[2]" value="Home"/> Home</td>
            <td><input type="radio" name="Game[2]" value="Away"/> Away</td>
        </tr>
        <tr>
            <td colspan="2" align="center">Game 3</td>
        </tr>
        <tr height="40px">
            <td><input type="radio" name="Game[3]" value="Home"/> Home</td>
            <td><input type="radio" name="Game[3]" value="Away"/> Away</td>
        </tr>
        <button type="submit">Submit the Info</button>
    </form>
</table>

Would get you a single array called Game loaded up with all the values.

array(1) {
  ["Game"]=>
  array(3) {
    [1]=>
    string(4) "Home"
    [2]=>
    string(4) "Away"
    [3]=>
    string(4) "Home"
  }
}