| | |
Form with an if...loop
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2008
Posts: 13
Reputation:
Solved Threads: 0
Hi all,
I have a two-part form intended to replicate the output from a toolbar, with the aim of sending data to a database.
So the data is generated by the following code (sim1.php):
this is then sent to toolbar_process.php:
The aim is that the data should either come from sim1.php, or if the user has landed on the second page without going to sim1, the data is sent via similar form.
Both times the origin is added to the row.
Sadly, the entries are not being recorded on mysql.
I'm sure it's almost there... Can anyone help?
I have a two-part form intended to replicate the output from a toolbar, with the aim of sending data to a database.
So the data is generated by the following code (sim1.php):
PHP Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Start</title> <link href="toolbar_popup.css" rel="stylesheet" type="text/css" /> </head> <body> <form method="post" action="toolbar_process.php"> <input type="text" name="tbar_site"/> <br /> <select name="tbar_type" size="2"> <option value="h">housing</option> <option value="o">other</option> </select> <br /> <input type="hidden" name="origin" value="tbar"> <input type="submit" value="Enter details!"/> </form> </body> </html>
PHP Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> <!-- Begin function popUp(URL) { eval("page" + id + " = window.open(URL, '" + id + "','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=200,height=300');"); } // End --> </script> <title>Toolbar Process</title> <link href="toolbar_popup.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper"> <div id="text"> <?php if (!isset($origin)) { require_once('connect.php'); if ($_SERVER['REQUEST_METHOD'] == "POST") { #set variables, stripslashes $type = addslashes($_POST["tbar_type"]); $site = addslashes($_POST["tbar_site"]); # setup SQL statement $sql = "INSERT INTO `wkho_users`.`web_dir` (`site`, `type`,`origin`) VALUES ('$site','$type','$origin')"; #execute SQL statement #$result = mysql_query($sql); # check for error if (mysql_error()) { print "Database ERROR1: " . mysql_error(); } //message for user echo "<h1>Thanks!</h1>Your toolbar entry: ".$site.", was submitted."; } $origin = NULL; } ?> <h2> Got more?</h2> <form method="post" action="toolbar_process.php"> <input type="text" name="site"/> <br /> <select name="type" size="2"> <option value="h">housing</option> <option value="o">other</option> </select> <br /> <input type="submit" value="Enter details!"/> </form> </div> <?php require_once('connect.php'); if ($_SERVER['REQUEST_METHOD'] == "POST") { #set variables, stripslashes $type1 = addslashes($_POST["type"]); $site1 = addslashes($_POST["site"]); $origin1 = "page"; # setup SQL statement $sql = "INSERT INTO `wkho_users`.`web_dir` (`site`, `type`,`origin`) VALUES ('$site1','$type1','$origin1')"; #execute SQL statement #$result = mysql_query($sql); # check for error if (mysql_error()) { print "Database ERROR1: " . mysql_error(); } //message for user echo "<h1>Thanks!</h1>Your entry was submitted."; } ?> </div> </div> </body> </html>
The aim is that the data should either come from sim1.php, or if the user has landed on the second page without going to sim1, the data is sent via similar form.
Both times the origin is added to the row.
Sadly, the entries are not being recorded on mysql.
I'm sure it's almost there... Can anyone help?
Try uncommenting line 33 where you have commented the function mysql_query(). Also try replacing the $_SERVER['REQUEST_METHOD'] with the following:
php Syntax (Toggle Plain Text)
if (isset($_POST) && !empty($_POST)) {
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
My favourite PC. - MacGyver Fan
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*` My favourite PC. - MacGyver Fan
•
•
Join Date: Jun 2008
Posts: 13
Reputation:
Solved Threads: 0
Thanks cwarn.
I've altered the code as you suggested:
The problem now is that I have two entries when submit is pressed (see attachment).
So, how do I enter just one new row/fire the sql once? And only show the relevant message...sheesh, I know that's a lot to ask!
I've altered the code as you suggested:
PHP Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Toolbar Process</title> <link href="toolbar_popup.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper"> <div id="text"> <?php if (!isset($origin)) { require_once('connect.php'); if (isset($_POST) && !empty($_POST)) { #set variables, stripslashes $type = addslashes($_POST["tbar_type"]); $site = addslashes($_POST["tbar_site"]); $origin1 = "tbar"; # setup SQL statement $sql = "INSERT INTO `wkho_users`.`web_dir` (`site`, `type`,`origin`) VALUES ('$site','$type','$origin')"; #execute SQL statement $result = mysql_query($sql); # check for error if (mysql_error()) { print "Database ERROR1: " . mysql_error(); } //message for user echo "<h1>Thanks!</h1>Your toolbar entry: ".$site.", was submitted."; } $origin = NULL; } ?> <h2> Got more?</h2> <form method="post" action="toolbar_process.php"> <input type="text" name="site"/> <br /> <select name="type" size="2"> <option value="h">housing</option> <option value="o">other</option> </select> <br /> <input type="submit" value="Enter details!"/> </form> </div> <?php require_once('connect.php'); if ($_SERVER['REQUEST_METHOD'] == "POST") { #set variables, stripslashes $type1 = addslashes($_POST["type"]); $site1 = addslashes($_POST["site"]); $origin1 = "page"; # setup SQL statement $sql = "INSERT INTO `wkho_users`.`web_dir` (`site`, `type`,`origin`) VALUES ('$site1','$type1','$origin1')"; #execute SQL statement $result = mysql_query($sql); # check for error if (mysql_error()) { print "Database ERROR1: " . mysql_error(); } //message for user echo "<h1>Thanks!</h1>Your entry: ".$site1.", was submitted."; } ?> </div> <h2>What now?</h2> Would you like to visit the <a href="index.php">wkho.me.uk homepage</a>? <br /> Or add to the <a href="directory_entry.php">Directory</a>? </div> </body> </html>
So, how do I enter just one new row/fire the sql once? And only show the relevant message...sheesh, I know that's a lot to ask!
Try this:
php Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Toolbar Process</title> <link href="toolbar_popup.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper"> <div id="text"> <?php if (!isset($origin)) { require_once('connect.php'); if (isset($_POST) && !empty($_POST)) { #set variables, stripslashes $type = mysql_real_escape_string($_POST["tbar_type"]); $site = mysql_real_escape_string($_POST["tbar_site"]); $origin1 = "tbar"; # setup SQL statement $sql = "INSERT INTO `wkho_users`.`web_dir` (`site`, `type`,`origin`) VALUES ('$site','$type','$origin')"; #execute SQL statement $result = mysql_query($sql); # check for error if (mysql_error()) { print "Database ERROR1: " . mysql_error(); } //message for user echo "<h1>Thanks!</h1>Your toolbar entry: ".$site.", was submitted."; } $origin = NULL; } ?> <h2> Got more?</h2> <form method="post" action="toolbar_process.php"> <input type="text" name="site"/> <br /> <select name="type" size="2"> <option value="h">housing</option> <option value="o">other</option> </select> <br /> <input type="submit" value="Enter details!"/> </form> </div> <?php require_once('connect.php'); if (isset($_POST) && !empty($_POST)) { #set variables, stripslashes $type1 = mysql_real_escape_string($_POST["type"]); $site1 = mysql_real_escape_string($_POST["site"]); $origin1 = "page"; # setup SQL statement $sql = "INSERT INTO `wkho_users`.`web_dir` (`site`, `type`,`origin`) VALUES ('$site1','$type1','$origin1')"; #execute SQL statement $result = mysql_query($sql); # check for error if (mysql_error()) { print "Database ERROR1: " . mysql_error(); } //message for user echo "<h1>Thanks!</h1>Your entry: ".$site1.", was submitted."; } ?> </div> <h2>What now?</h2> Would you like to visit the <a href="index.php">wkho.me.uk homepage</a>? <br /> Or add to the <a href="directory_entry.php">Directory</a>? </div> </body> </html>
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
My favourite PC. - MacGyver Fan
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*` My favourite PC. - MacGyver Fan
![]() |
Similar Threads
- updating multiple rows with one form (PHP)
- Trying to access controls on a form from a method (C#)
- Syntax to use a variable in a for loop (Shell Scripting)
- form name and loop (PHP)
- Print without using any loop (C++)
- Update multiple rows from loop? (PHP)
- problem with loop (C++)
Other Threads in the PHP Forum
- Previous Thread: Zip Codes
- Next Thread: Web Crwaler : File Searching
| Thread Tools | Search this Thread |
access ajax api applicaions array asp beginner body broken c# check checkbox class cms css curl data database display dropdownlist email error errorlog events execute file files flash form forms function google html ibm image include ingres insert integration java javascript jquery keywords lamp libcurl limit link linux login longisland mail malfunction menu multiple mysql mysqlindex mysqlquery news oop oracle parsing paypal php phpmyadmin post programming projectmanagement query radio remote script search security select server session sms soap software source spam sql sqlserver sun table thread training tutorial upload validation vbulletin video web webdesign windows wpf xml xss youtube zend







