Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

submit button acts differently to onchange=submit()

Status
Not open for further replies.

scriggs

IS-IT--Management
Jun 1, 2004
286
GB
I am not sure if PHP or JS is causing this issue, please tell me if I should ask in JS.

I have some code which created a dynamic drop down list and then queries a database for record data.

This works fine if i use the submit button but not if I use onchange=submit(). The submit button causes the page to refresh and load the database entry. Onchange=submit() causes the page to refresh and show the drop down list again.

Any help?

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Time and Fees Data</title>
<!-- 
Generates a dynamic drop down list of all
client codes in Time & Fees.

When a client is selected the page refreshes
and loads the client details from Time & Fees
-->
<style type="text/css">
<!--
body {font-family:verdana; font-size:11px; font-style:normal; font-weight:normal;}
th {color:white; background-color:gray; font-size:11px;}
td {background-color: silver; font-size:11px;}
-->
</style>
</head>
<body>
<?php

// odbc_query.php
$odbc_dsn = "timeandfees_db";
$odbc_userid = "";
$odbc_password = "";

if(@$_POST["custname"]!="") {
//if (!isset($_POST['TFsubmit'])) { // Step 1: fill initial form and present it
   $tmp = array();
   $tmp[] = '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
   $tmp[] = '<select name="custname" onchange="this.form.submit();">';
   $tmp[] = '<option value="">Select from list...</option>';

$query = "SELECT Clients.ClientKey FROM clients ORDER BY Clients.ClientKey";

if(!($odbc_db = odbc_connect($odbc_dsn, $odbc_userid, $odbc_password)))
	die ("Could not connect ot ODBC data source $odbc_dsn");
  
if(!($odbc_rs = odbc_do($odbc_db, $query)))
	die("Error executing query $query");
  
$num_cols = odbc_num_fields($odbc_rs);
if($num_cols < 1) die("Query returned an empty set");


while(odbc_fetch_row($odbc_rs)) $tmp[] = '<option>' . odbc_result($odbc_rs,'ClientKey') . '</option>';
$tmp[] = '</select>';
$tmp[] = '<input type="submit" name="TFsubmit" value="Display Time and Fees Information">';
$tmp[] = '</form>';
echo implode("\n",$tmp)."\n";

echo "<input type=\"text\" name=\"searchterm\" size=\"25\">";

}

else { // Step 2: Display data
   $tmp = array();
//   $valid_cust = validate($_POST['custname']); //make sure that input is in the format you are expecting
   $valid_cust = $_POST['custname'];
   $query = "SELECT Clients.ClientKey, Clients.CoyName, Addresses.* FROM Addresses INNER JOIN Clients ON 

Addresses.AddrKey = Clients.AddrKey where Clients.ClientKey = '" . $valid_cust . "'";
if(!($odbc_db = odbc_connect($odbc_dsn, $odbc_userid, $odbc_password)))
	die ("Could not connect ot ODBC data source $odbc_dsn");
  
if(!($odbc_rs = odbc_do($odbc_db, $query)))
	die("Error executing query $query");
  
$num_cols = odbc_num_fields($odbc_rs);
if($num_cols < 1) die("Query returned an empty set");

?>

<table summary="" style="{width:100%;}">
	<tr>
  <?php
  	for($a=1;$a<=$num_cols;$a++)
    {
  ?>
  	<th style="{text-align:left;}">
    	<b>
    <?php
    	echo odbc_field_name($odbc_rs, $a);
    ?>
    	</b>
    </th>
  <?php
  	}
  ?>
  </tr>
<?php
	while(odbc_fetch_row($odbc_rs))
  {
?>
	<tr>
  <?php
  	for($a=1;$a<=$num_cols;$a++)
    {
    	$data = odbc_result($odbc_rs, $a);
      echo "<td>$data</td>";
    }
  ?>
  </tr>
<?php
	}
}
?>
</table>    

</body>
</html>
 
It looks like you have nothing in the $_POST['custname']. After submitting the form, do a
Code:
print_r ($_POST['custname']);
to inspect if the values you are expecting are really there. If they are not, try to work out why they are not sent.
 
Thanks for the reply.

I thought about that and changed back to my original code which uses the submit button as a reference, but this has the same action.

Code:
//if (!isset($_POST['TFsubmit'])) { // Step 1: fill initial form and present

 
Oops, I just noticed I made a grave mistake. After the submission, you should do [tt]print_r ($_POST);[/tt] to get all the variables in the post array. That will help you debug your code. It is pretty normal for some browsers not to send the information about the button when the button was not really pressed so your original code would fail as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top