nepzap2 Posted March 12, 2008 Share Posted March 12, 2008 Hello Everyone. I'm slowly threading and getting more comfortable with PHP/MySQL. I'd first like to thank you all for answering my past questions. But like a true Noob I have another question. I have created a simple form that inserts data into a MySQL table. Simple. The problem is if I hit my Reset button and then hit my Submit button again it creates an emty tuple in my databases's table. I have tried setting the values of the form fields to value="" and nothing, even a simple javascript that supposedly clears it but all it clears is the fields them selves. I think the values are still saved in the $_POST array. Below is my form and my php file that process the form. I have omitted the database info, you guys understand. Once again, any help is extremely appreciated Thanks, <script> function clearForms() { var i; for (i = 0; (i < document.forms.length); i++) { document.forms[i].reset(); } } </script> </head> <body onLoad="clearForms()" onUnload="clearForms()"> <form action="add_contact.php" method="post"> First Name: <input type="text" name="first_name"/> Last Name: <input type="text" name="last_name"/> Phone: <input type="text" name="phone"/> Mobile: <input type="text" name="mobile"/> E-mail: <input type="text" name="email"/> Country: <input type="text" name="country"/> <input type="Submit"/> <input type="reset" value="Reset" name="Reset"> </form> </body> <?php $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $phone = $_POST['phone']; $mobile = $_POST['mobile']; $email = $_POST['email']; $country = $_POST['country']; mysql_connect("localhost", "", "") or die(mysql_error()); @mysql_select_db("testinput") or die(mysql_error()); $query = "INSERT INTO addressbook (first_name,last_name,phone,mobile,email,country) VALUES ( '$first_name', '$last_name', '$phone', '$mobile', '$email', '$country')"; //echo "The form data was successfully added to your database."; //echo "<br><br><a href=\"javascript:history.back();\">Add Another Entry</a>"; mysql_query($query); mysql_close(); header('Location: form.html'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/95866-clearing-form-so-to-not-submit-to-mysql-twice/ Share on other sites More sharing options...
Lashiec Posted March 12, 2008 Share Posted March 12, 2008 Just needs some error checking... When your information gets passed into the page you can do $data = trim($_POST['data']) and that will remove and extra white spaces from the beginning and end of whatever was typed into the box... If the person typed a lot of spaces in there with no text it will clear them out. Then, you'll want to do something like: if(!$data) $error[] = 'Please enter something in the box...'; That will check to see if anything is in the variable $data, if there isn't it will return true to the if statement and shove "Please enter..." into the variable $error. Then, before you enter data into the mysql database you wrap that code in an if statement like... if(!isset($error)) { //code for mysql } Quote Link to comment https://forums.phpfreaks.com/topic/95866-clearing-form-so-to-not-submit-to-mysql-twice/#findComment-490836 Share on other sites More sharing options...
nepzap2 Posted March 13, 2008 Author Share Posted March 13, 2008 Lashiec thank you for answering my question... but I don't think I understand you quite right. Am I supposed to modify my code like so below? Can you please clarify. I do understand about the error checking... but I'm kind of lost as to what your modifications are trying to do. Thanks <?php $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $phone = $_POST['phone']; $mobile = $_POST['mobile']; $email = $_POST['email']; $country = $_POST['country']; $data = trim($_POST['data']); if(!$data) $error[] = 'Please enter something in the box...'; if(!isset($error)){ mysql_connect("localhost", "root", "napibalu") or die(mysql_error()); @mysql_select_db("testinput") or die(mysql_error()); $query = "INSERT INTO addressbook (first_name,last_name,phone,mobile,email,country) VALUES ( '$first_name', '$last_name', '$phone', '$mobile', '$email', '$country')"; //echo "The form data was successfully added to your database."; //echo "<br><br><a href=\"javascript:history.back();\">Add Another Entry</a>"; mysql_query($query); } mysql_close(); header('Location: form.html'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/95866-clearing-form-so-to-not-submit-to-mysql-twice/#findComment-490903 Share on other sites More sharing options...
teng84 Posted March 13, 2008 Share Posted March 13, 2008 sample.. check if your fields has value before inserting in your db if(isset($_POST['name'])&& isset($_POST['name']) && isset($_POST['name'])){ // insert here }else{ //tell the user fill the from completely } Quote Link to comment https://forums.phpfreaks.com/topic/95866-clearing-form-so-to-not-submit-to-mysql-twice/#findComment-490918 Share on other sites More sharing options...
Lashiec Posted March 13, 2008 Share Posted March 13, 2008 You would have to do... $first_name = trim($first_name); For each of your variables, so: first_name, last_name, phone, mobile, email, and country. Then for each of these you'd probably want something like... if(!$first_name) $errors[] = 'Please enter a first name.'; For each of your variables listed above. Then the if(!isset($errors)) with your mysql code in the if statement. If any of the fields are blank (or the ones you don't want blank) then it will add a message to the $errors variable, if the variable is set then it won't do the mysql add, and you can output those errors to the user. Quote Link to comment https://forums.phpfreaks.com/topic/95866-clearing-form-so-to-not-submit-to-mysql-twice/#findComment-490960 Share on other sites More sharing options...
nepzap2 Posted March 13, 2008 Author Share Posted March 13, 2008 Thanks for the help. Maybe I'm not making my self clear... I'm new to this so please bare with me. After I enter information my input text fields go empty, but they are still set, so when I click submit even if there is nothing there it creates an empty row in my database. From what I'm seeing there are two things that I need to do: 1. Clear fields after submitting 2. check that the fields are set and that they are not empty Is this correct? I've tried several of the solutions you guys have offered but none of them have worked, It may be me who is not implementing the solutions properly. Here is my original code Any help is extremely appreciated Thanks, <?php $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $phone = $_POST['phone']; $mobile = $_POST['mobile']; $email = $_POST['email']; $country = $_POST['country']; mysql_connect("localhost", "root", "napibalu") or die(mysql_error()); @mysql_select_db("testinput") or die(mysql_error()); $query = "INSERT INTO addressbook (first_name,last_name,phone,mobile,email,country) VALUES ( '$first_name', '$last_name', '$phone', '$mobile', '$email', '$country')"; //echo "The form data was successfully added to your database."; //echo "<br><br><a href=\"javascript:history.back();\">Add Another Entry</a>"; mysql_query($query); mysql_close(); header('Location: form.html'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/95866-clearing-form-so-to-not-submit-to-mysql-twice/#findComment-490972 Share on other sites More sharing options...
nepzap2 Posted March 13, 2008 Author Share Posted March 13, 2008 Lashiec Thank you, Now it makes sence! And it Works! Quote Link to comment https://forums.phpfreaks.com/topic/95866-clearing-form-so-to-not-submit-to-mysql-twice/#findComment-490981 Share on other sites More sharing options...
ikmyer Posted March 13, 2008 Share Posted March 13, 2008 to solve that refresh problem on forms i just do a header to the same page... in a sense forwarding them onto the same or current page. When this takes place the POST data will be dropped. Quote Link to comment https://forums.phpfreaks.com/topic/95866-clearing-form-so-to-not-submit-to-mysql-twice/#findComment-490988 Share on other sites More sharing options...
nepzap2 Posted March 13, 2008 Author Share Posted March 13, 2008 Guys I apologize. But the modifications are not working quite right. here is the code. Any help is extremely appreciated <?php $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $phone = $_POST['phone']; $mobile = $_POST['mobile']; $email = $_POST['email']; $country = $_POST['country']; $first_name = trim($first_name); $last_name = trim($last_name); $phone = trim($phone); $mobile = trim($mobile); $email = trim($email); $country = trim($country); if(!$first_name) $errors[] = 'Please enter a first name.'; if(!$last_name) $errors[] = 'Please enter a last name.'; if(!$phone) $errors[] = 'Please enter a phone.'; if(!$mobile) $errors[] = 'Please enter a mobile phone.'; if(!$email) $errors[] = 'Please enter a email.'; if(!$country) $errors[] = 'Please enter a country.'; if(!isset($errors)) { mysql_connect("localhost", "root", "napibalu") or die(mysql_error()); @mysql_select_db("testinput") or die(mysql_error()); $query = "INSERT INTO addressbook (first_name,last_name,phone,mobile,email,country) VALUES ( '$first_name', '$last_name', '$phone', '$mobile', '$email', '$country')"; //echo "The form data was successfully added to your database."; //echo "<br><br><a href=\"javascript:history.back();\">Add Another Entry</a>"; mysql_query($query); mysql_close(); } header('Location: form.html'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/95866-clearing-form-so-to-not-submit-to-mysql-twice/#findComment-491256 Share on other sites More sharing options...
BlueSkyIS Posted March 13, 2008 Share Posted March 13, 2008 But the modifications are not working quite right. please clarify. Quote Link to comment https://forums.phpfreaks.com/topic/95866-clearing-form-so-to-not-submit-to-mysql-twice/#findComment-491270 Share on other sites More sharing options...
nepzap2 Posted March 13, 2008 Author Share Posted March 13, 2008 BlueSkyIS Where you replying to me and didn't finish you reply or were you just mocking me? Any help is appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/95866-clearing-form-so-to-not-submit-to-mysql-twice/#findComment-491476 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.