Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Form/Database/Mail help

    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 messages
    If 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. Also it only works when placed at the top of the page and when i do this the error message's then do not appear in the right place.

    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.

    Code:
    <?
    
    	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'];
    
      		// server side validation initialize a variable to put any errors into an array
    
        	$errors = array();
    
       
    
       if(!preg_match("/^[0-9]{4,6}$/", $orderid))
    
       		 {	
    
          	 	$errors[] = "*Order ID Must be between 4 and 6 digits";	//store error in array
    
    		 }
    
    	  
    
       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)";
    
    		}
    
    		
    
    		//preg_match ship_to
    
    		
    
       if(!preg_match("/^\s*[]?\s*\d+(\.\d{2})?\s*$/", $shippingcost))
    
       
    
       		{
    
       		 $errors[] = "*Invalid Shipping Cost, please enter a valid currency value";
    
    		}
    
    		
    
       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 (correct format is mm/dd/yyyy or m/d/yyyy)";
    
    		}
    
    		
    
    	  
    
       if (count($errors)>0)
    
       		{			//if any errors found print and display the errors.
    
          		
    
    			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="left">
    
    <h1>XML</h1>
    
    <h2>Message Interface</h2>
    
    
    
    <div id="nav">
    
    <a href="index.php">Home</a> 
    
    <a href="create.php">Create</a> 
    
    <a href="downloads.php">Downloads</a> 
    
    <a href="faq.php">FAQ</a> 
    
    <a href="contact.php">Contact</a> 
    
    <a href="#">Logout</a></div>
    
    
    
    <h3>Last Updated</h3>
    
    <p>28/03/2007</p>
    
    </div>
    
    
    
    <div id="right">
    
    <h1>Welcome...</h1>
    
    <h2>Username</h2>
    
    
    
    <h3> home </a> > Create &gt; 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'> &nbsp;&nbsp; <input type='button' value='Cancel' onClick="location.href='/';"></td></tr>
    
    </table>
    
    
    
     
    
    </form>
    
    
    
    <h3>Website Information</h3>
    
    <p>&copy; 2007 Design by <a href="mailto:garethowens@gmail.com">Gareth Owens</a></p>
    
    </div>
    
    
    
    </body>
    
    </html>

  • #2
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:
    echo $xmldocument->asXML('shippingnoticenew.xml'); 
    You can't echo anything out before the header redirect. You get the headers already sent error (since your already "sent" data to the screen). Instead of echo'ing that xml data there, just save it in a variable and then add that variable to the session. Then echo it on the next page. That should solve that problem.

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ohh and you can't print "Thank You File Added" either. You may want to rethink your approach on what you are trying to accomplish.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •