Jump to content

How do I clear the form fields AFTER it has been submitted ??


spacepoet

Recommended Posts

Hi:

 

I am using this code for my contact us feedback form:

<?php
$error = NULL;
$myDate = NULL;
$FullName = NULL;
$Address = NULL;
$City  = NULL;
$State = NULL;
$Zip = NULL;
$Phone = NULL;
$Email = NULL;
$Website = NULL;
$Comments = NULL;

if(isset($_POST['submit'])) {

$myDate = $_POST['myDate'];
$FullName = $_POST['FullName'];
$Address = $_POST['Address'];
$City = $_POST['City'];
$State = $_POST['State'];
$Zip = $_POST['Zip'];
$Phone = $_POST['Phone'];
$Email = $_POST['Email'];
$Website = $_POST['Website'];
$Comments = $_POST['Comments'];

if(empty($FullName)) {
   $error .= '-- Enter your Full Name. <br />';
}

if(empty($Email)) {
   $error .= '-- Enter your Email. <br />';
}

if($error == NULL) {

$sql = sprintf("INSERT INTO myContactData(myDate,FullName,Address,City,State,Zip,Phone,Email,Website,Comments) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",

                        mysql_real_escape_string($myDate),
                        mysql_real_escape_string($FullName),
                        mysql_real_escape_string($Address),
                        mysql_real_escape_string($City),
                        mysql_real_escape_string($State),
                        mysql_real_escape_string($Zip),
                        mysql_real_escape_string($Phone),
                        mysql_real_escape_string($Email),
                        mysql_real_escape_string($Website),
                        mysql_real_escape_string($Comments));
                        
  if(mysql_query($sql)) {
  
    $error .= 'Thank you for contacting us.';
    
mail( "d@direct.com", "Contact Request",

"Date Sent: $myDate\n Full Name: $FullName\n Address: $Address\n City: $City\n State: $State\n Zip: $Zip\n Phone: $Phone\n Email: $Email\n Website: $Website\n Comments: $Comments\n",
"From: $Email" );
  }
  else {
    $error .= 'There was an error in our Database, please Try again!';
  }
}
}
echo '<span class="textError">' . $error . '</span>';

?>

			<form name="myform" action="" method="post">
			<input type="hidden" name="myDate" size="45" maxlength="50" value="<?php echo date("F j, Y"); ?>" />

				<div id="tableFormDiv">

					<fieldset><span class="floatLeftFormWidth"><span class="textErrorItalic">* - Required</span></span> <span class="floatFormLeft"> </span></fieldset>
					<fieldset><span class="floatLeftFormWidth"><span class="textErrorItalic">*</span> Full Name:</span> <span class="floatFormLeft"><input type="text" name="FullName" size="45" maxlength="50" value="<?php echo $FullName; ?>" /></span></fieldset>
					<fieldset><span class="floatLeftFormWidth">Address:</span> <span class="floatFormLeft"><input type="text" name="Address" size="45" maxlength="50" value="<?php echo $Address; ?>" /></span></fieldset>
					<fieldset><span class="floatLeftFormWidth">City:</span> <span class="floatFormLeft"><input type="text" name="City" size="45" maxlength="50" value="<?php echo $City; ?>" /></span></fieldset>
					<fieldset><span class="floatLeftFormWidth">State:</span> <span class="floatFormLeft"><input type="text" name="State" size="45" maxlength="50" value="<?php echo $State; ?>" /></span></fieldset>
					<fieldset><span class="floatLeftFormWidth">Zip:</span> <span class="floatFormLeft"><input type="text" name="Zip" size="45" maxlength="50" value="<?php echo $Zip; ?>" /></span></fieldset>
					<fieldset><span class="floatLeftFormWidth">Phone:</span> <span class="floatFormLeft"><input type="text" name="Phone" size="45" maxlength="50" value="<?php echo $Phone; ?>" /></span></fieldset>
					<fieldset><span class="floatLeftFormWidth"><span class="textErrorItalic">*</span> Email:</span> <span class="floatFormLeft"><input type="text" name="Email" size="45" maxlength="50" value="<?php echo $Email; ?>" /></span></fieldset>
					<fieldset><span class="floatLeftFormWidth">Website:</span> <span class="floatFormLeft"><input type="text" name="Website" size="45" maxlength="50" value="<?php echo $Website; ?>" /></span></fieldset>
					<fieldset><span class="floatLeftFormWidth">Comments:</span> <span class="floatFormLeft"><textarea name="Comments" cols="40" rows="10"><?php echo $Comments; ?></textarea></span></fieldset>
				</div>	
					<input type="submit" name="submit" value="Submit" class="submitButton" /><br />
			</form>

 

 

I the only thing I can't figure out is, how do I clear the form fields AFTER the form has been submitted and the message

"Thank you for contacting us." appears ??

 

I haven't been able to figure it out with JavaScript/PHP, so I posted my original code in hopes that someone will have an idea.

 

Anyone?

 

Thanks!

 

 

 

 

Link to comment
Share on other sites

I may be way off here but I would think that since your sending the post info forward, that you would have it send to one page that sends out the email part and have that one redirect post back to the original page with blank post variables. So

 

Page A --> Page B --> Page A

 

Page B would just but the script to send the email and then dump the data and go back to Page A.

Link to comment
Share on other sites

well I suppose one way you could do it is inside your condition:

if($error == NULL) {
// your other stuff
}

 

unset() all of the relevant variables ($myDate, etc...) or assign "" to all of them.

 

unset($myDate); // do this for all of them

// or 

$myDate = ""; // do this for all of them

 

assigning an empty string value would be easier because if you unset them, you will generate a NOTICE error for using undefined variables in your form later on.  It wouldn't break anything but if you aren't suppressing notices you may see a NOTICE error output from doing so.  Which you can get rid of by altering the variables in the form code to look more like (have to do it for each form field):

 

<?php echo (isset($Address))? $Address : ""; ?>

Link to comment
Share on other sites

Excellent!

 

Thank you very much, it's working like a charm!

 

On another note:

Are you any good with how to upload and send images with a feedback form?

 

I'm trying to add this to another form but am a bit lost - haven't made one of these before with PHP and could use some direction ...

Link to comment
Share on other sites

you should start a new topic for that, and you should give some more details about what "upload and send" means to you.  Do you mean for a user to include an image in a feedback form and it gets uploaded and...then what?  It gets sent to some email address? It gets stored on your server somewhere?  But make a new topic.

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.