PHP - Insert Multiple Forms Into Mysql
Hi all
I am trying to take the data from a form and add into a mySQL table. I have multiple forms on one page that uses a loop: Code: [Select] <?php $textqty = $showbasket['qty']; for ($i = 1; $i <= $textqty ; $i++) { ?> <form id="texts" name="texts" method="post" action=""> <input name="mainqty" type="hidden" value="<?php echo $textqty; ?>" /> <input name="productid" type="hidden" value="<?php echo $showbasket['productid']; ?>" /> <input name="productqtyid" type="hidden" value="<?php echo $i; ?>" /> <input name="productsize" type="hidden" value="<?php echo $showbasket['size']; ?>" /> Text: <input name="text_<?php echo $i; ?>" type="text" value="<?php echo $showtext['text']; ?>" size="35" maxlength="250" /> Colour: <select name="colour"> <?php $getcolours = mysql_query(" SELECT * FROM text_colours ORDER BY id ASC"); while($showcolours = mysql_fetch_array($getcolours)) { ?> <option value="<?php echo $showcolours['colour']; ?>"><?php echo $showcolours['colour']; ?></option> <?php } ?> </select> No. Characters: <br /> <?php } ?> <input name="update" type="submit" id="update" value="Update" /> </form> This data is then inserted into the mySQL: Code: [Select] <?php if(isset($_POST['update'])) { $mqty = $_POST['mainqty']; for ($i = 1; $i <= $mqty ; $i++) { $productid = $_POST['productid']; $productqtyid = $_POST['productqtyid']; $productsize = $_POST['productsize']; $colour = $_POST['colour']; $producttext = $_POST['text_$i']; mysql_query (" INSERT INTO emb_texts SET sessionid = '".$sessionid."', productid = '".$productid."', qtyid = '".$productqtyid."', size = '".$productsize."', colour = '".$colour."', text = '".$producttext."'") or die(mysql_error()); } } ?> This almost works but it adds the $productqtyid the same very time. I'm not sure if I am going about the the right way? Each form needs to add its own values into the mySQL. Many thanks for your help Similar TutorialsOk I'm trying to insert multiple rows by using a while loop but having problems. At the same time, need to open a new mysql connection while running the insert query, close it then open the previous mysql connection. I managed to insert multiple queries before using a loop, but for this time, the loop does not work? I think it is because I am opening another connection... yh that would make sense actually? Here is the code: $users = safe_query("SELECT * FROM ".PREFIX."user"); while($dp=mysql_fetch_array($users)) { $username = $dp['username']; $nickname = $dp['nickname']; $pwd1 = $dp['password']; $mail = $dp['email']; $ip_add = $dp['ip']; $wsID = $dp['userID']; $registerdate = $dp['registerdate']; $birthday = $dp['birthday']; $avatar = $dp['avatar']; $icq = $dp['icq']; $hp = $dp['homepage']; echo $username." = 1 username only? :("; // ----- Forum Bridge user insert ----- $result = safe_query("SELECT * FROM `".PREFIX."forum`"); $ds=mysql_fetch_array($result); $forum_prefix = $ds['prefix']; define(PREFIX_FORUM, $forum_prefix); define(FORUMREG_DEBUG, 0); $con = mysql_connect($ds['host'], $ds['user'], $ds['password']) or system_error('ERROR: Can not connect to MySQL-Server'); $condb = mysql_select_db($ds['db'], $con) or system_error('ERROR: Can not connect to database "'.$ds['db'].'"'); include('../_phpbb_func.php'); $phpbbpass = phpbb_hash($pwd1); $phpbbmailhash = phpbb_email_hash($mail); $phpbbsalt = unique_id(); safe_query("INSERT INTO `".PREFIX_FORUM."users` (`username`, `username_clean`, `user_password`, `user_pass_convert`, `user_email`, `user_email_hash`, `group_id`, `user_type`, `user_regdate`, `user_passchg`, `user_lastvisit`, `user_lastmark`, `user_new`, `user_options`, `user_form_salt`, `user_ip`, `wsID`, `user_birthday`, `user_avatar`, `user_icq`, `user_website`) VALUES ('$username', '$username', '$phpbbpass', '0', '$mail', '$phpbbmailhash', '2', '0', '$registerdate', '$registerdate', '$registerdate', '$registerdate', '1', '230271', '$phpbbsalt', '$ip_add', '$wsID', '$birthday', '$avatar', '$icq', '$hp')"); if (FORUMREG_DEBUG == '1') { echo "<p><b>-- DEBUG -- : User added: ".mysql_affected_rows($con)."<br />"; echo "<br />-- DEBUG -- : Query used: ".end($_mysql_querys)."</b></p><br />"; $result = safe_query("SELECT user_id from ".PREFIX_FORUM."users WHERE username = '$username'"); $phpbbid = mysql_fetch_row($result); safe_query("INSERT INTO `".PREFIX_FORUM."user_group` (`group_id`, `user_id`, `group_leader`, `user_pending`) VALUES ('2', '$phpbbid[0]', '0', '0')"); safe_query("INSERT INTO `".PREFIX_FORUM."user_group` (`group_id`, `user_id`, `group_leader`, `user_pending`) VALUES ('7', '$phpbbid[0]', '0', '0')"); mysql_close($con); } include('../_mysql.php'); mysql_connect($host, $user, $pwd) or system_error('ERROR: Can not connect to MySQL-Server'); mysql_select_db($db) or system_error('ERROR: Can not connect to database "'.$db.'"'); } So I need to be able to insert these rows using the while loop.. how can I do this? I really appreciate any help. Hello everyone, I'm a newbie with PHP and mySQL and need some assistance with writing a php script that searches a mySQL database using a form. The form has five fields that I want to search from and one is a required field (State). I need to filter or narrow down the search by either two or more fields. The problem I am having is if I used multiple WHERE clauses using the AND condition I have to enter valid information in all five fields and if I use the OR condition then my search does not produce the desired outcome (too many results). I "think" I need to use the AND condition but I need to be able to leave some of the fields blank (except for the State field) and narrow my search with using anywhere from 2-5 search fields. Also, another requirement is to be able to enter partial information in the search field "without" having to enter a wildcard in the search field. Any assistance is very much appreciated and thanks in advance for your help. Form Fields: State SELECT FIELD Lease TEXT FIELD Operator Name TEXT FIELD County or Parish TEXT FIELD Well No TEXT FIELD I have a table called well_permits and it is structure is as follows: date DATE state TEXT county VARCHAR api VARCHAR permit_no VARCHAR operator VARCHAR phone VARCHAR contact VARCHAR lease VARCHAR well_no VARCHAR permit_for VARCHAR welltype VARCHAR wellspot VARCHAR lat FLOAT lon FLOAT depth VARCHAR This is what I have for the connecting to my database and selecting the fields: <?php require_once('../../../Connections/Wldatabase.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $currentPage = $_SERVER["PHP_SELF"]; //Variable to store Unique_ID aka API which will be passed to the well-search-results.php page $var_api_rs_search = $_Get['api']; $maxRows_rs_search = 20; $pageNum_rs_search = 0; if (isset($_GET['pageNum_rs_search'])) { $pageNum_rs_search = $_GET['pageNum_rs_search']; } $startRow_rs_search = $pageNum_rs_search * $maxRows_rs_search; $var_state_rs_search = "%"; if (isset($_GET['state'])) { $var_state_rs_search = $_GET['state']; } $var_lease_rs_search = "%"; if (isset($_GET['lease'])) { $var_lease_rs_search = $_GET['lease']; } $var_well_no_rs_search = "%"; if (isset($_GET['well_no'])) { $var_well_no_rs_search = $_GET['well_no']; } $var_operator_rs_search = "%"; if (isset($_GET['operator'])) { $var_operator_rs_search = $_GET['operator']; } $var_county_rs_search = "%"; if (isset($_GET['County'])) { $var_county_rs_search = $_GET['County']; } mysql_select_db($database_Wldatabase, $Wldatabase); $query_rs_search = sprintf("SELECT DISTINCT * FROM well_permits WHERE (well_permits.`state` LIKE %s AND well_permits.county LIKE %s) OR (well_permits.lease LIKE %s) OR (well_permits.operator LIKE %s) OR (well_permits.well_no LIKE %s) ORDER BY well_permits.county", GetSQLValueString($var_state_rs_search, "text"),GetSQLValueString($var_county_rs_search, "text"),GetSQLValueString($var_lease_rs_search, "text"),GetSQLValueString($var_operator_rs_search, "text"),GetSQLValueString($var_well_no_rs_search, "text")); $query_limit_rs_search = sprintf("%s LIMIT %d, %d", $query_rs_search, $startRow_rs_search, $maxRows_rs_search); $rs_search = mysql_query($query_limit_rs_search, $Wldatabase) or die(mysql_error()); $row_rs_search = mysql_fetch_assoc($rs_search); ?> This is my form: <form action="search.php" method="GET" name="frmsearch" target="_self"> <input name="api" type="hidden" value="" /> <div> <table width="900" border="0" align="center" cellpadding="2" cellspacing="2"> <tr> <td colspan="6"> <p style="text-align:left">Select a State then enter at least one search criteria. State is a required field.</p> * Denotes a required field.<br> </td> </tr> <tr> <td align="right">* State: </td> <td> <select name="state" size="1" dir="ltr" lang="en"> <option value="AL">AL</option> <option value="AR">AR</option> <option value="CA">CA</option> <option value="CO">CO</option> <option value="IL">IL</option> <option value="IN">IN</option> <option value="KS">KS</option> <option value="KY">KY</option> <option value="LA">LA</option> <option value="MI">MI</option> <option value="MS">MS</option> <option value="MT">MT</option> <option value="ND">ND</option> <option value="NE">NE</option> <option value="NM">NM</option> <option value="NY">NY</option> <option value="OH">OH</option> <option value="OK">OK</option> <option value="OS">OS</option> <option value="PA">PA</option> <option value="SD">SD</option> <option value="TX">TX</option> <option value="UT">UT</option> <option value="WV">WV</option> <option value="WY">WY</option> </select> </td> <td align="right">County or Parish: </td> <td align="left"><input name="County" type="text" value="" size="35" maxlength="40" /></td> </tr> <tr> <td width="63" align="right">Lease: </td> <td width="239"><input name="lease" type="text" value="" /></td> <td align="right">Well No: </td> <td><input name="well_no" type="text" value="" /></td> </tr> <tr> <td width="111" align="right">Operator Name: </td> <td width="261"><input name="operator" type="text" value="" /></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td align="left"><input name="search" type="submit" value="Search" /></td> </tr> </table> </form> My Repeat Region starts here <table width="100%" border="1" align="center" cellpadding="2" cellspacing="2"> <tr> <td align="right"> </td> <th align="center">Operator</th> <th align="center">Lease</th> <th align="center">Well Number</th> <th align="center">County</th> <th align="center">State</th> </tr> <tr> <?php do { ?> <td align="center"><a href="results.php?recordID=<?php echo $row_rs_search['api']; ?>">Select</a></td> <td align="left"><?php echo $row_rs_search['operator']; ?></td> <td align="left"><?php echo $row_rs_search['lease']; ?></td> <td align="center"><?php echo $row_rs_search['well_no']; ?></td> <td align="center"><?php echo $row_rs_search['county']; ?></td> <td align="center"><?php echo $row_rs_search['state']; ?></td> </tr> <?php } while ($row_rs_search = mysql_fetch_assoc($rs_search)); ?> </table> <p align="center">Number of Wells Located: <?php echo ($startRow_rs_search + 1) ?> to <?php echo min($startRow_rs_search + $maxRows_rs_search, $totalRows_rs_search) ?> of <?php echo $totalRows_rs_search ?></p> <table border="0" align="center"> <tr> <td align="center"><?php if ($pageNum_rs_search > 0) { // Show if not first page ?> <a href="<?php printf("%s?pageNum_rs_search=%d%s", $currentPage, 0, $queryString_rs_search); ?>">First</a> <?php } // Show if not first page ?></td> <td align="center"><?php if ($pageNum_rs_search > 0) { // Show if not first page ?> <a href="<?php printf("%s?pageNum_rs_search=%d%s", $currentPage, max(0, $pageNum_rs_search - 1), $queryString_rs_search); ?>">Previous</a> <?php } // Show if not first page ?></td> <td align="center"><?php if ($pageNum_rs_search < $totalPages_rs_search) { // Show if not last page ?> <a href="<?php printf("%s?pageNum_rs_search=%d%s", $currentPage, min($totalPages_rs_search, $pageNum_rs_search + 1), $queryString_rs_search); ?>">Next</a> <?php } // Show if not last page ?></td> <td align="center"><?php if ($pageNum_rs_search < $totalPages_rs_search) { // Show if not last page ?> <a href="<?php printf("%s?pageNum_rs_search=%d%s", $currentPage, $totalPages_rs_search, $queryString_rs_search); ?>">Last</a> <?php } // Show if not last page ?></td> </tr> </table> I’m trying to insert from a multiple option form into MySQL. I’ve tried numerous methods and have had issues such as blank entries being inserted for each specified column, to the last value selected from the form being inserted multiple times, but I never get the desired outcome. Any guidance on how to insert from this type of HTML form? I know that there are issues regarding SQL injection - I'll worry about that later. I'd like to understand how to insert data from this kind of HTML form into MySQL, as i can't seem to find any guidance. There's plenty on how to insert from a standard HTML form i.e. where either every value is selected or left blank, but each entry corresponds to a column in MySQL which either receives a value or NULL. I'm really struggling with when a user can select more than one option from a list. Quote
<form action ='insert.php' method='POST'>
Here’s one option I’ve tried for PHP Quote
<?php
{ { } $conn->close(); ?>
Hi all . In my scripts , there is a textarea that allow user to enter multiple phone number , message and date . If there are 3 phone numbers in textarea , how can I insert 3 of them into sql with the same data of message and date? Such as : phone number : 0102255888,0235544777,0896655444 message:hello all date:10/10/2011 and when it's insert into table , it will become: 1. 0102255888 | hello all | 10/10/2011 2. 0235544777 | hello all | 10/10/2011 3. 0896655444 | hello all | 10/10/2011 Here is the code I tried , but it will just save the last phone number . Code: [Select] $cellphonenumber = explode(',', $_POST['cellphonenumber']); $message = $_POST['inputtext']; $date = $_POST['datetime']; foreach($cellphonenumber as $value) { $sql="INSERT INTO esp( Recipient , Message , Date) VALUES ('$value','$message','$date')"; } mysql_query($sql) or die ("Error: ".mysql_error()); echo "Database updated."; Any hints or advices ? Thanks for every reply . Hello all, I am trying to learn OOP in PHP so please forgive my ignorance. I seem to be having difficulty inserting data into my database from checkbox items. My database has a column for each each fruit. I would like for each column to be populated with either a one(1) for checked or a zero(0) for not checked. Currently, my attempts have failed. I can either get all checkboxes to insert all zeros or I can get the first column to insert a one(1) while the others as zeros. Is there anybody here that can help me figure out what I am doing wrong and help me to re-work my code so I can get this to work? If anybody can help me using OOP methods that would be fantastic. Thank you all in advance.
$preffruit->create_preffruit(array( 'fruit_apple' => escape(Input::get(['fruit_selection'][0]) ? 1 : 0), 'fruit_orange' => escape(Input::get(['fruit_selection'][1]) ? 1 : 0), 'fruit_banana' => escape(Input::get(['fruit_selection'][2]) ? 1 : 0), 'fruit_pear' => escape(Input::get(['fruit_selection'][3]) ? 1 : 0), 'fruit_kiwi' => escape(Input::get(['fruit_selection'][4]) ? 1 : 0) )); <input type="checkbox" name="fruit_selection[]" value="fruit_apple"> <input type="checkbox" name="fruit_selection[]" value="fruit_orange"> <input type="checkbox" name="fruit_selection[]" value="fruit_banana"> <input type="checkbox" name="fruit_selection[]" value="fruit_pear"> <input type="checkbox" name="fruit_selection[]" value="fruit_kiwi"> public function insert_preffruit($table, $fields = array()) { $keys = array_keys($fields); $values = ''; $x = 1; foreach($fields as $field) { $values .= '?'; if($x < count($fields)) { $values .= ', '; } $x++; } $sql = "INSERT INTO preffruit (`user_id`, `" . implode('`, `', $keys) . "`) VALUES (LAST_INSERT_ID(), {$values})"; if(!$this->query($sql, $fields)->error()) { return true; } return false; } Edited September 23, 2020 by ke-jo I have a form where a user can duplicate part of a form like below: I've named the fields with the HTML arrays (ie. "fieldName[]") and came up with this: //--> CONTROLLERS $system_controllers_qty = $_POST['system_controllers_qty']; $system_controllers_make = $_POST['system_controllers_make']; $system_controllers_model = $_POST['system_controllers_model']; $system_controllers_serial_no = $_POST['system_controllers_serial_no']; for ($i = 0; $i < count($system_controllers_qty); $i++) { echo "qty " . clean($system_controllers_qty[$i]) . "<br/>"; echo "make " . clean($system_controllers_make[$i]) . "<br/>"; echo "model " . clean($system_controllers_model[$i]) . "<br/>"; echo "serial_no " . clean($system_controllers_serial_no[$i]) . "<br/>"; } //--> CPUS $system_cpus_qty = $_POST['system_cpus_qty']; $system_cpus_model = $_POST['system_cpus_model']; $system_cpus_serial_no = $_POST['system_cpus_serial_no']; $system_cpus_speed = $_POST['system_cpus_speed']; for ($i = 0; $i < count($system_cpus_qty); $i++) { echo "qty " . clean($system_cpus_qty[$i]) . "<br/>"; echo "model " . clean($system_cpus_model[$i]) . "<br/>"; echo "serial_no " . clean($system_cpus_serial_no[$i]) . "<br/>"; echo "speed " . clean($system_cpus_speed[$i]) . "<br/>"; } //--> DISKS $system_disks_qty = $_POST['system_disks_qty']; $system_disks_make = $_POST['system_disks_make']; $system_disks_model_no = $_POST['system_disks_model_no']; $system_disks_size = $_POST['system_disks_size']; $system_disks_serial_no = $_POST['system_disks_serial_no']; for ($i = 0; $i < count($system_disks_qty); $i++) { echo "qty " . clean($system_disks_qty[$i]) . "<br/>"; echo "make " . clean($system_disks_make[$i]) . "<br/>"; echo "model_no " . clean($system_disks_model_no[$i]) . "<br/>"; echo "size " . clean($system_disks_size[$i]) . "<br/>"; echo "serial_no " . clean($system_disks_serial_no[$i]) . "<br/>"; } //--> MEMORY $system_memory_qty = $_POST['system_memory_qty']; $system_memory_serial_no = $_POST['system_memory_serial_no']; $system_memory_manf = $_POST['system_memory_manf']; $system_memory_part_no = $_POST['system_memory_part_no']; $system_memory_size = $_POST['system_memory_size']; for ($i = 0; $i < count($system_memory_qty); $i++) { echo "qty " . clean($system_memory_qty[$i]) . "<br/>"; echo "serial " . clean($system_memory_serial_no[$i]) . "<br/>"; echo "manf " . clean($system_memory_manf[$i]) . "<br/>"; echo "part_no " . clean($system_memory_part_no[$i]) . "<br/>"; echo "size " . clean($system_memory_size[$i]) . "<br/>"; } This just outputs all the fields I post at the moment. Say I have 3 different disk types, it's going to loop 3 times. Is it possible to make this all into 1 giant query or am I better off doing each query separately? Each section is a different table. Can anyone tell me why this is not INSERTing? My array data is coming out just fine.. I've tried everything I can think of and cannot get anything to insert.. Ahhhh! <?php $query = "SELECT RegionID, City FROM geo_cities WHERE RegionID='135'"; $results = mysqli_query($cxn, $query); $row_cnt = mysqli_num_rows($results); echo $row_cnt . " Total Records in Query.<br /><br />"; if (mysqli_num_rows($results)) { while ($row = mysqli_fetch_array($results)) { $insert_city_query = "INSERT INTO all_illinois SET state_id=$row[RegionID], city_name=$row[City] WHERE id = null" or mysqli_error(); $insert = mysqli_query($cxn, $insert_city_query); if (!$insert) { echo "INSERT is NOT working!"; exit(); } echo $row['City'] . "<br />"; echo "<pre>"; echo print_r($row); echo "</pre>"; } //while ($rows = mysqli_fetch_array($results)) } //if (mysqli_num_rows($results)) else { echo "No results to get!"; } ?> Here is my all_illinois INSERT table structu CREATE TABLE IF NOT EXISTS `all_illinois` ( `state_id` varchar(255) NOT NULL, `city_name` varchar(255) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; Here is my source table geo_cities structu CREATE TABLE IF NOT EXISTS `1` ( `CityId` varchar(255) NOT NULL, `CountryID` varchar(255) NOT NULL, `RegionID` varchar(255) NOT NULL, `City` varchar(255) NOT NULL, `Latitude` varchar(255) NOT NULL, `Longitude` varchar(255) NOT NULL, `TimeZone` varchar(255) NOT NULL, `DmaId` varchar(255) NOT NULL, `Code` varchar(255) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; Hello! I've got several forms on one page that use the same variable names for input values. (Say, form1, form2, and form3). I'm using javascript to show only one of the forms based on a selection (say British, American, Canadian). How can I write code which will tell the page to ONLY look at the variables from a particular form when I hit the submit button for one of the forms? Thanks so much.... I continue to have problems with this issue - primarily relates to the the placement of the code for processing the 2nd form on a page. This is what i wish to do: a. Form 1 - Select a User b. For selected user, list all the records c. Form 2 - Choose 1 record from the list. d. Update the record. Here's what i have so far. I've just put the structure out here as the error comes up when form 2 tries to get processed . I dont know if the issue is with the way i 've named the forms or the submit buttons. All pointers appreciated. Many thanks. Swati <? (processing form1) =================== if($_POST['submit-user']){ $user = $_POST["user"]; $result = mysql_query("SELECT ..."); if ($myrow = mysql_fetch_array($result)) { do { $xid=$myrow["Xid"]; //this has to be form 2 form2 ===== <form method="post" action="form2.php"> // is this correct - the first form was a "self" printf("<tr><td><input type=\"radio\" name=\"choice\" value=%d><td> <td>%s </td> <td>%s </td> </tr></p></table>", $xid , $myrow["timein"], $myrow["trip"] ); } while ($myrow = mysql_fetch_array($result)); } echo "</select>\n"; } <input type="submit" name="submit-data" value="Submit"> </p> </form> (end of form 2) processing form 2 - but where does it get placed ? ================== if($_POST['submit-data']){ $sscid = $_POST["choice"]; $result11 = mysql_query("SELECT * FROM `Table` WHERE `SSCID` = $sscid "); $myrow11 = mysql_fetch_array($result11); process this data } // does the bracket close here or below with the bracket of form 1 ?> // end processing of both forms. <html> <head> </head> <?php } else { ?> form1 ===== <FORM METHOD=post ACTION="<? echo $PHP_SELF ?>"> <select name="user"><option value="">[Select One] <? $result = mysql_query("SELECT ..."); if ($myrow = mysql_fetch_array($result)) { do { $userid=$myrow["Userid"]; printf("<option value = '$userid'> %s", $myrow["Username"]); } while ($myrow = mysql_fetch_array($result)); echo "</select>\n"; } ?> <input type="submit" name="submit-user" value="Submit"> </p> </form> (end of form 1) <?php } ?> </html> Hi I have a script that pulls all records out of the database, each record is within a form with no name which results with a lot of forms with essentially the same name. I have an hidden field with the accID which I use to delete the record. This does what I want it to do but have read that you should avoid having mutliple forms, I guess for validation. I know I could do this with a link and use GET, but would rather not having this action showing the details of the deletion in the URL. Is multiple forms so bad? Thanks Hi, I've tried to follow a couple tuts on multi-page forms using php. The reason I'm doing it is that I'd like to create a mock-job application form for my students at school. This would be too long to have on one page. Anyway, I've created the most simple form I could think of and was hoping someone could help me out and show me how to get the info from page 1 to 2, then the info from pages 1 and 2 to 3, and then submit the info from pages 1, 2 and 3 to the database. Page 1 asks for firstname, page 2 asks for lastname, and page 3 asks for email address. The fourth page is the one that processes the form. Page 1: (app_form_1.php) Code: [Select] <form id="form1" name="form1" method="post" action="app_form_2"> Firstname: <label> <input type="text" name="firstname" id="firstname" /> </label> </form> <p> <label> <input type="submit" name="Continue" id="Continue" value="Continue" /> </label> Page 2 (app_form_2.php) Code: [Select] <form id="form1" name="form1" method="post" action="app_form_3"> Lastname: <label> <input type="text" name="firstname" id="firstname" /> </label> </form> <p> <label> <input type="submit" name="Continue" id="Continue" value="Continue" /> </label> Page3 ( app_form_3.php ) Code: [Select] <form id="form1" name="form1" method="post" action="app_form_process"> Email address: <label> <input type="text" name="emailaddress" id="emailaddress" /> </label> </form> <p> <label> <input type="submit" name="submit" id="Submit" value="Submit" /> </label> Page 4 (app_form_process.php ) Code: [Select] <?php global $_POST; $firstname = $_POST["firstname"] ; $lastname = $_POST["lastname"]; $emailaddress = $_POST["emailaddress"]; //**********************SEND TO DATABASE**************************** //MySQL Database Connect include 'mysql_connect.php'; $query = "INSERT INTO application_form (firstname, lastname, emailaddress)" . "VALUES ('$firstname', '$lastname', '$emailaddress')"; //if($query){echo 'data has been placed'} mysql_query($query) or die(mysql_error()); ?> Any help is much appreciated. Thanks, Dave Hi all I have a form that i need to submit a get to the URL and then a second form that sends a different URL parameter but keeps the first in place? Here's my code: Code: [Select] <select name="category" onChange="this.form.submit();"> <option value= >--Choose category--</option> <?php $getcats = mysql_query (" SELECT * FROM `categories` ORDER by name ASC"); while ($showcats = mysql_fetch_array($getcats)) { echo "<option value=\"".$showcats['id']."\"".(($_GET['category']==$showcats['id']) ? ' selected="selected"':'').">".$showcats['name']."</option>"; } ?> </select> </form> <form id="make" name="make" method="GET" action="create-advert.php?category='<?php echo $_GET['category']; ?>"> <select name="make" onchange="this.form.submit();"> <option>--Choose make--</option> <?php $getmakes = mysql_query (" SELECT * FROM `makes` ORDER by name ASC"); while ($showmakes = mysql_fetch_array($getmakes)) { echo "<option value=\"".$showmakes['id']."\"".(($_SESSION['make']==$showmakes['id']) ? ' selected="selected"':'').">".$showmakes['name']."</option>"; } ?> </select> </form> Each time I select the make in the second form the url parameter 'category' disappears from the first? Many thanks Pete [/code] Code: [Select] //display an external link or form button if ($product_link = $meta['mp_product_link']) { $button = '<a class="mp_link_buynow" href="' . esc_url($product_link) . '">' . __('Buy Now »', 'mp') . '</a>'; } else { if ($all_out) { $button .= '<span class="mp_no_stock">' . __('Out of Stock', 'mp') . '</span>'; } else { $button = '<div class="mp_product_variations" name="variation">'; //create select list if more than one variation if (is_array($meta["mp_price"]) && count($meta["mp_price"]) > 1 && empty($meta["mp_file"])) { // for each as foreach ($meta["mp_price"] as $key => &$value) { $disabled = (in_array($key, $no_inventory)) ? ' disabled="disabled"' : ''; $variation_select = '<form name="' . $key . '" class="mp_buy_form" method="post" action="' . mp_cart_link(false, true) . '">\n'; $variation_select .= '<input type="hidden" name="product_id" value="' . $post_id . '" />'; $variation_select .= '<input type="hidden" name="variation" value="' . $key . '">'; $variation_select .= '<span>' . esc_html($meta["mp_var_name"][$key]) . ' - '; if ($meta["mp_is_sale"] && $meta["mp_sale_price"][$key]) { $variation_select .= $mp->format_currency('', $meta["mp_sale_price"][$key]); } else { $variation_select .= $mp->format_currency('', $value); }$variation_select .= "</span>\n"; if ($context == 'list') { if ($variation_select) { $variation_select .= '<a class="mp_link_buynow" href="' . get_permalink($post_id) . '">' . __('Choose Option »', 'mp') . '</a>'; } else if ($settings['list_button_type'] == 'addcart') { $variation_select .= '<input type="hidden" name="action" value="mp-update-cart" />'; $variation_select .= '<input class="mp_button_addcart" type="submit" name="addcart" value="' . __('Add To Cart »', 'mp') . '" />'; } else if ($settings['list_button_type'] == 'buynow') { $variation_select .= '<input class="mp_button_buynow" type="submit" name="buynow" value="' . __('Buy Now »', 'mp') . '" />'; } } else { $button .= $variation_select; //add quantity field if not downloadable if ($settings['show_quantity'] && empty($meta["mp_file"])) { $button .= '<span class="mp_quantity"><label>' . __('Quantity:', 'mp') . ' <input class="mp_quantity_field" type="text" size="1" name="quantity" value="1" /></label></span> '; } if ($settings['product_button_type'] == 'addcart') { $button .= '<input type="hidden" name="action" value="mp-update-cart" />'; $button .= '<input class="mp_button_addcart" type="submit" name="addcart" value="' . __('Add To Cart »', 'mp') . '" />'; } else if ($settings['product_button_type'] == 'buynow') { $button .= '<input class="mp_button_buynow" type="submit" name="buynow" value="' . __('Buy Now »', 'mp') . '" />'; } } $variation_select .= "</form>\n"; } //end for each $variation_select .= '</div>'; } else { $button .= '<input type="hidden" name="variation" value="0" />'; } } } Everything inbetween "for each" is being looped, except for the <form>. How can I get it to include <form> and </form> in the loop? Hi all I need to combine URL parameters using the following forms: <form id="type_filter" name="type_filter" method="get" action=""> <label for="type_filter"></label> <select name="type" id="type_filter" style="width: 160px" onchange="this.form.submit();" > <option value="" selected class="meter-calc-text">Product Type</option> <?php $fetchtypes=mysql_query("SELECT * FROM `product_types` ORDER BY id ASC"); while($returnedtypes=mysql_fetch_array($fetchtypes)) { echo "<option value=\"".$returnedtypes['id']."\"".(($returnedtypes['id']==$_GET['type']) ? ' selected="selected"':'')." >".$returnedtypes['name']."</option>"; } ?> </select> </form> <form id="colour_filter" name="colour_filter" method="get" action=""> <label for="colour_filter"></label> <select name="colour" id="colour_filter" style="width: 160px" onchange="this.form.submit();" > <option value="" selected class="meter-calc-text">Colour / Finish</option> <?php $fetchcolours=mysql_query("SELECT * FROM `product_colours` ORDER BY id ASC"); while($returnedcolours=mysql_fetch_array($fetchcolours)) { echo "<option value=\"".$returnedcolours['id']."\"".(($returnedcolours['id']==$_GET['colour']) ? ' selected="selected"':'')." >".$returnedcolours['name']."</option>"; } ?> </select> </form> At the moment they only send one parameter each. How do I drop down on one and add it to the parameter already on there to give this URL: ../stone.php?type=1&colour=1 Many thanks Pete Hi I have put together a options page for the script that I have been working on. I have 1 options page where there options are divided into 4 jQuery tabs with there own save button. The options in each tab are posted using Ajax to their own separate file like process.php, process1.php etc. My question is there a way to place all the processing code in one file and if so would this affect the load on the server? Would each form be better off having their own page for processing? For example: Code: [Select] <form id="options_1" action="process1.php" method="post" > <!-- this is form 1 that is processed with process1.php --> </form> <form id="options_2" action="process2.php" method="post" > <!-- this is form 2 Can this form be processed using process1.php instead of process2.php --> </form> <form id="options_3" action="process3.php" method="post" > <!-- this is form 3 Can this form be processed using process1.php instead of process3.php --> </form> Hey all, This question is coming forth of another topic, but since that topic is not really about this issue, I posted this new topic. Okay, so I made a table, and each row is generated with a WHILE loop. In this while loop, there's also a form generated for each row/record, for updating them seperately. I'm having trouble with naming those forms to process them seperately. I could name them like name="form20110001", with the number being the record's id. But since there can be gaps between id's, how can I retreive them efficiently? And I want them to process all on a single process page, obviously. How can I do this easily? Can someone give me a concise example? Would appreciate it a lot. Thanks Hey guys, im new to php and have become stuck with having more than one html form/submit that will relays to another piece of code to view something At the moment my first form works but once i submit my second form it relays back to the first form how can i prevent this so i can have many submit forms on one php page? Thanks Code: [Select] <?php $dbname = $_POST['dbname']; $tblname = $_POST['tblname']; checksubmit( $dbname); function checksubmit( $dbname){ if(isset($_POST['enter'])) { checkform( $dbname); } else { page1( $dbname); } } function checkform ($dbname){ if(empty($_POST['dbname'])){ echo 'fill out database name'; } else page2( $dbname ); } function page1( $dbname){ echo " <form method='post' action=''> <table> <tr> <td>Database name:</td><td><input type='text' name='dbname' /></td> </tr><tr> <td colspan='2' align='right'><input type='submit' name='enter' value='enter' /></td> </tr> </table> </form> "; } function page2( $dbname ){ echo " <form method='post' action=''> <table> <tr> <td>Table Name:</td><td><input type='text' name='tblname' /></td> </tr><tr> <td colspan='2' align='right'><input type='submit' name='return' value='return' /></td> </tr> </table> </form> "; if(isset($_POST['return'])) { page3( $dbname ); } } function page3( $dbname ) { echo 'I want to make it here'; } ?> Hello, I'm having a bit of a problem here, all help to this issues would be much appreciated I am trying to use text boxes to insert numbers into the database based on what is inputed. If I have a string, like this for example: $variable = 09385493; And I want to insert it into the database like this: mysql_query("INSERT INTO integers(number) VALUES ('$variable')"); When checking the integers table in my database, looking at the number field, the $variable that was inserted is outputted as 9385493 Notice the number zero was taken out of the front of the number. If the number is double 0's (009385493), both of those zero's would disappear, too. Thanks I am creating a user inbox system. I am retrieving all the unread messages. Each message row contains a "reply" form. So say I have 10 messages showing on a single page. That's 10 forms. What I would like to know is how can I submit any one of the 10 forms and not have it affect the remaining 9 forms? Here is the basic code. if(isset($_POST['submit'])) { $post_message = trim($_POST['message']); $errors = array(); $db->beginTransaction(); if(empty($post_message)) { $errors[] = 'The message field can not be empty!'; } if(empty($errors)) { $db->commit(); echo 'success'; } else { $db->rollBack(); } } <form action="" method="post"> <fieldset> <textarea name="message" maxlength="10000" placeholder="What would you like to say?"></textarea> </fieldset> <fieldset> <input type="submit" name="submit" value="Submit" /> </fieldset> </form>
|