spacepoet Posted March 17, 2011 Share Posted March 17, 2011 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( "[email protected]", "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! Quote Link to comment https://forums.phpfreaks.com/topic/230938-how-do-i-clear-the-form-fields-after-it-has-been-submitted/ Share on other sites More sharing options...
Sleeper Posted March 17, 2011 Share Posted March 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230938-how-do-i-clear-the-form-fields-after-it-has-been-submitted/#findComment-1188776 Share on other sites More sharing options...
.josh Posted March 17, 2011 Share Posted March 17, 2011 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 : ""; ?> Quote Link to comment https://forums.phpfreaks.com/topic/230938-how-do-i-clear-the-form-fields-after-it-has-been-submitted/#findComment-1188777 Share on other sites More sharing options...
spacepoet Posted March 17, 2011 Author Share Posted March 17, 2011 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 ... Quote Link to comment https://forums.phpfreaks.com/topic/230938-how-do-i-clear-the-form-fields-after-it-has-been-submitted/#findComment-1188789 Share on other sites More sharing options...
.josh Posted March 17, 2011 Share Posted March 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230938-how-do-i-clear-the-form-fields-after-it-has-been-submitted/#findComment-1188845 Share on other sites More sharing options...
spacepoet Posted March 17, 2011 Author Share Posted March 17, 2011 Hi there: Yes, will do that now - new topic. Hope you can look at it - I lost on this one... Quote Link to comment https://forums.phpfreaks.com/topic/230938-how-do-i-clear-the-form-fields-after-it-has-been-submitted/#findComment-1188851 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.