Jump to content

If submit is click do something


Pauls74462

Recommended Posts

// If submit is click doif(isset($_POST['submit'])) {// Insert fields$query = "INSERT INTO Members (Domain, City, State, Zip, Ip)VALUES('$domain', '$city', '$state', '$zip', '$ip')";mysql_query($query) or die(mysql_error());mysql_close();} else {// Do nothing}
Anyone see what is wrong here? When the page loads it inserts into the db without clicking 'submit'.
Link to comment
Share on other sites

its the very first line of your (executable) code.edit: basically if(isset($_POST['submit'])) == true , which will happen when someone clicks on the submit button, then the code between the curly braces will execute. http://us.php.net/manual/en/function.isset.php

Link to comment
Share on other sites

Why does my page insert in the db without clicking submit?
is that all the code you have? No form that's submitting to that PHP script? edit: based on the title of your post, that's what one would have been led to assume.
Link to comment
Share on other sites

is that all the code you have? No form that's submitting to that PHP script? edit: based on the title of your post, that's what one would have been led to assume.
Here is the long page code. I have move sections around different ways noting worked.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><link rel="SHORTCUT ICON"href="http://softseven-online.com/favicon.ico"><meta name="COPYRIGHT" content="© 2010 bestautosells.com"><title>Bestautosells domain</title></head><body style="background-color: white;"><p align="center"></p><?php// Get db connectionsinclude ("config.php");// Get IP$ip = $_SERVER['REMOTE_ADDR'];// Clear variable$domain = "";$city = "";$state = "";$zip = "";?><!-- Form --><form name="login" method="post" action="AddDomain.php"><tablestyle="background-color: white; text-align: left; margin-left: auto; margin-right: auto; width: 942px; height: 135px;" border="0"><tbody><tr align="center"><td colspan="4" rowspan="1"><b><fontface="Arial Black" size="3">Add Domain</font></b></td></tr><tr><tdstyle="width: 393px; background-color: rgb(255, 255, 255);"><b><fontcolor="black" face="Arial Black" size="3">Domain</font></b></td><td style="width: 172px;"><bigstyle="font-family: Arial Black;"><spanstyle="font-weight: bold;">City</span></big><br></td><tdstyle="width: 43px; font-family: Arial Black; font-weight: bold;">State</td><tdstyle="width: 60px; font-family: Arial Black; font-weight: bold;">Zip</td></tr><tr><td style="width: 393px;"><input size="100"name="domain"></td><td style="width: 172px;"><input size="25"name="city"></td><td style="width: 43px;"><input size="5"name="state" maxlength="100"></td><td style="width: 60px;"><input size="7"name="zip"></td></tr><tr><td colspan="4" rowspan="1"style="width: 393px;"><p align="center"><input name="submit" value="submit" type="submit"></p></td></tr></tbody></table></form><!-- Start PHP<?php// Make a MySQL Connection$link2 = mysql_connect( $dbhost, $dbuser, $dbpass)or die("Could notconnect: ".mysql_error());mysql_select_db($dbname) or die(mysql_error());// set varabels$domain = $_POST['domain'];$city = $_POST['city'];$state = $_POST['state'];$zip = $_POST['zip'];// If submit is click then insertif(isset($_POST['submit'])) {// Insert fields$query = "INSERT INTO Members (Domain, City, State, Zip, Ip)VALUES('$domain', '$city', '$state', '$zip', '$ip')";mysql_query($query) or die(mysql_error());mysql_close();} else {// Do nothing in PHP}// Quit PHP?><br><center><font size="5" face="Arial Black"><b>Your IP address is being logged!</center><br><center>If you have authority to be here we are glad do have you as a member.</center><br><center>If your not authorized to be here we will find you.</center><br><center>Your IP is: <?php echo $ip; ?></center></b></font></body></html>
Link to comment
Share on other sites

so you're saying when you load up that page records are being inserted into the database without having to click the submit button? :)what values are being inserted into the table?

Link to comment
Share on other sites

How do you know data is actually being inserted into the database? There might be an error triggered.

Link to comment
Share on other sites

I meant the table in the database. What I'm trying to get at is how do you know the script is even working, let alone working without having to hit the submit button. Like mentioned above, you should really be checking for errors. echo variables and the such throughout your script is a good way to track and make sure things are doing what they're supposed to be doing.
Link to comment
Share on other sites

I meant the table in the database. What I'm trying to get at is how do you know the script is even working, let alone working without having to hit the submit button. Like mentioned above, you should really be checking for errors. echo variables and the such throughout your script is a good way to track and make sure things are doing what they're supposed to be doing.
It does insert information in the db if you add it.How do I send you a PM here, I'll add you to the db so you can enter a url.
Link to comment
Share on other sites

I recommend querying your database from the command-line. I've just tested the script on my development machine (Apache 2, PHP 5.3.2) and everything works fine.

Link to comment
Share on other sites

I recommend querying your database from the command-line. I've just tested the script on my development machine (Apache 2, PHP 5.3.2) and everything works fine.
did you reload the page, that is when it does a double entry.
Link to comment
Share on other sites

Does it say anything about information resending? If yes, this is what causes your error, because you're trying to access the page which has all the info stored in the $_POST super-global, therefore believing the form was actually submitted.

Link to comment
Share on other sites

Does it say anything about information resending? If yes, this is what causes your error, because you're trying to access the page which has all the info stored in the $_POST super-global, therefore believing the form was actually submitted.
No message about resending. I just submits it as if you pressed 'submit'. U know how to fix?
Link to comment
Share on other sites

Reloading the page is an entirely different issue than just detecting if the submit button was pressed. When you refresh the page you do resubmit the form also, that's why the code is working like it should. If you don't want them to resubmit the form when they refresh the page, then after you add the item to the database you need to redirect. You can redirect to the same page if you want, but the redirect will make it so that if they refresh the page it will not resubmit the old form data.

Link to comment
Share on other sites

Reloading the page is an entirely different issue than just detecting if the submit button was pressed. When you refresh the page you do resubmit the form also, that's why the code is working like it should. If you don't want them to resubmit the form when they refresh the page, then after you add the item to the database you need to redirect. You can redirect to the same page if you want, but the redirect will make it so that if they refresh the page it will not resubmit the old form data.
What is the redirect command after you submit the form?
Link to comment
Share on other sites

Issue a location header:

header("location:blah.php");exit();

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...