Click to See Complete Forum and Search --> : Submiting a from to a script on the same page


lightnb
07-09-2006, 09:22 PM
I have a form:

<form>

<input name="color_number_input" type="text">

<input name="" type="submit">

</form>

and I have a PHP script that I want to execute once the form is submited:

<?PHP

$color_number_input=$_POST['color_number_input'];

$sql = mysql_query('SELECT color_name, color_number, transmission_value_percent, correction FROM roscolux_color WHERE color_number = \'$color_number_input\'');
echo(mysql_error());
echo '<table border="1">';
while ($row = mysql_fetch_assoc($sql)) {
echo '<tr><td width="90">';
echo $row["color_number"];
echo '</td><td width="190">';
echo $row["color_name"];
echo '</td><td width="90">';
echo $row["transmission_value_percent"];
echo '</td><td>';
echo $row["correction"];
echo '</td></tr>';
}
echo '<table>';
?>


How do I connect these two pieces together so that when the submit button is pressed, the script executes, and displays the newly generated table below the original form?

NogDog
07-09-2006, 09:39 PM
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="color_number_input" type="text">
<input name="color_submit" type="submit">
</form>


if(isset($_POST['color_submit']))
{
// process the form input here
}

lightnb
07-09-2006, 10:25 PM
Thank you. The code works and produces no errors, but submiting the form doesn't return any results either. Am I doing something wrong in the mySQL part of the code?

I added echo $color_number_input; To be sure that the variable was being submited corectly, and it appears to be, I'm just not getting a response from the database.

Here's what I'm using:

/* Create a connection to the database */
$connect = mysql_connect($host, $db_user, $pass);
mysql_select_db($dbn, $connect);
?>

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

<input name="color_number_input" type="text">

<input name="color_submit" type="submit">

</form>


<?PHP

if(isset($_POST['color_submit']))
{
$color_number_input = $_POST['color_number_input'];
echo $color_number_input;

$sql = mysql_query('SELECT color_name, color_number, transmission_value_percent, correction FROM roscolux_color WHERE color_number = "$color_number_input"');
echo(mysql_error());

echo '<table border="1">';

while ($row = mysql_fetch_assoc($sql))
{
echo '<tr><td width="90">';
echo $row["color_number"];
echo '</td><td width="190">';
echo $row["color_name"];
echo '</td><td width="90">';
echo $row["transmission_value_percent"];
echo '</td><td>';
echo $row["correction"];
echo '</td></tr>';
}

echo '</table>';

}
?>

NogDog
07-09-2006, 10:38 PM
Invert your quotes on the query string (double around the entire string, single quotes internally) so that your variable gets interpolated:

$sql = mysql_query("SELECT color_name, color_number, transmission_value_percent, correction FROM roscolux_color WHERE color_number = '$color_number_input'");

As you have it with the whole string being inside of single quotes, you are actually passign the string '$color_number_input' and not the value of that variable to the query.

I often set the query into a variable and then pass that variable to mysql_query(), which makes it easy to output the query string to find out where I screwed up when things aren't working. :)

lightnb
07-09-2006, 11:25 PM
Thanks - It's working correctly now. :)

I never knew there was a difference between single and double quotes- Is there a rule of thumb on which to use where?

NogDog
07-09-2006, 11:56 PM
Use double quotes when the string contains variables and/or special escape characters ("\n" for newline, "\t" for tab, etc.).

In any other situation (no variables or special escape characters), you can use either single or double quotes, with there being a popular opinion that single quotes will parse a little bit faster.

There's also a quoting method called "heredoc (http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)" which interpolates variables just like double-quoting, but makes it easy to use both double and single quotes internally:

$string = <<<END
"This is a test."
It is only a test.
This isn't very interestin, is it?
The value of variable is $variable.
END;