Jump to content
Sign in to follow this  
Murtaza66

How to insert a multiple textbox with the same name

Recommended Posts

Hello everyone,

I'm working on a project for school.
It's an invoice program but I'm stuck, can anyone help me
figure out how to get the values out of multiple textboxes /fields
and insert them in to my database.

This is what it looks like.
The user can select the amount of textfields that will be inserted in to the database.

<div class="input_fields_wrap">
						<button id="remove_field">x</button>
						<?php $query = mysql_query("SELECT * FROM producten"); ?>
						<select name="Producten[]"  id="Producten">
						<div><?php while($row = mysql_fetch_array($query)){
										echo "<option>" . $row['Producten'] . "</option>";
									}?>
						</div><input type="text" name="ProdOms[]">
							  <input type="text" size="3" name="Aantal[]">
							  <input type="text" size="3" name="Prijs[]">
							  <a href="javascript:void(0)" onclick="toggle_visibility('popup-box1');"><img src="../img/icons/info.png" height="16" width="16"></a>
						</select>
					</div>

this is my html form I have 15 of these how to insert the values in the database 

 

Share this post


Link to post
Share on other sites

First, use mysqli instead of mysql for your database functionality.  

Next write a simple script to output your data on post.  I think you'll see post data differently then you're expecting.  

/* form.php script */
<form action="seepostdata.php" method="post">
  ... form fields, etc

Then in your seepostdata.php script:

/* output post array to viewer */
echo "<pre>";
print_r($_POST);
echo "</pre>";

This should output something like 

Array (
 [Producten] => array (
    [0]=>"first option selection",
    [1]=>"next option selection",
    [2]=>"etc etc"
 )
 [ProdOms] => array (
    [0]=>"first string",
    [1]=>"another string",
    [2]=>"etc etc"
)
)

Then copy paste that output to a text file and save it for reference.  It is very difficult to visualize a multidimensional array.  Which is being created when you name your fields like name="Poducten[]".  Good luck.

Share this post


Link to post
Share on other sites

To answer your question more directly.  Here is one way to access your post variables.

$producten = $_POST['Producten'][0]; //would be the first producton select item
Edited by rwhite35

Share this post


Link to post
Share on other sites

thanks rwhite35 but what if the user fills 2 form how to get the value of them I thought using a foreach statement like this $row_data = array();

foreach($_POST['Producten'] as $row=>$data){
$Producten = mysql_real_escape_string($data);
$ProdOms   = mysql_real_escape_string($_POST['ProdOms'][$row]);
$Aantal    = mysql_real_escape_string($_POST['Aantal'][$row]);
$Prijs     = mysql_real_escape_string($_POST['Prijs'][$row]);
 
$row_data[] = "('$Producten','$ProdOms','$Aantal','$Prijs')";
 
$sql = mysql_query("INSERT into hulptabel (ID, FactuurNR, Product,ProdOms,Aantal, Prijs) VALUES ('$ID','$FactuurNR' ,'$Producten','$ProdOms','$Aantal','$Prijs')");
 
}
 
But when I wants to insert more then 2 rows it will insert only 1 row

Share this post


Link to post
Share on other sites

Check out this post here.  You're looking to batch all 15 forms in to one transaction. Also read ConNix comment on prepared statements.  Prepared statements will automatically optimize your POST data and it is more secure then what you have.

Share this post


Link to post
Share on other sites

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.

Sign in to follow this  

×
×
  • 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.