Jump to content

Clearing Form So to not submit to MySQL twice.


nepzap2

Recommended Posts

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');

?>

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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');

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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');

?>

Link to comment
Share on other sites

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');
?>

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.