Hi i'm having some problems with my coding, i think i may be trying to do too much with a single form. I'm trying to do the following:
Take user input from a forms ranging from 4-15 elements.
Validate the input, if invalid repost the form with error messagesIf the form is valid, build an XML message using the input
Store the form entries plus the XML document in mySQL (BLOB for the XML)
E-Mail the receiver with the file as at attachment (not started this area yet, but will probably appear on the second page).
Once this is done i want to forward the user to a next page which basically says, thank you...xml message contents are as follows...and a link to the XML document.
I've managed to do all of these things seperately but when i try and bring all the code together something doesn't work correctly.
Is there a different way to forward to the user to the second page as the header() function doesn't work when the DB and XML code is placed in the 'else' section of the code.
When i did get it to forward to the next page the variables used were not passed through so i couldn't display the contents of the XML document.
Basically if someone could just point me in the right direction in relation to structuring the code so the errors are output in the right place (just above where the form is output" and how to forward to a second page as discussed. Any help with PHP's mail() function would be a bonus as well.
<?
session_start();
session_destroy();
$submit=$_POST['submit'];
if($submit)
{
$sender = $_SESSION['username'];
$receiver = $_POST['receiver'];
$orderid = $_POST['orderid'];
$datereceived = $_POST['datereceived'];
$shipto = $_POST['shipto'];
$shippingcost = $_POST['shippingcost'];
$datesent = $_POST['datesent'];
$message = $_POST['message'];
$errors = array();
if(!preg_match("/^[0-9]{4,6}$/", $orderid))
{
$errors[] = "*Order ID Must be between 4 and 6 digits";
}
if(!preg_match("/^([1-9]|0[1-9]|1[012])\D([1-9]|0[1-9]|[12][1-9]|3[01])\D(19[0-9][0-9]|20[0-9][0-9])$/", $datereceived))
{
$errors[] = "*Invalid Date Received (correct format is mm/dd/yyyy or m/d/yyyy)";
}
if(!preg_match("/^\s*[£]?\s*\d+(\.\d{2})?\s*$/", $shippingcost))
{
$errors[] = "*Invalid Shipping Cost";
}
if(!preg_match("/^([1-9]|0[1-9]|1[012])\D([1-9]|0[1-9]|[12][1-9]|3[01])\D(19[0-9][0-9]|20[0-9][0-9])$/", $datesent))
{
$errors[] = "*Invalid Date Sent";
}
if (count($errors)>0)
{
echo "<strong>ERROR:<br \>\n";
foreach($errors as $err)
echo "$err<br \></strong>\n";
} else {
$xmldocument = simplexml_load_file('shippingnotice.xml');
$xmldocument->order[0]->sender = $sender;
$xmldocument->order[0]->receiver = $receiver;
$xmldocument->order[0]->orderid = $orderid;
$xmldocument->order[0]->datereceived = $datereceived;
$xmldocument->shipping[0]->shipto = $shipto;
$xmldocument->shipping[0]->shippingcost = $shippingcost;
$xmldocument->shipping[0]->datesent = $datesent;
$xmldocument->shipping[0]->message = $message;
echo $xmldocument->asXML('shippingnoticenew.xml');
$_SESSION['filename'] = "shippingnoticenew.xml";
// Connect database.
$host="localhost"; // Host name.
$db_user="root"; // MySQL username.
$db_password=""; // MySQL password.
$database="xml"; // Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);
$query1="INSERT INTO `shippingnotice` ( `shipping_msg_id` , `sender` , `receiver` , `orderid` , `datereceived` , `shipto` , `shippingcost` , `datesent` , `message` , `xml_file` ) VALUES ( '' , '$sender', '$receiver', '$orderid', '$datereceived', '$shipto', '$shippingcost', '$datesent', '$message', 'shippingnoticenew.xml')";
$result=mysql_query($query1) or die("Couldn't add users");
print "Thank You File Added";
//mail()
header("location:shipping-2.php" );
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Web EDI Interface</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="Web EDI Interface" />
<link rel="stylesheet" href="formstyle.css" type="text/css" />
<SCRIPT LANGUAGE="JavaScript">
function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=1,resizable=1,width=800,height=600,left = 300,top = 300');");
}
</script>
</head>
<body>
<div id="right">
<h1>Welcome...</h1>
<h2>Username</h2>
<h3> home </a> > Create > Shipping Notice </h3>
<p>Welcome to the 'Create' Area for the Shipping Notice message type. Other message types are available by pressing the back buttton on your browser, for help refer to the FAQ or individual help '?' next to each form entry.</p>
<p>Please fill out all form elements and press the send button. Invalid entries will be rejected and you will be asked to re-type information. Valid entries will be forwarded to the XML create area. </p>
<form name="shippingnotice" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<table cellspacing='16' cellpadding='0' border='0' >
<tr>
<td class="form_field" valign='top' align='right'>Receiver </td><td width='10' aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
<td class="form_text">
<select name="receiver">
<option value=''>- Select -</option>
<option value="Number 1" > Number 1
<option value="Number 2" > Number 2
<option value="Number 3" > Number 3
</select> <a href="javascript:popUp('shippinghelp.php')">?</a> </td>
</tr>
<tr>
<td class="form_field" valign='top' align='right'>Order Identification Number (6 Digits) </td><td width='10' aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
<td class="form_text">
<input type="text" name="orderid" <?php if (isset($errors)){ echo 'value="'.htmlentities($_POST['orderid']).'"';}?>> <a href="javascript:popUp('shippinghelp.php')">?</a> </td>
</tr>
<tr>
<td class="form_field" valign='top' align='right'>Date Recieved (mm-dd-yyyy) </td><td width='10' aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
<td class="form_text">
<input type="text" name="datereceived" <?php if (isset($errors)){ echo 'value="'.htmlentities($_POST['datereceived']).'"';}?>> <a href="javascript:popUp('shippinghelp.php')">?</a> </td>
</tr>
<tr>
<td class="form_field" valign='top' align='right'>Ship To </td><td width='10' aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
<td class="form_text">
<textarea name="shipto" rows=4 cols=25 <?php if (isset($errors)){ echo 'value="'.htmlentities($_POST['shipto']).'"';}?>></textarea> <a href="javascript:popUp('shippinghelp.php')">?</a> </td>
</tr>
<tr>
<td class="form_field" valign='top' align='right'>Shipping Cost </td><td width='10' aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
<td class="form_text">
<input type="text" name="shippingcost" <?php if (isset($errors)){ echo 'value="'.htmlentities($_POST['shippingcost']).'"';}?>> <a href="javascript:popUp('shippinghelp.php')">?</a> </td>
</tr>
<tr>
<td class="form_field" valign='top' align='right'>Date Sent (mm-dd-yyyy) </td><td width='10' aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
<td class="form_text">
<input type="text" name="datesent" <?php if (isset($errors)){ echo 'value="'.htmlentities($_POST['datesent']).'"';}?>> <a href="javascript:popUp('shippinghelp.php')">?</a> </td>
</tr>
<tr>
<td class="form_field" valign='top' align='right'>Additional Message (optional) </td><td width='10' aligh='right' valign='top'></td>
<td class="form_text">
<textarea name="message" rows=4 cols=25 <?php if (isset($errors)){ echo 'value="'.htmlentities($_POST['message']).'"';}?>></textarea> <a href="javascript:popUp('shippinghelp.php')">?</a> </td>
</tr>
<tr><td colspan=3 align='center'><input name='submit' type='submit' value='Submit'> <input type='button' value='Cancel' onClick="location.href='/';"></td></tr>
</table>
</form>
</div>
</body>
</html>