iEntry 10th Anniversary Forum Rules Search
WebProWorld
Register FAQ Calendar Mark Forums Read
Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Share Thread: & Tags

Share Thread:

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-12-2008, 01:15 AM
WebProWorld New Member
 
Join Date: Nov 2008
Posts: 2
overflo RepRank 0
Default PHP Sticky fields

Hi.I have the following code (a function) to display and process a html form. What I need is for it to have "sticky fields" so that if there is an error and the message is displayed, when they are returned to the form the details that were correctly input are still there.

Code:
function show_page1(){
$action = $_SERVER['PHP_SELF']."?chall=page1";;
		
if(!isset($_POST['submit'])){
$content = "<div id='form'>
			 <form id='form1' name='form1' method='post' action=$action>
			  <table width='303' height='181' border='0'>
			   <tr>
			    <th width='105'>Name:</th>
				<td width='196'><input name='name' type='text' id='name' size='30'  /></td>
			  </tr>
			  <tr>
			   <th width='105'>Suburb:</th>
			   <td width='196'><input name='suburb' type='text' id='suburb' size='30'  /></td>
			  </tr>
			  <tr>
			   <th scope='row'><div align='left'></div></th>
				<td><div align='left'>
				 <input name='submit' type='submit' value='Submit'>
			    </div></td>
			   </tr>
										
			 </table>
			</form>
		   </div>";
}else{
	if($_POST['name']==""){
		$message .="<p>Please enter a name</p>";
	}else{
		$name = mysql_real_escape_string($_POST['name']);
					}
	if($_POST['suburb']==""){
		$message .="<p>Please enter a Suburb</p>";
	}else{
		$name = mysql_real_escape_string($_POST['suburb']);
	}
					
	if(!$message==""){
		$content .=  $message . "<p>Please click <a href=$action>here</a> to go back and try again</p>" ;
	}else{
		//sql insert queries go here
	}
						
}
}
any suggestions greatly appreciated
Reply With Quote
  #2 (permalink)  
Old 11-12-2008, 08:32 PM
WebProWorld Member
 
Join Date: Oct 2005
Posts: 40
niggles RepRank 1
Default Re: PHP Sticky fields

sorry - duplicated post....
__________________
-------------------------------------------------
World Music World - bringing the World's Folk Music Cultures Together
http://www.worldmusicworld.com/
-------------------------------------------------

Last edited by niggles; 11-12-2008 at 08:34 PM.
Reply With Quote
  #3 (permalink)  
Old 11-12-2008, 08:32 PM
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
  #4 (permalink)  
Old 11-13-2008, 01:37 AM
WebProWorld New Member
 
Join Date: Nov 2008
Posts: 2
overflo RepRank 0
Default Re: PHP Sticky fields

Thanks niggles

Will give it a go

Cheers
Reply With Quote
  #5 (permalink)  
Old 11-13-2008, 05:08 AM
fulleffect's Avatar
WebProWorld Pro
 
Join Date: Jun 2008
Location: Northeast UK
Posts: 107
fulleffect RepRank 1
Default Re: PHP Sticky fields

I usually just do something like this........


Code:
<?

//check the submit button has been pressed
if($_POST)
{

//check fields arent blank
if($_POST['name']!="" &&
$_POST['telephone']!="")
{

//do further validation and upon success, mail
etc etc
mail();
unset($_POST);
$msg = 'Thank you, your details have been submitted';

}
else
{
$msg = 'Please ensure fields arent blank';
}


}
?>

<html>

<?=$msg?>

<form method="post" action="<?=$_SERVER['REQUEST_URI'];?>">

<input type="text" name="name" id="name" value="<?=$_POST['name']?>" /><br />
<input type="text" name="telephone" id="telephone" value="<?=$_POST['telephone']?>" /><br />
<input type="submit" name="submit" value="Submit" />

</form>

</html>
__________________

Reply With Quote
  #6 (permalink)  
Old 11-13-2008, 05:09 AM
fulleffect's Avatar
WebProWorld Pro
 
Join Date: Jun 2008
Location: Northeast UK
Posts: 107
fulleffect RepRank 1
Default Re: PHP Sticky fields

Whooops, duplicate post.
__________________

Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Your Eyes - Puzzle Table Fields freetraff Graphics & Design Discussion Forum 1 12-10-2007 07:35 PM
Name of file relevant in some non competitive fields begabloomers Search Engine Optimization Forum 0 09-04-2006 03:11 PM
multipage HTML form with hidden fields webgurl Web Programming Discussion Forum 1 03-15-2006 08:54 AM
Mandatory fields in a form (formmail) aventvoy Web Programming Discussion Forum 4 11-17-2005 01:45 AM
cannot get 12 fields back, only 3 bufhal Database Discussion Forum 9 05-11-2004 05:14 PM


All times are GMT -4. The time now is 08:30 AM.



Search Engine Optimization by vBSEO 3.3.0