Complex Form Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SHOverine
    New Member
    • Sep 2006
    • 18

    Complex Form Question

    I have a 3-part form that I am having trouble with. First part is to select the user group and the week and year that I want to submit results for, this calls the elements that I want to update.

    The second part is to enter the results and submit them to a MySQL table called results the final part echoes the results.

    The issue is that my form writes the elements that I called up to the this table before I enter the results and click the submit button. I attached a simplified version of my code below:


    Thanks for your help
    Cheers,
    Seth
    _______________ _______________ _______________ _______________ _
    FORM 1:
    <?php

    include("php/common.php");
    include("php/db.php");
    session_start() ;
    // RESULTS1.PHP
    // DISPLAY FORM 1 - GET USER GROUP AND GAME INFORMATION
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <meta http-equiv="content-type"content="t ext/html; charset=ISO-8859-1">
    <html>
    <head><title>Th e Weekly 13: Results Manager</title></head>
    <body style="backgrou nd-image: url

    (http://theweekly13.com/images/core/pabstheaderbg.j pg);">
    <h1 style="text-align: center; font-family: Arial;"><font color="#000099" >
    The Weekly 13: Game Results Manager</font></h1>

    <form style="font-family: Arial;" method="POST" name="results1. php"
    action="results 2.php">

    <table style="text-align: left; width: 35%;" border="0" cellpadding="0"

    cellspacing="0" >
    <tbody>
    <tr>
    <td style="width: 83px;" colspan="1" rowspan="3">
    <img style="width: 80px; height: 83px;"alt=""

    src="http://theweekly13.com/images/core/pabstheader.jpg ">
    </td>
    <td style="text-align: right; width: 25%; font-weight: bold;">Season:
    </td>
    <td align="undefine d" valign="undefin ed"><input size="5" name="Year"

    value="2006">
    </td>
    </tr>
    <tr>
    <td style="text-align: right; font-weight:

    bold;"valign="u ndefined">Leagu e:
    </td>
    <td>

    <?php // GET LEAGUE
    include_once 'php/db.php';
    dbConnect("thew eekl_test");
    $query = mysql_query("SE LECT League, LeagueNumber FROM Leagues");
    echo "<form action=somethin g.php method=POST> <select name=League>";
    while ($r = mysql_fetch_arr ay($query))
    { $League = $r["League"]; $LeagueNumber = $r["LeagueNumb er"];
    echo "<option value='$LeagueN umber'>$League</option>";
    } echo "</select>"; ?>
    </td>
    </tr>
    <tr>
    <td style="text-align: right; font-weight: bold;">Week:
    </td>
    <td>

    <?php // GET WEEK
    include_once 'php/db.php';
    dbConnect("thew eekl_test");
    $query = mysql_query("SE LECT WeekName, WeekID FROM Weeks");
    echo "<form action=somethin g.php method=POST>
    <select name=Week>";
    while ($r = mysql_fetch_arr ay($query))
    { $Week = $r["WeekName"]; $WeekID = $r["WeekID"];
    echo "<option value='$WeekID' >$Week</option>";
    } echo "</select>";
    ?>
    </td>
    </tr>
    <tr>
    <td colspan="3" rowspan="1" align="undefine d" valign="undefin ed">
    <hr noshade="noshad e">
    <div style="text-align: right;"><input value="Reset Form"

    type="reset">
    <input name="submitok" value=" OK " type="submit">
    </div>
    </td>
    </tr>
    </tbody>
    </table>
    </form>
    </body>
    </html>


    _______________ _______________ _______________ _______________ __
    FORM 2:

    <?php
    include("php/common.php");
    include("php/db.php");
    if (!isset($_POST['submitok2'])) {
    //RESULTS2.PHP
    // DISPLAY FORM 2 - GET GAME INFORMATION, POPULATE FORM 2
    ?>

    <title>Weekly 13: Game Results Manager</title>
    <meta http-equiv="content-type"
    content="text/html; charset=ISO-8859-1">
    <body style="backgrou nd-image: url

    (http://theweekly13.com/images/core/pabstheaderbg.j pg);">
    <h1 style="text-align: center; font-family: Arial;"><font
    color="#000099" >The Weekly 13: Game Results Manager</font></h1>
    <br>

    <form method="post" action="results 3.php"
    name="results2. php">
    <input type="hidden" name="Year" value="<?=$Year ?>">
    <input type="hidden" name="Week" value="<?=$Week ?>">
    <input type="hidden" name="League" value="<?=$Leag ue?>">
    <table style="text-align: left; width: 35%;" border="0"
    cellpadding="0" cellspacing="0" >
    <tbody>
    <tr>
    <td style="width: 83px;" colspan="1"
    rowspan="3"><im g style="width: 80px; height: 83px;"
    alt="" src="http://theweekly13.com/images/core/pabstheader.jpg "></td>
    <td style="text-align: right; width: 25%; font-weight: bold;">Season:
    </td>
    <td align="undefine d" valign="undefin ed">
    <?php echo $Year; ?>
    </td>
    </tr>
    <tr>
    <td style="text-align: right; font-weight: bold;"
    valign="undefin ed">League:</td>
    <td>
    <?php
    include_once 'php/db.php';
    dbConnect('thew eekl_test');
    $sqlA = "SELECT League FROM Leagues WHERE LeagueNumber = $League";
    $queryA = mysql_query($sq lA);
    $LeagueName = mysql_result($q ueryA, 0);
    echo $LeagueName; ?>
    </td>
    </tr>
    <tr>
    <td style="text-align: right; font-weight: bold;">Week:
    </td>
    <td>
    <?php echo $Week; ?>
    </td>
    </tr>
    </tbody>
    </table>
    <hr style="width: 100%; height: 2px;"><font
    color="#000099" >
    <table style="text-align: left; width: 100%;" border="0"
    cellpadding="0" cellspacing="0" >
    <tbody>
    <tr>
    <td
    style="text-align: center; width: 10%; font-weight: bold; font-family:

    Arial;">GameID</td>
    <td
    style="text-align: center; width: 25%; font-weight: bold; font-family:

    Arial;">Away
    Team (line)</td>
    <td
    style="text-align: center; width: 7%; font-weight: bold; font-family:

    Arial;">Away
    Score</td>
    <td
    style="text-align: center; width: 25%; font-weight: bold; font-family:

    Arial;">Home
    Team (line)</td>
    <td
    style="text-align: center; width: 8%; font-weight: bold; font-family:

    Arial;">Home
    <br>
    Score</td>
    </tr>
    <tr>
    <td style="text-align: center;" valign="undefin ed">
    <?php
    include_once 'php/db.php';
    dbConnect('thew eekl_test');
    $sql1 = "SELECT GameID FROM Games WHERE League = $League AND WeekID = $Week

    and Game = '1'";
    $query1 = mysql_query($sq l1);
    $Game1 = mysql_result($q uery1, 0);
    echo $Game1;
    ?></td>
    <td style="text-align: center;" valign="undefin ed">
    <?php
    dbConnect('thew eekl_test');
    include_once 'php/db.php';
    $sql1A = "SELECT AwayTeam FROM Games WHERE League = $League AND WeekID =

    $Week and Game = '1'";
    $query1A = mysql_query($sq l1A);
    $AwayTeam1 = mysql_result($q uery1A, 0);
    echo $AwayTeam1;
    ?>&nbsp;
    <?php
    include_once 'php/db.php';
    dbConnect('thew eekl_test');
    $sql1AA = "SELECT AwayLine FROM Games WHERE League = $League AND WeekID =

    $Week and Game = '1'";
    $query1AA = mysql_query($sq l1AA);
    $AwayLine1 = mysql_result($q uery1AA, 0);
    echo $AwayLine1;
    ?>
    </td>
    <td style="text-align: center;" valign="undefin ed"><input
    size="4" name="AwayScore 1"></td>
    <td style="text-align: center;" valign="undefin ed">
    <?php
    include_once 'php/db.php';
    dbConnect('thew eekl_test');
    $sql1B = "SELECT HomeTeam FROM Games WHERE League = $League AND WeekID =

    $Week and Game = '1'";
    $query1B = mysql_query($sq l1B);
    $HomeTeam1 = mysql_result($q uery1B, 0);
    echo $HomeTeam1;
    ?>&nbsp;
    <?php
    include_once 'php/db.php';
    dbConnect('thew eekl_test');
    $sql1BB = "SELECT HomeLine FROM Games WHERE League = $League AND WeekID =

    $Week and Game = '1'";
    $query1BB = mysql_query($sq l1BB);
    $HomeLine1 = mysql_result($q uery1BB, 0);
    echo $HomeLine1;
    ?>
    </td>
    <td style="text-align: center;"><input size="4"
    name="HomeScore 1"></td>
    </tr>
    <tr>
    <td colspan="6" rowspan="1" align="undefine d" valign="undefin ed">
    <hr noshade="noshad e">
    <div style="text-align: right;"><input value="Reset Form"

    type="reset">
    <input name="submitok2 " value=" OK " type="submit">
    </div>
    </td>
    </tr>
    </tbody>
    </table>
    </font>
    </form>
    <?php } ?>
    </body>
    </html>

    <?php

    include_once 'php/db.php';
    dbConnect('thew eekl_test');
    //PROCESS GAME RESULTS

    $sql1 = "INSERT INTO Results (ResultID, GameID, AwayTeam, HomeTeam,

    AwayScore, HomeScore, Cover) VALUES ('', '$Game1', '$AwayTeam1',

    '$HomeTeam1', '$AwayScore1', '$HomeScore1', '')";
    mysql_query($sq l1) or die(mysql_error ());
    ?>
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    My guess is that the 3rd part of your form, i.e.
    [PHP]<?php

    include_once 'php/db.php';
    dbConnect('thew eekl_test');
    //PROCESS GAME RESULTS

    $sql1 = "INSERT INTO Results (ResultID, GameID, AwayTeam, HomeTeam,

    AwayScore, HomeScore, Cover) VALUES ('', '$Game1', '$AwayTeam1',

    '$HomeTeam1', '$AwayScore1', '$HomeScore1', '')";
    mysql_query($sq l1) or die(mysql_error ());
    ?>
    [/PHP]
    is not a separate script but is stuck at the end of your second script Result2.php. That means that, whatever you do in your second script will always be followed by an update to the MySql database.

    Ronald :cool:

    Comment

    • SHOverine
      New Member
      • Sep 2006
      • 18

      #3
      Originally posted by ronverdonk
      My guess is that the 3rd part of your form, i.e.
      [PHP]<?php

      include_once 'php/db.php';
      dbConnect('thew eekl_test');
      //PROCESS GAME RESULTS

      $sql1 = "INSERT INTO Results (ResultID, GameID, AwayTeam, HomeTeam,

      AwayScore, HomeScore, Cover) VALUES ('', '$Game1', '$AwayTeam1',

      '$HomeTeam1', '$AwayScore1', '$HomeScore1', '')";
      mysql_query($sq l1) or die(mysql_error ());
      ?>
      [/PHP]
      is not a separate script but is stuck at the end of your second script Result2.php. That means that, whatever you do in your second script will always be followed by an update to the MySql database.

      Ronald :cool:

      This is what I desire - "whatever you do in your second script will always be followed by an update to the MySql database."

      But what I do in the first First form gets written in my table - it writes the row before I enter values into the "AwayScore" and "HomeScore" fields. I verified this by checking the table right after I hit the 1st submit ("submitok") . My code writes a row that has the correct ResultID, GameID, AwayTeam, and HomeTeam and writes Zeros for the AwayScore and HomeScore before I click the second submit button.

      Cheers,
      Seth

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        That's exactly what I said. The data is always written to the database if you have the UPDATE code in your second script.

        The MySql UPDATE code DOES NOT WAIT for a submit button to be hit, it just continues processing after the </form> statement in your 2nd script. The code does not have an event listener that catches something (such as submit) and then continues its processing.

        What you should do is put the MySql UPDATE statement in a 3rd script (e.g. Results3.php) and let that script be called by the form in your 2nd script. As follows:

        Results1.php: hit submit --> action=Results2 .php
        Results2.php: hit submit --> action=Results3 .php
        Results3.php : perform the MySql UPDATE command

        Ronald :cool:

        Comment

        • SHOverine
          New Member
          • Sep 2006
          • 18

          #5
          Thanks Ronald! One more thing I had to do is to set the $_SESSION variables so the team names and game ids would carry all the way to form three. Works like a charm :-)

          CHeers,
          Seth

          Comment

          Working...