gple Posted March 20, 2007 Share Posted March 20, 2007 I want to create a form with the same field 15 teams. I want to submit different information in each text box and then after clicking submit, have each text box be inserted to the database as 1 record. So in total, there would be 15 different records inserted into the db. Link to comment https://forums.phpfreaks.com/topic/43428-form-with-multiple-fields/ Share on other sites More sharing options...
Rottingham Posted March 20, 2007 Share Posted March 20, 2007 Well, This post has a few flaws... first...do you want to create 15 fields of the same TYPE, like text boxes? Or 15 fields of the same name? Do you want to insert them into a mySQL database? You need to be a little more specific. Link to comment https://forums.phpfreaks.com/topic/43428-form-with-multiple-fields/#findComment-210918 Share on other sites More sharing options...
redarrow Posted March 20, 2007 Share Posted March 20, 2007 <?php echo"<form method='POST' action='".$_SERVER['PHP_SELF']."'>"; for($i=0; $i<14; $i++){ echo"<input type='text' name='database_values[]' >"; } echo" <input type='submit' name='submit' value='send me'>"; ?> Link to comment https://forums.phpfreaks.com/topic/43428-form-with-multiple-fields/#findComment-210948 Share on other sites More sharing options...
gple Posted March 20, 2007 Author Share Posted March 20, 2007 what does this do action='".$_SERVER['PHP_SELF']."'>"; how does this insert into the table. each field is a record of the same type. Link to comment https://forums.phpfreaks.com/topic/43428-form-with-multiple-fields/#findComment-211533 Share on other sites More sharing options...
marmite Posted March 20, 2007 Share Posted March 20, 2007 The PHP_SELF is a shorthand way of referring to the server you're working on. The code doesn't have the SQL in it to actually put the data into the DB (I don't think, anyway). You need to write some SQL to INSERT, apply it to a variable, and shoot the variable into the DB using the OnClick command or something, See this: <?php if ($_POST['submit1']) { $email=trim($_POST['email']); $query="INSERT INTO email (email_address) VALUES ('$email')"; $result=mysql_query($query); } ?> <table width="100%"> <tr> <td width="100%" align="center"><form id="form1" name="form1" method="post" action="<?=$_SERVER['PHP_SELF']?>"> <label></label> <input name="email" type="text" id="email" onfocus="this.value = ''; this.onfocus = function () {}" value="E-mail address" size="15" /> </label> <input type="submit" name="submit1" value="GO!" onclick="return validate(form1)" /> </form></td> </tr> </table> JAVASCRIPT function validate(formCheck) { var mail = formCheck.email.value if (mail.indexOf("@") == -1) { alert("Please type a valid email address"); formCheck.email.focus(); return false; } alert("Thanks! Your information is being registered"); return true; } Link to comment https://forums.phpfreaks.com/topic/43428-form-with-multiple-fields/#findComment-211555 Share on other sites More sharing options...
grimmier Posted March 21, 2007 Share Posted March 21, 2007 Here is something thats pretty much what you are asking. I use this form at work for our testers to enter tested equipment into the inventory. <?php // Connection information Normally in a seperate php file and called with an include. $con = mysql_connect("localhost","user_name","pass_word"); // change these to what you use. if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("DB_NAME", $con); //format date This is for the 2 date fields, it will allow the users to enter a date in mm-dd-yy or mm/dd/yy format. $string=$_POST['date_rec']; $dash = '-'; $pos = strripos($string, $dash); if ($pos === false) { $inputTime=explode("/", $string); if (substr_count($string, '/') < 2) {die('ERROR: Improper Date');} $time=date("Y-m-d", mktime(0,0,0,$inputTime[0],$inputTime[1],$inputTime[2])); } else { $inputTime=explode("-", $string); if (substr_count($string, '-') < 2) {die('ERROR: Improper Date');} $time=date("Y-m-d", mktime(0,0,0,$inputTime[0],$inputTime[1],$inputTime[2])); } $string2=$_POST['date_clear']; $pos2 = strripos($string2, $dash); if ($pos2 === false) { $inputTime2=explode("/", $string2); $time2=date("Y-m-d", mktime(0,0,0,$inputTime2[0],$inputTime2[1],$inputTime2[2])); } else { $inputTime2=explode("-", $string2); $time2=date("Y-m-d", mktime(0,0,0,$inputTime2[0],$inputTime2[1],$inputTime2[2])); } //post data to the database. $sql="INSERT INTO Inventory (`Inventory ID`, `Owner ID`, `Date Received`, `Date Cleared`, `Status`, `Equipment Type`, `Make`, `Model`, `Serial Number`, `Processor`, `Speed`, `HD`, `RAM`, `Optical Drive`, `Tag #`, `Comments`, `Tested By`) VALUES ('$_POST[inventoryID]','$_POST[ownerID]','$time','$time2','$_POST[status]','$_POST[type]','$_POST[make]','$_POST[model]','$_POST[serial]','$_POST[processor]','$_POST[speed]','$_POST[hd]','$_POST[ram]','$_POST[cd]','$_POST[tag]','$_POST[comments]','$_POST[tester]')"; // I use $_POST here instead of SESSION, I have the form set to reload the same page. if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added", '<br>'; echo $time2; mysql_close($con) ?> Then to make sure everything is entered as it should be, you may want to do a form validation. something like this snippet. $valid_form = true; if ($_POST['inventoryID'] == "") {echo " -- Enter Inventory ID -- " , '<br>'; $valid_form = false;} if ($_POST['ownerID'] == "") {echo " -- Enter Owner ID -- ", '<br>'; $valid_form = false;} if ($_POST['date_rec'] == "") {echo " -- Enter Date Received -- ", '<br>'; $valid_form = false;} if($valid_form == false) {die('<strong>'. '<font color="#990000">'. ' -- ERROR: Missing Data Fields! -- '.'</font>'. '</strong>');} ?> Link to comment https://forums.phpfreaks.com/topic/43428-form-with-multiple-fields/#findComment-211937 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.