It works fine with your new code for the script above.
But now I want to use it with my form validation also. When I do that it is acting the same like before.
This is the total code.
<?php
include("ok/connectdb.php");
switch($_POST['view'])
{
case 1: /* first time submit, show preview */
$product_name = $_POST['product_name'];
$product_number = $_POST['product_number'];
$category_id = $_POST['category_id'];
$view = $_POST['view'];
$category_name_result = mysql_query("SELECT category_name FROM category WHERE category_id = '$category_id'") or die(mysql_error());
$row = mysql_fetch_assoc($category_name_result);
$category_name = $row['category_name'];
$errors = array(); // set the errors array to empty, by default
$fields = array(); // stores the field values
$success_message = "";
// import the validation library
require("validation.php");
$rules = array(); // stores the validation rules
// standard form fields
$rules[] = "required,product_name,Please enter a name.";
$rules[] = "required,product_number,Please enter a number.";
$rules[] = "required,category_id,Please enter a category.";
$errors = validateFields($_POST, $rules);
// if there were errors, re-populate the form fields
if (!empty($errors))
{
$fields = $_POST;
$INPUT_BUTTON = "<input type='submit' name='preview' value='preview'>";
}
// no errors! redirect the user to the thankyou page (or whatever)
else
{
$message = "All fields have been validated successfully!";
// here you would either email the form contents to someone or store it in a database.
// To redirect to a "thankyou" page, you'd just do this:
$type = 'none';
$category_name_result = mysql_query("SELECT category_name FROM category WHERE category_id = '$category_id'") or die(mysql_error());
$row = mysql_fetch_assoc($category_name_result);
$category_name = $row['category_name'];
$view = 2;
$EXTRA_INFO = "Are your sure you want to submit:<br> <p>Name: $product_name <p>Category: $category_name";
$INPUT_BUTTON = "<input type='submit' name='submit' value='Edit'>
<input type='submit' name='submit' value='Submit'>";
}
break;
case 2: /* second time submit, if edit pressed, then edit else submit */
if($_POST['submit'] == "Edit")
{ /* Edit and reset to first time here */
$product_name = $_POST['product_name'];
$category_id = $_POST['category_id'];
$type = "block";
$view = 1;
$INPUT_BUTTON = "<input type='submit' name='preview' value='preview'>";
$EXTRA_INFO = "Please edit your input:<br>";
}
else
{ /* Submit info */
$product_name = $_POST['product_name'];
$category_id = $_POST['category_id'];
$category_name_result = mysql_query("SELECT category_name FROM category WHERE category_id = '$category_id'") or die(mysql_error());
$row = mysql_fetch_assoc($category_name_result);
$category_name = $row['category_name'];
if(!mysql_query("INSERT INTO product(product_name) values('$product_name')"))
$EXTRA_INFO = "Error inserting data into database";
$product_id = mysql_insert_id();
if (!mysql_query("INSERT INTO product_category(product_id, category_id) values ('$product_id','$category_id')"))
$EXTRA_INFO = "Error inserting data into database";
else
$EXTRA_INFO = "The following entry has been submitted:<br><pre>$product_name, $category_name</pre>";
$type = "none";
}
break;
default: /* either we've never been here or something has screwed our counter */
$EXTRA_INFO = "Please input your desired submission:<br>";
$INPUT_BUTTON = "<input type='submit' name='preview' value='preview'>";
$type = "block";
$view = 1;
break;
}
/* display our page accordingly */
echo $EXTRA_INFO;
echo "<form action=".$_SERVER["PHP_SELF"]." method='post'>";
// if $errors is not empty, the form must have failed one or more validation
// tests. Loop through each and display them on the page for the user
if (!empty($errors))
{
echo "<div class='error' style='width:100%;'>Please fix the following errors:\n<ul>";
foreach ($errors as $error)
echo "<li>$error</li>\n";
echo "</ul></div>";
}
if (!empty($message))
{
echo "<div class='notify'>$success_message</div>";
}
echo "<span name='input_form' style='display: $type'>
<input type='hidden' value='$view' name='view'>
<p>Name:
<input type='text' name='product_name' value='$product_name'><br>
<input type='text' name='product_number' value='$product_number'><br>
<p>Category: <select name='category_id'>
<!-- Drop down -->";
$categoryresult = mysql_query("SELECT DISTINCT category_name, category_id FROM category") or die(mysql_error());
while($row = mysql_fetch_array($categoryresult))
{
# If the post variable is set and the current row matches, select the row.
if ( isset ( $_POST['category_id'] ) && $row['category_id'] == $_POST['category_id'] )
echo "<option value=".$row['category_id']." selected>".$row['category_name']."</option>";
else
echo "<option value=".$row['category_id'].">".$row['category_name']."</option>";
}
echo "<option value='category_name' selected></option>
</select>
<!-- and so on for the rest -->
</span>";
echo $INPUT_BUTTON;
echo "</form>";
?>
Idea?