Jump to content

combining the form with the processing page


barrowvian

Recommended Posts

The below code is what I have for my basic contact form;

<form method="POST" action="mailer.php">
   <table width="100%" border="0" cellspacing="0" cellpadding="0">
     <tr>
       <td width="9%" align="left" valign="top">Name:</td>
       <td width="91%"><input type="text" name="name" size="38" /></td>
     </tr>
     <tr>
       <td align="left" valign="top">Email:</td>
       <td><input type="text" name="email" size="38" /></td>
     </tr>
     <tr>
       <td align="left" valign="top"></td>
       <td></td>
     </tr>
     <tr>
       <td align="left" valign="top">Subject:</td>
       <td><select size="1" name="drop_down">
         <option>General Enquiries</option>
         <option>Sales Enquiries</option>
         <option>Press Enquiries</option>
       </select></td>
     </tr>
     <tr>
       <td align="left" valign="top">Message:</td>
       <td><textarea rows="5" name="message" cols="30"></textarea></td>
     </tr>
     <tr>
       <td> </td>
       <td><p>
<img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br>
<label for='message'>Enter the code above here :</label><br>
<input id="6_letters_code" name="6_letters_code" type="text"><br>
<small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small>
</p></td>
     </tr>
     <tr>
       <td> </td>
       <td><input type="submit" value="Submit" name="submit" /></td>
     </tr>
   </table>
</form>

<script language='JavaScript' type='text/javascript'>
function refreshCaptcha()
{
var img = document.images['captchaimg'];
img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
}
</script>

 

And the following code is what is included in mailer.php

<?php
session_start();
if(isset($_POST['submit'])) {
	$name_field  = isset($_POST['name']) ? trim($_POST['name'])  : '';
	$email_field  = isset($_POST['email']) ? trim($_POST['email'])  : '';
	$message  = isset($_POST['message']) ? trim($_POST['message'])  : '';
	$dropdown = $_POST['drop_down'];
	$flag="OK";   // This is the flag and we set it to OK
	$msg="";      // Initializing the message to hold the error messages
}

//name validation
if (!preg_match("/^[a-zA-Z\-\s]{2,40}$/", $name_field)){
	$msg= $msg . "<font color=\"color\">Invalid Name!</font><br />";
	$flag="NOTOK";   //setting the flag to error flag.
}

//email validatin
if(!preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/', $email_field)){
	$msg= $msg . "<font color=\"color\">Invalid Email!</font> <br />";
	$flag="NOTOK";   //setting the flag to error flag.
}

//message validation
if(strlen($message) < 5 ){
	$msg= $msg . "<font color=\"color\">Message must be over 5 characters!</font><br />";
	$flag="NOTOK";   //setting the flag to error flag.
}

if(strlen($message) > 250 ){
	$msg= $msg . "<font color=\"color\">Message cannot be over 250 characters!</font><br />";
	$flag="NOTOK";   //setting the flag to error flag.
}

