Forum Moderators: open

Message Too Old, No Replies

Question about submitting form using select onChange()

         

davestech

5:41 pm on Apr 13, 2010 (gmt 0)

10+ Year Member



I have a mysql table with the following fields:

table name: products
fields: prd_id int, prd_desc varchar(100),myvalue varchar(100)

sample data
1, Product 1, Some random text for product 1
2, Product 2, Some random text for product 2

I want to output the value in the myvalue field when the value changes in the combobox.

If I change the select's value and use a submit button, the program works properly and the text is outputted accordingly.



<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">

<?php

mysql_connect("localhost", "mylogin", "mypw") or die(mysql_error());
mysql_select_db("mytest") or die(mysql_error());

$myvalue = '';
$query = "SELECT * FROM product";


if($result = mysql_query($query))
{
if($success = mysql_num_rows($result) > 0)
{
echo "Product : <select name='queryTypeCombo'>\n";
echo "<option>-- Please select --</option>\n";

while ($row = mysql_fetch_array($result))
{
$Productname = $row['prd_desc'];
$ProductID = $row['prd_id'];

if(isset($_POST['send']))
{
echo '<option value="'.$ProductID.'"';
if($ProductID==$_REQUEST['queryTypeCombo'])
{
echo ' selected';
$myvalue = $row['myvalue'];
}
echo '>'. $Productname . '</option>'."\n";
}
else
{
echo "<option value='$ProductID'>$Productname</option>\n";
}
}
echo "</select>\n";
}
}

echo "<br>";
echo "myvalue : " . $myvalue . "<br>";

?>


<br />
<input name="send" type="submit" value="Send!">
</form>




If I use the select's onChange() to submit the form, the text is not outputted and the state of the combobox is not restored.



<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">

<?php

mysql_connect("localhost", "mylogin", "mypw") or die(mysql_error());
mysql_select_db("mytest") or die(mysql_error());

$myvalue = '';
$query = "SELECT * FROM product";

if($result = mysql_query($query))
{
if($success = mysql_num_rows($result) > 0)
{
echo "Product : <select name='queryTypeCombo' onChange='this.form.submit();'>\n";
echo "<option>-- Please select --</option>\n";

while ($row = mysql_fetch_array($result))
{
$Productname = $row['prd_desc'];
$ProductID = $row['prd_id'];

if(isset($_POST['send']))
{
echo '<option value="'.$ProductID.'"';
if($ProductID==$_REQUEST['queryTypeCombo'])
{
echo ' selected';
$myvalue = $row['myvalue'];
}
echo '>'. $Productname . '</option>'."\n";
}
else
{
echo "<option value='$ProductID'>$Productname</option>\n";
}
}
echo "</select>\n";
}
}

echo "<br>";
echo "myvalue : " . $myvalue . "<br>";

?>

<br />
<input name="send" type="submit" value="Send!">
</form>




What is different about submitting a form using the select's onChange() and clicking a submit button?

edit: why is the spacing formatting of my code listing missing in my post?

Fotiman

6:01 pm on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Intput elements with type="submit" are ONLY submitted if they are clicked. For example, you could have multiple submit buttons:

<input type="submit" name="foo" value="Submit">
<input type="submit" name="bar" value="Submit">

And only the one what was clicked on would be sent to the server (foo=Submit or bar=Submit, but not both).

In your PHP code, you are explicitly looking for the submit button:

if(isset($_POST['send']))

Therefore, when you submit the form via the onchange handler, the "send" button is not "on" (that is, it won't be sent to the server), so any code within the body of that if statement will not be executed.

As for the formatting of your post, I typically put code examples in:
[quote][pre][code]
your code here

[/code][/pre][/quote]
Note, it also helps to remove any blank lines as they will cause it to format strange as well.

davestech

12:06 am on Apr 17, 2010 (gmt 0)

10+ Year Member



thx for the help...