Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8615207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:19:05+00:00 2026-06-12T05:19:05+00:00

I have the following code to retain the value of a select box, which

  • 0

I have the following code to retain the value of a select box, which is submitting the form on change.

<form id="form2" name="form2" method="post" action="index.php" >
    <table width="98%" border="0">
      <tr>
        <td>SPAK Ref</td>
        <td><select name="SPKSelect" id="SPKSelect" onchange="document.forms[0].submit();document.forms['form2'].submit(); ">

                    <option value="">-- Select Ref --</option>
            <?php    

                $selectedSPK = $_POST['SPKSelect'];
                $assigned = $_POST['Sales_Exec'];
                $date = $_POST['DateSelect'];

                if ($selectedSPK);
                {
                    $SPKquery = "SELECT DISTINCT SPKCustNo FROM Data WHERE Assigned = '$assigned' AND RenewalDate = '$date' ORDER by RenewalDate";

                    $SPKresult = mysql_query($SPKquery);

                    while($row = mysql_fetch_array($SPKresult))
                    {
                        echo "<option value=\"".$row['SPKCustNo']."\">".$row['SPKCustNo']."</option>\n  ";
                    }

                }
            ?>
            <script type="text/javascript">
                document.getElementById('SPKSelect').value = <?php echo json_encode(trim($selectedSPK));?>;
            </script>
        </select> 

And this isn’t working, it just defaults back to the origianl –Select Ref— option.

I have this exact code working for other selectboxes in 'form1' however this is the first one in “form2′ which sits next to form 1. From what I can tell it’s not retaining the POST value of SPKSelect. Is this something to do with the fact that I’m using 2 forms? Does POST only work on 1 form per page? If so how can I get around this?

The full page is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>TIL / 006 Renewal Support Tool</title>
<link href="RenewalToolStyles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="scripts/functions.js"></script>

</head>

<?php

//Connect to Server / Database
$conn = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("TILRenewals");


function GenerateTable($Query)
{
    $host = "localhost";
$user = "root";
$pass = "";
$db = "TILRenewals";
    $link = mysql_connect($host, $user, $pass) or die('Could not connect: ' . mysql_error());      //build MySQL Link
    mysql_select_db($db) or die('Could not select database');        //select database
    $Table = "";  //initialize table variable

    $Table.= "<table border='1' style=\"border-collapse: collapse; text-align: center; font-size: small; cellspacing: 10px; \">"; //Open HTML Table

    $Result = mysql_query($Query); //Execute the query
    if(mysql_error())
    {
        $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
    }
    else
    {
        //Header Row with Field Names
        $NumFields = mysql_num_fields($Result);
        $Table.= "<tr style=\"background-color: #000; text-align: center; color: #FFFFFF; font-size: medium;\">";
        for ($i=0; $i < $NumFields; $i++)
        {     
            $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>"; 
        }
        $Table.= "</tr>";

        //Loop thru results
        $RowCt = 0; //Row Counter
        while($Row = mysql_fetch_assoc($Result))
        {
            //Alternate colors for rows
            if($RowCt++ % 2 == 0) $Style = "background-color: #3D6522; color: #FFFFFF;";
            else $Style = "background-color: #FFFFFF; color:#3D6522;";

            $Table.= "<tr style=\"$Style\">";
            //Loop thru each field
            foreach($Row as $field => $value)
            {
                $Table.= "<td>$value</td>";
            }
            $Table.= "</tr>";
        }
       // $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\"><td colspan='$NumFields'>Query Returned " . mysql_num_rows($Result) . " records</td></tr>";
    }
    $Table.= "</table>";

    return $Table;

}



?>





<body>

<div class="container">
  <div class="header"><img src="Images/Banner.png" width="728" height="90" /><!-- end .header --></div>
  <div class="content">
  <div id ="leftcolumn">
    <h2>View Assigned Cases</h2>
    <form id="form1" name="form1" method="post" action="index.php">



        <p>Sales Exec
          <select name="Sales_Exec" id="Sales_Exec" onchange="document.forms[0].submit();">

            <option value="">-- Select SE --</option>
            <?php    

$salesexec=$_POST['Sales_Exec'];

if ($salesexec);

{
    $Execquery = "SELECT DISTINCT Assigned FROM Data";

$Execresult = mysql_query($Execquery);

while($row = mysql_fetch_array($Execresult))
{
    echo "<option value=\"".$row['Assigned']."\">".$row['Assigned']."</option>\n  ";
}

}
?>
<script type="text/javascript">
document.getElementById('Sales_Exec').value = <?php echo json_encode(trim($_POST['Sales_Exec']));?>;
        </script>
          </select>
          Date
          <select name="DateSelect" id="DateSelect" onchange="document.forms[0].submit();">
            <option value="">-- Select Date --</option>
            <?php    

$selecteddate=$_POST['DateSelect'];

if ($selecteddate);

{
    $Datequery = "SELECT DISTINCT RenewalDate FROM Data ORDER by RenewalDate";

$Dateresult = mysql_query($Datequery);

while($row = mysql_fetch_array($Dateresult))
{
    echo "<option value=\"".$row['RenewalDate']."\">".$row['RenewalDate']."</option>\n  ";
}

}
?>
<script type="text/javascript">
document.getElementById('DateSelect').value = <?php echo json_encode(trim($_POST['DateSelect']));?>;
        </script>
          </select>
        </p>
        <p>
          <?php

    $assigned = $_POST['Sales_Exec'];
    $date = $_POST['DateSelect'];
    echo GenerateTable("SELECT SPKCustNo, RenewalDate, Product, ForeName, Surname FROM Data WHERE Assigned = '$assigned' AND RenewalDate = '$date' ");

?>
        </p>
    </form>

    <p>&nbsp;</p>
  </div>
  <div id ="rightcolumn">
  <h2>View Individual Case</h2>
  <form id="form2" name="form2" method="post" action="index.php" >
    <table width="98%" border="0">
      <tr>
        <td>SPAK Ref</td>
        <td><select name="SPKSelect" id="SPKSelect" onchange="submit_form('form1'); submit_form('form2'); ">

                    <option value="">-- Select Ref --</option>
            <?php    

$selectedSPK = $_POST['SPKSelect'];
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];


    $SPKquery = "SELECT DISTINCT SPKCustNo FROM Data WHERE Assigned = '$assigned' AND RenewalDate = '$date' ORDER by RenewalDate";

$SPKresult = mysql_query($SPKquery);

while($row = mysql_fetch_array($SPKresult)) {

    if($row["SPKCustNo"] == $selectedSPK){

         $select_true = "selected='selected'";

    }

         print "<option " . $select_true . " value='" .$row["SPKCustNo"] . "'>" . $row["SPKCustNo"] . "</option>";

    }


?>

    <script type="text/javascript">
document.getElementById('SPKselect').value = <?php echo json_encode(trim($_POST['SPKSelect']));?>;
        </script>    

        </select>
     </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td width="33%">ForeName</td>
        <td><select name="FName" id="FName">

          <option value="">--Customer ForeName---</option>
          <?php
                $assigned = $_POST['Sales_Exec'];
                $date = $_POST['DateSelect'];
                $selectedSPK = $_POST['SPKSelect'];

                if ($selectedSPK) {

                $FNamequery = "SELECT ForeName FROM Data WHERE  SPKCustNo = '$selectedCust'";

                $FNameresult = mysql_query($FNamequery);

                while($row = mysql_fetch_array($FNameresult))
                {
                    echo "<option value=\"".$row['ForeName']."\">".$row['ForeName']."</option>\n  ";
                }


                }


            ?>
          <script type="text/javascript">
document.getElementById('FName').value = <?php echo json_encode(trim($_POST['FName']));?>;
            </script>



        </select></td>
      </tr>
      <tr>
        <td></td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
  </table>
    <p>&nbsp;</p>
  </form>
  <p>&nbsp;</p>
  </div>

  <!-- end .content --></div>

   <div class="footer">
    <p>Footer<img src="Images/TILLogo.png" width="150" height="79" /></p>
    <!-- end .footer --></div>
  <!-- end .container --></div>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:19:07+00:00Added an answer on June 12, 2026 at 5:19 am

    IMPORTANT EDIT:

    I have overlooked one big thing!

    When you are submitting forms only the fields from current form are sent trough the request.

    There is one workaround, you can have hidden fields in current form that corresponds to inputs from first form and use javascript on before submit to copy values from first form to current hidden elements. And then submit current form.

    Possible error in onchange:

    onchange="document.forms[0].submit();document.forms['form2'].submit(); "
    

    Change it to:

    onchange="document.forms['form2'].submit();"
    

    And when you do an mysql_fetch do this so that you can know what is the current data so that you can select it, like this:

        while($row = mysql_fetch_array($SPKresult)) {
    
        if($row["SPKCustNo"] == $selectedSPK){
    
             $select_true = "selected='selected'";
    
        }
    
             print "<option " . $select_true . " value='" .$row["SPKCustNo"] . "'>" . $row["SPKCustNo"] . "</option>";
    
        }
    

    Also like Laurent said you need to remove semi colon from this line:

    if ($selectedSPK);
    {
    

    it should look like this:

    if ($selectedSPK) {
    

    EDIT JAVASCRIPT TIP:

    Create external .js file for example “workers.js” put it in for example js dir and include it in head of HTML like this:

    <script type="text/javascript" src="js/workers.js"></script>
    

    Then write your javascript functions in that file first function is this for submiting forms:

    function submit_form(formid) {
    
        document.getElementById(formid).submit();
    
    }
    

    Then call it from HTML onchange like this:

    onchange="submit_form('form_id');"
    

    This is much cleaner method and longterm it will give you readable code.

    Also try: http://www.smarty.net (for php templating)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code for loading image from url in xml parsing endElement method
Possible Duplicate: NSNumber retain count issue Hello, I have the following code: NSNumber *number
I have the following php code, which has been snipped for brevity. I am
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
I have following code <div id=main> <div id=one> </div> <div id=two> </div> <div id=three>
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have following code for inserting data into database using PDO. It inserts data
I have following code. ASPX Page <a href=AnyASPXPageOfWebsite.aspx onclick=javascript:CallJQuery(); > Set Price </a> JS
I have following code that I am compiling in a .NET 4.0 project namespace
I have following code using hibernate to throw a custom exception on error and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.