// captcha validation
if(empty($_SESSION['6_letters_code'] ) ||
    strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
	{
    $msg= $msg . "<font color=\"color\">The captcha code does not match!</font><br />";
	$flag="NOTOK";
    }

//check the flag
if($flag <>"OK"){
	echo "$msg <br /><hr><input type='button' value='Go Back' onClick='history.go(-1)'>";
}

//send the email to location based upon result of dropdown menu
if($dropdown == "General Enquiries" && $flag == "OK"){
	$to = "ENTER EMAIL HERE";
	$subject = "General Enquiry";
	$body = "From:" . htmlspecialchars($name_field) . "\n E-Mail:" . htmlspecialchars($email_field) . "\n Message:" . 			        htmlspecialchars($message);
	echo "Your email has been submitted,<strong> " . htmlspecialchars($name_field) . "</strong>. I aim to reply to all emails within 48 hours.";
	mail($to, $subject, $body);
	// this is the reply message
	$reply = "Thank you for your email";
	$replybody = "thank you again";
	mail($email_field, $reply, $replybody);
}elseif($dropdown == "Sales Enquiries" && $flag == "OK"){
	$to = "ENTER EMAIL HERE";
	$subject = "Sales Enquiry";
	$body = "From:" . htmlspecialchars($name_field) . "\n E-Mail:" . htmlspecialchars($email_field) . "\n Message:" . 			        htmlspecialchars($message);
	echo "Your email has been submitted,<strong> " . htmlspecialchars($name_field) . "</strong>. I aim to reply to all emails within 48 hours.";
	mail($to, $subject, $body);
}elseif($dropdown == "Press Enquiries" && $flag == "OK"){
	$to = "ENTER EMAIL HERE";
	$subject = "Press Enquiry";
	$body = "From:" . htmlspecialchars($name_field) . "\n E-Mail:" . htmlspecialchars($email_field) . "\n Message:" . 			        htmlspecialchars($message);
	echo "Your email has been submitted,<strong> " . htmlspecialchars($name_field) . "</strong>. I aim to reply to all emails within 48 hours.";
	mail($to, $subject, $body);
}else {
 // an errror was detected
}
?>

 

Is there a way that I can have it all on one page rather than 2? And also to include the error message for each field opposite?

 

How would I go about achieving this?

 

Thanks

Link to comment
Share on other sites

Thanks for that Siric, it was a good help. That has now left me with this code;

<?php include("includes/header.php"); ?>

<div id="main">
    	<div class="container">
        
        	<div id="header">
            
            	<ul id="menu">
                	<li><a href="/ads/index.php">Homepage</a></li>
                	<li><a href="/ads/search.php">Search</a></li>
                	<li><a href="/ads/faq.php">FAQ</a></li>
                	<li><a href="/ads/blog.php">Blog</a></li>
                	<li><a href="/ads/contact.php" class="active">Contact</a></li>
                </ul>
                
                <ul id="menu2" name="menu2">
			<form action="search.php" method="GET">
			  <input type="text" name="searchterms" size="20" />
			  <input type="submit" 			value="Search"></form>
			</ul>
                
            	<div id="logo"></div>
            
            </div>
            
            <div id="block_featured" class="block">
			<img src="images/ribbon_featured.png" class="ribbon" alt="Featured Project"/>
       	    <div class="block_inside">
                
                	<div class="text_block">
                    <h2>Contact Me</h2>
                    
                    <?php
				session_start();
				if (isset($_POST['submit'])) {  //This checks to see if the submit button is set //process query

				$name_field  = isset($_POST['name']) ? trim($_POST['name'])  : '';
				$email_field  = isset($_POST['email']) ? trim($_POST['email'])  : '';
				$message  = isset($_POST['message']) ? trim($_POST['message'])  : '';
				$dropdown = $_POST['drop_down'];
				$flag="OK";   // This is the flag and we set it to OK
				$msg="";      // Initializing the message to hold the error messages


//name validation
if (!preg_match("/^[a-zA-Z\-\s]{2,40}$/", $name_field)){
	$msg= $msg . "<font color=\"color\">Invalid Name!</font><br />";
	$flag="NOTOK";   //setting the flag to error flag.
}

//email validatin
if(!preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/', $email_field)){
	$msg= $msg . "<font color=\"color\">Invalid Email!</font> <br />";
	$flag="NOTOK";   //setting the flag to error flag.
}

//message validation
if(strlen($message) < 5 ){
	$msg= $msg . "<font color=\"color\">Message must be over 5 characters!</font><br />";
	$flag="NOTOK";   //setting the flag to error flag.
}

if(strlen($message) > 250 ){
	$msg= $msg . "<font color=\"color\">Message cannot be over 250 characters!</font><br />";
	$flag="NOTOK";   //setting the flag to error flag.
}

// captcha validation
if(empty($_SESSION['6_letters_code'] ) ||
    strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
	{
    $msg= $msg . "<font color=\"color\">The captcha code does not match!</font><br />";
	$flag="NOTOK";
    }

//check the flag
if($flag <>"OK"){
	echo "$msg <br /><hr><input type='button' value='Go Back' onClick='history.go(-1)'>";
}

//send the email to location based upon result of dropdown menu
if($dropdown == "General Enquiries" && $flag == "OK"){
	$to = "ENTER EMAIL HERE";
	$subject = "General Enquiry";
	$body = "From:" . htmlspecialchars($name_field) . "\n E-Mail:" . htmlspecialchars($email_field) . "\n Message:" . 			        htmlspecialchars($message);
	echo "Your email has been submitted,<strong> " . htmlspecialchars($name_field) . "</strong>. I aim to reply to all emails within 48 hours.";
	mail($to, $subject, $body);
	// this is the reply message
	$reply = "Thank you for your email";
	$replybody = "thank you again";
	mail($email_field, $reply, $replybody);
}elseif($dropdown == "Sales Enquiries" && $flag == "OK"){
	$to = "ENTER EMAIL HERE";
	$subject = "Sales Enquiry";
	$body = "From:" . htmlspecialchars($name_field) . "\n E-Mail:" . htmlspecialchars($email_field) . "\n Message:" . 			        htmlspecialchars($message);
	echo "Your email has been submitted,<strong> " . htmlspecialchars($name_field) . "</strong>. I aim to reply to all emails within 48 hours.";
	mail($to, $subject, $body);
}elseif($dropdown == "Press Enquiries" && $flag == "OK"){
	$to = "ENTER EMAIL HERE";
	$subject = "Press Enquiry";
	$body = "From:" . htmlspecialchars($name_field) . "\n E-Mail:" . htmlspecialchars($email_field) . "\n Message:" . 			        htmlspecialchars($message);
	echo "Your email has been submitted,<strong> " . htmlspecialchars($name_field) . "</strong>. I aim to reply to all emails within 48 hours.";
	mail($to, $subject, $body);
}else {
 // an errror was detected
}

				} else { // insert the form 

				echo '<FORM name="email"  action="$_SERVER[\'PHP_SELF\'];"  method="post" >';
   					
				echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
     				
				echo '<tr>
      				<td width="9%" align="left" valign="top">Name:</td>
       				<td width="91%"><input type="text" name="name" size="38" /></td>
     				</tr>
     				
				<tr>
       				<td align="left" valign="top">Email:</td>
       				<td><input type="text" name="email" size="38" /></td>
     				</tr>
     
 				<tr>
       				<td align="left" valign="top"></td>
       				<td></td>
    				</tr>
     
 				<tr>
       				<td align="left" valign="top">Subject:</td>
       				<td><select size="1" name="drop_down">
         				<option>General Enquiries</option>
         				<option>Sales Enquiries</option>
         				<option>Press Enquiries</option>
       				</select></td>
     				</tr>
     
 				<tr>
       				<td align="left" valign="top">Message:</td>
       				<td><textarea rows="5" name="message" cols="30"></textarea></td>
     				</tr>
     
 				<tr>
       				<td> </td>
       				<td><p>';

                    echo '<img src="captcha_code_file.php?rand=' . rand() . '" id=\'captchaimg\' ><br>';
				echo '<label for=\'message\'>Enter the code above here :</label><br>
				<input id="6_letters_code" name="6_letters_code" type="text"><br>
				<small>Can\'t read the image? click <a href=\'javascript: refreshCaptcha();\'>here</a> to refresh</small>
				</p></td>
     				</tr>
     
 				<tr>
       				<td> </td>
      				<td><input type="submit" value="Submit" name="submit" /></td>
     				</tr>
   
   				</table>
			</form>'; // echo closed here

				echo '<script language=\'JavaScript\' type=\'text/javascript\'>
				function refreshCaptcha()
				{
					var img = document.images[\'captchaimg\'];
					img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
				}
				</script>';

				}
				?>
                   




   	          </div>
                    
              </div>
            </div>
            

        
        </div>
    </div>



<?php include("includes/footer.php"); ?>

 

But when I submit it I get this from google chrome: Oops! This link appears to be broken. And the url is http://localhost/ads/$_SERVER['PHP_SELF'];

 

What do I need to change to get this bit working?

Link to comment
Share on other sites

you used single quote in your form

} else { // insert the form 

				echo '<FORM name="email"  action="$_SERVER[\'PHP_SELF\'];"  method="post" >';

replace it with

} else { // insert the form 

				echo '<FORM name="email"  action="'.$_SERVER[\'PHP_SELF\'].'"  method="post" >';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.