A
Anonymous
Guest
I am trying to create a script that recognizes whether or not the user came from the same form (clicked Add Order) or if they came from a different page. My code functions properly, but still puts out an error that says "Notice: Undefined index: op in C:\Program Files\Apache Group\Apache2\htdocs\add_order_form.php on line 3" I know that this is due to the fact that the script has not yet found a value for "op" because it isnt mentioned until later in the script. Can I start the script with a default value for 'op' that changes when the 'op' value is finally posted? Here is my code...
<?php
if ($_POST['op'] == "") {
//havent seen the form so show it
$display_block = "<h1>Add Insect Order</h1>
<form method=\"POST\" action=\"$_SERVER[PHP_SELF]\">
<P><strong>Order Name:</strong><br>
<input type=\"text\" name=\"order_name\" size=30 maxlength=40>
<P><strong>Order Common Name:</strong><br>
<input type=\"text\" name=\"order_common_name\" size=30 maxlength=40>
<P><strong>Order Distinguishing characteristic:</strong><br>
<textarea name=\"order_characteristic\" cols=50 rows=5 wrap=virtual></textarea>
<P><strong>Order general description:</strong><br>
<textarea name=\"order_description\" cols=50 rows=5 wrap=virtual></textarea>
<input type=\"hidden\" name=\"op\" value=\"add\">
<P><input type=\"submit\" name=\"submit\" value=\"Add Order\"></P>
</FORM>";
} else if ($_POST['op'] == "add") {
$display_block = "TEST";
//time to add to tables, so check for required fields
if (($_POST['order_name'] == "") || ($_POST['order_common_name'] == "") ||
($_POST['order_characteristic'] == "") || ($_POST['order_description'] == "")) {
header ("Location: add_order_form.php");
exit;
}
// Now that we know all information is available, open a connection to mysql
$conn = mysql_connect('localhost', 'root', '');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
// pick the database to use
mysql_select_db("fizzyfish",$conn);
//add information to the insect_order table
$add_new_order = "insert into insect_order (orderID, order_name, order_common_name,
order_characteristic, order_desc)
values ('', '$_POST[order_name]', '$_POST[order_common_name]', '$_POST[order_characteristic]',
'$_POST[order_description]')";
if (mysql_query($add_new_order, $conn)) {
echo "record added!";
} else {
echo "something went wrong";
}
//get orderID for use with other tables
$orderID = mysql_insert_id();
echo $orderID;
$display_block = "<h1>Order Added!</h1> Would you like to <A HREF=\"add_order_form.php\">add
another</A>?</P>";
}
?>
<HTML>
<HEADER>
</HEADER>
<BODY>
<?php echo $display_block; ?>
</BODY>
</HTML>
<?php
if ($_POST['op'] == "") {
//havent seen the form so show it
$display_block = "<h1>Add Insect Order</h1>
<form method=\"POST\" action=\"$_SERVER[PHP_SELF]\">
<P><strong>Order Name:</strong><br>
<input type=\"text\" name=\"order_name\" size=30 maxlength=40>
<P><strong>Order Common Name:</strong><br>
<input type=\"text\" name=\"order_common_name\" size=30 maxlength=40>
<P><strong>Order Distinguishing characteristic:</strong><br>
<textarea name=\"order_characteristic\" cols=50 rows=5 wrap=virtual></textarea>
<P><strong>Order general description:</strong><br>
<textarea name=\"order_description\" cols=50 rows=5 wrap=virtual></textarea>
<input type=\"hidden\" name=\"op\" value=\"add\">
<P><input type=\"submit\" name=\"submit\" value=\"Add Order\"></P>
</FORM>";
} else if ($_POST['op'] == "add") {
$display_block = "TEST";
//time to add to tables, so check for required fields
if (($_POST['order_name'] == "") || ($_POST['order_common_name'] == "") ||
($_POST['order_characteristic'] == "") || ($_POST['order_description'] == "")) {
header ("Location: add_order_form.php");
exit;
}
// Now that we know all information is available, open a connection to mysql
$conn = mysql_connect('localhost', 'root', '');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
// pick the database to use
mysql_select_db("fizzyfish",$conn);
//add information to the insect_order table
$add_new_order = "insert into insect_order (orderID, order_name, order_common_name,
order_characteristic, order_desc)
values ('', '$_POST[order_name]', '$_POST[order_common_name]', '$_POST[order_characteristic]',
'$_POST[order_description]')";
if (mysql_query($add_new_order, $conn)) {
echo "record added!";
} else {
echo "something went wrong";
}
//get orderID for use with other tables
$orderID = mysql_insert_id();
echo $orderID;
$display_block = "<h1>Order Added!</h1> Would you like to <A HREF=\"add_order_form.php\">add
another</A>?</P>";
}
?>
<HTML>
<HEADER>
</HEADER>
<BODY>
<?php echo $display_block; ?>
</BODY>
</HTML>