View Single Post
  #3 (permalink)  
Old 11-12-2008, 08:32 PM
niggles niggles is offline
WebProWorld Member
 
Join Date: Oct 2005
Posts: 40
niggles RepRank 1
Default Re: PHP Sticky fields

Here's what you need to do:

* put the main PHP above the form so you can check all the variables etc first - so change if the code to be more like:
Code:
if(!isset($_POST['submit'])){
// do something
} else {
// do something else
}
to

Code:
if(isset($_POST['submit'])){
// process the post variables

// if all is fine...
$sucess = true;
}
* if it's sucessful and no errors , you no longer need to show the form, otherwise you need to still show the form.

* In the form, change the fields so they read like this:

Code:
<input name='name' type='text' id='name' size='30'  value='<?= $name ?>' />
This puts the posted value into the field if we're still showing the form.

Hope that helps and is not too confusing.

Here's a script I use which also includes some basic anti-spambot functions - you'll need to set your own mailing routine, but otherwise, this works great:

Code:
<?php

$_POST = array_merge($_POST, $_GET);
$error = "";
$name = isset($_POST["name"]) ? htmlspecialchars($_POST["name"]) : null;
$company = isset($_POST["company"]) ?  htmlspecialchars($_POST["company"]): null;
$title = isset($_POST["title"]) ? htmlspecialchars($_POST["title"]) : null;
$country = isset($_POST["country"]) ? htmlspecialchars($_POST["country"]) : null;
$email = isset($_POST["email"]) ?  htmlspecialchars($_POST["email"]): null;
$message = isset($_POST["message"]) ?  htmlspecialchars($_POST["message"]): null;
	 
	 
if(!empty($_POST["submited"])){



// check if any of the SPAMBOT criteria are true

	if(preg_match("/bcc:|cc:|multipart|url|Content-Type:/i", implode($_POST))) {
		$spam=true;
	}
	if (preg_match_all("/<a|http:/i", implode($_POST), $out) > 3) {
		$spam=true;
	}
	if(!empty($_POST['emailagain'])){
		$spam = true;
	}
	if(empty($_POST['name'])){
			$error = true ;
	}
		if(empty($_POST['company'])){
			$error = true ;
	}
		
		if(empty($_POST['country'])){
			$error = true ;
	}
	
	// if e-mail is not formatted correctly, show error message
	if(!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $_POST['email'])) {
			$error = true ;
	}
	
	
	if(empty($_POST['message'])){
		$error = true ;
	}
	if($_POST['formtime'] < time()-3600)  {
		$spam=true;
	}


$errorMessage = "Please fill in all the fields.";


if(empty($error) and empty($spam)){
// create and send the email

$emailMessage = "";

$emailMessage .= '<b>Company:</b>' . $company. '<br>';
$emailMessage .= '<b>Name:</b>' . $name. '<br>';
$emailMessage .= '<b>Title:</b>' . $title. '<br>';
$emailMessage .= '<b>Email:</b>' . $email. '<br>';
$emailMessage .= '<b>Country:</b>' . $country. '<br>';


$emailMessage .= '<b>Enquiry:</b>' . $message. '<br><br>';
$newlines   = array("\r\n", "\n", "\r");
$replace = '<br>';
$emailMessage = str_replace($newlines, $replace, $emailMessage);		


	$mail = new PHPMailer();
	$mail->IsHTML(true);
	
	$mail->From     = "my name";
	$mail->FromName = "Some Website";
	$mail->Subject = "Enquiry from Some Website";
	$mail->AddAddress("some@email", "Some Person");
	
	$mail->Body = $emailMessage;
	$mail->Body   = eregi_replace("[\]",'',$mail->Body);
	$mail->AltBody= strip_tags($mail->Body);
	
	if(!$mail->Send()) {
	
		  echo 'Failed to send mail';
	
	} else {
	
		$sentemail = true;
	
	}
}
}

?>
// html stuff in here
   <?php if(empty($sentemail)){ ?>
              
              
      <form id="contactform" name="contactform" method="post" action="index.php">
        <table width="100%" border="0" cellpadding="0" cellspacing="0">
                  <?php if($error){ ?>
<tr>
            <td align="right" valign="top">&nbsp;</td>
            <td valign="top"><span class="errorMessage">
                  <?= $errorMessage ?>
                  </span></td>
          </tr><?php } ?>
          <tr>
            <td width="80" align="left" valign="top"><strong>Company*</strong></td>
            <td valign="top"><input name="company" type="text" id="company" value="<?= $company ?>" /></td>
          </tr>
          <tr>
            <td align="left" valign="top"><strong><span class="highlight">N</span>ame*</strong></td>
            <td valign="top"><input name="name" type="text" id="name" value="<?= $name ?>" /></td>
          </tr>
          <tr>
            <td align="left" valign="top"><strong>Title*</strong></td>
            <td valign="top"><input name="title" type="text" id="title" value="<?= $title ?>" /></td>
          </tr>
          <tr>
            <td align="left" valign="top"><strong><span class="highlight">E</span>mail*</strong></td>
            <td valign="top"><input name="email" type="text" id="email" value="<?= $email ?>" /></td>
          </tr>
          <tr>
            <td align="left" valign="top"><strong>Country*</strong></td>
            <td height="30" valign="top"><input name="country" type="text" id="country" value="<?= $country ?>" /></td>
          </tr>
          
          <tr>
            <td align="left" valign="top"><strong><span class="highlight">Enquiry*</span></strong></td>
            <td valign="top"><textarea name="message" rows="6" id="message"><?= $message ?></textarea></td>
          </tr>
          <tr>
            <td valign="top"><input name="submitted" type="hidden" id="submitted" value="true" />
                <span style="display:none;visibility:hidden;"> </span>
                </p>
                <span style="display:none;visibility:hidden;">
                <label for="emailagain">Do not enter anything in this field as it's designed to stop SPAMBOTS!</label>
                <input type="text" name="emailagain" id="emailagain" value="" />
                <input type="text" name="formtime" value="<?php echo time(); ?>" />
                <input type="text" name="submited" value="1" />
              </span></td>
            <td valign="top">
              <input type="submit" name="button" id="button" value="Submit" />                    </td>
          </tr>
        </table>
      </form>
      <?php } else { ?>
                <p><br />
                  <strong>Thanks. We'll get back to you shortly.</strong><br />
   
         
                </p>
              <?php } ?>
__________________
-------------------------------------------------
World Music World - bringing the World's Folk Music Cultures Together
http://www.worldmusicworld.com/
-------------------------------------------------
Reply With Quote