Jump to content

Form rows as an array of text boxes for data entry


bulrush

Recommended Posts

I am somewhat new to PHP. I am going through a PHP tutorial book "Head First: PHP and MySql". So I know the PHP basics. But none of their examples cover what I need to do in a real world application.

 

I would like to make a form with many columns per row, with multiple rows. Ideally each row would be an entry in an array, so the user can enter 10 rows at once, and save them all at once. My form with one row looks like this:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<table>
<tr><td>Description <td>Number <td>Price 
<tr><td><input type="text" name="txtDesc" value="<?php if (!empty($desc)) echo $desc; ?>" size="45">
    <td><input type="text" name="txtModel" value="<?php if (!empty($model)) echo $model; ?>" size="20">
    <td><input type="text" name="txtPrice" value="<?php if (!empty($price)) echo $price; ?>" size="6">
</table>

<p>

<p>Commands: <input type="submit" name="submit" value="save">

</form>

 

Now, how can I turn this into 10 rows, like an array?

 

I think so. In Visual Basic I think it's called a Data Grid, or simply a Grid control. I'm not sure what the PHP term is.

 

But yes, I want to display n rows on the New Record Entry screen, where each row would become a new record in a table. Later, I will make a screen to edit existing records. This screen is for mass entry of new records.

 

Data Grid isn't bind to any one specific language. But there's definitely a way to do that. One way would be to give them separate names like: txtDec1, txtDec2, etc.

 

You can initially set up a table with 10 rows, all editable fields. Then have a button to add more if need be. Use JavaScript to save how many rows are added + the original 10. Then upon submit, have JavaScript fill a hidden field with that count and have PHP do a loop through them all.

Ok. So if I name the form fields txtDesc1, txtDesc2, txtDesc3, txtModel1, txtModel2, txtModel3, etc for rows 1, 2, and 3. How would I construct the field names for my INSERT statement inside a loop?

 

for ($i=1; $i<=10; $i++)
  {
  $query = "INSERT INTO mytable (desc, model) VALUES (??, ??)";
  //... do query
  } //for i

 

o.O huh?

 

You get the values and put them into the query. I'm not understanding your question at all.

 

You know that the fields are txtDesc#. The # part will be specified by $i. So you just fetch the values of those fields and put them into the query.

But txtDesc1 is not a variable at this point, is it? It's the name of a form field (text box). Isn't that different than a PHP variable? A form is in HTML, a variable is in PHP. I have not saved the value of the form field in a PHP variable yet.

 

Perhaps this will work:

for ($i=1; $i<=10; $i++)
  {
  $descvar='txtDesc'.$i;
  $modelvar='txtModel'.$i;
  $query = "INSERT INTO mytable (desc, model) VALUES ('$$descvar', '$$modelvar')";
  //... do query
  } //for i

 

Maybe I should clarify. My php page is called "entry.php". When the user goes to this form the first time, it shows a form with 10 rows. If they click the Save button (named "submit") then the form reloads itself and saves all 10 rows to the table.

 

So, when the form is submitted, and the form fields are named txtDesc1, txtDesc2, txtModel1, txtModel2, the values are saved in $_POST['txtDesc1'], $_POST['txtDesc2'], etc. They are not stored in variables $txtDesc1 from my understanding, unless I manually copy the values from $_POST['txtDesc1'] to $txtDesc1.

 

 

It's tricky because I'm new to PHP. I just started going through this book last week, and I'm not sure how to construct a PHP variable inside the double quotes of my query statement.

 

Do you think this would work?

for ($i=1; $i<=10; $i++)
  {
  $descvar=$_POST['txtDesc'.$i];
  $modelvar=$_POST['txtModel'.$i];
  $query = "INSERT INTO mytable (desc, model) VALUES ('$$descvar', '$$modelvar')";
  //... do query
  } //for i

 

Archived

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

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