|
|
||||||
|
||||||
| Index Link To US Private Messages Archive FAQ RSS | ||||||
| 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
|
||||
|
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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
}
}
}
|
|
|||
|
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. |
|
|||
|
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
}
Code:
if(isset($_POST['submit'])){
// process the post variables
// if all is fine...
$sucess = true;
}
* In the form, change the fields so they read like this: Code:
<input name='name' type='text' id='name' size='30' value='<?= $name ?>' /> 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"> </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/ ------------------------------------------------- |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
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 |
|
WebProWorld |
Advertise |
Contact Us |
About |
Forum Rules |
MVP's |
Archive |
Newsletter Archive |
Top |
WebProNews
WebProWorld is an iEntry, Inc. ® site - © 2009 All Rights Reserved Privacy Policy and Legal iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509 |