gaza165 Posted September 29, 2008 Share Posted September 29, 2008 a user is submitting a comment to the database what is the best way to use for form validation this is what i currently have implemented but because it is not redirecting to another page and coming back, if the user refreshes the page, it will send the post again. <?php // basic form and form processing code - // condition the inputs and setup default values - $submitted = isset($_POST['submit']) ? $_POST['submit'] : FALSE; // was the form submitted? $name = isset($_POST['name']) ? $_POST['name'] : ""; // condition the form's name field $comments = isset($_POST['comments']) ? $_POST['comments'] : ""; // condition the form's name field if($submitted) { include ('dbconnect/dbconnect.php'); $form_error = array(); // array to hold any form validation errors // validate the form data here (set elements in $form_error to hold error messages) if(empty($name)) { $form_error[] = "Please fill in the name"; } if(empty($comments)) { $form_error[] = "Please fill in the comments"; } // if there were no form validation errors, use the data that was submitted if(empty($form_error)) { // do something with the data here $result = "INSERT INTO public_comments (name,message) values ('$name','$comments')"; $sql = mysql_query($result); } } // display the form if it has not been submitted or there are form validation errors If(!$submitted || !empty($form_error)) { // check for and display any form validation errors if(!empty($form_error)) { foreach($form_error as $error) { echo "$error<br />"; } } } // display the form, with any previously submitted values ?> <div id="contactform"> <a name="sendmessage"> <form action="#showcomments" method="POST"> <p class="input">Name:<input type="text" name="name" id="name" value="<?php echo $name; ?>" /></p> <p class="input">Comments:<textarea cols="55" rows="5" id="comments" name="comments"></textarea></p> <input type="submit" id="send_message" name="submit" value="Send Message" class="contactbutton"/> </form> </a> </div> Link to comment https://forums.phpfreaks.com/topic/126350-form-validation/ Share on other sites More sharing options...
gaza165 Posted September 29, 2008 Author Share Posted September 29, 2008 someone must have a response to this... Garry Link to comment https://forums.phpfreaks.com/topic/126350-form-validation/#findComment-653418 Share on other sites More sharing options...
monkeytooth Posted September 30, 2008 Share Posted September 30, 2008 Well I would suggest unset().. since your script already does a check to see if the form was submitted and does what it has to accordingly from there.. I would say after the data has been input into the database.. do unset($_POST['submit']); Add it here.. // if there were no form validation errors, use the data that was submitted if(empty($form_error)) { // do something with the data here $result = "INSERT INTO public_comments (name,message) values ('$name','$comments')"; $sql = mysql_query($result); unset($_POST['submit']); //New Line } } Link to comment https://forums.phpfreaks.com/topic/126350-form-validation/#findComment-653500 Share on other sites More sharing options...
monkeytooth Posted September 30, 2008 Share Posted September 30, 2008 that will remove the set value behind what defines your form as submited.. or should atleast, so this way it wont exist if the page is refreshed.. Link to comment https://forums.phpfreaks.com/topic/126350-form-validation/#findComment-653501 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.