I'm looking for some help with code that will take POST information from a form and insert it into a table, for multiple rows at a time.

The problem is, the amount of rows the form could have varies depending on how many the user chooses to submit. I have a javascript Add Row button that adds additional rows to an original one row.

Here is the javascript and form code:

<script>
function generateRow() {

var d=document.getElementById("div");
d.innerHTML+="&nbsp;#&nbsp;<input name=\"varnum\" type=\"text\" id=\"food\" size=\"5\"/>
			Desc:&nbsp;<input name=\"vardesc\" type=\"text\" size=\"30\" />
			Instr by:&nbsp;<input name=\"varins\" type=\"text\" size=\"8\" />
			&#163;&nbsp;<input name=\"varvalue\" type=\"text\" /size=\"8\" /><br>";

}

</script>
			<h2>Create New Valuation</h2>
			<h3>Step 2 - Add Variations</h3>
			<p>Complete all fields in the form below</p>
			<form action=\"add.php?action=submit_valuation_step2\" name=\"frmAddValuation\" method=\"post\" onsubmit=\"return validateForm3()\">
			<table width=\"650\" border=\"0\">

		<tr><td bgcolor=\"#CCCCCC\" valign=\"top\">Variations </td><td>

		&nbsp;#&nbsp;<input name=\"varnum\" type=\"text\" size=\"5\"/>
		Desc:&nbsp;<input name=\"vardesc\" type=\"text\" size=\"30\" />
		Instr by:&nbsp;<input name=\"varins\" type=\"text\" size=\"8\" />
		&#163;&nbsp;<input name=\"varvalue\" type=\"text\" /size=\"8\" /><br>

		<div id=\"div\"></div>

		<input type=\"button\" value=\"Add More Rows\" onclick=\"generateRow()\"/>

		</td></tr>
		</table><input type=\"submit\" name=\"submit\" value=\"Continue to Step 2\">
		</form>

I guess what Im looking for is the php and INSERT query for dealing with an undetermined number of rows...some sort of array. Can anyone help?

    Make sure that the names of the form elements that might occur multiple times are part of an array. So instead of using <input type="text" name="lastname"> use <input type="text" name="lastname[]">.

    In your processing code you can now use the variable $_POST['lastname'] as an array. A foreach loop will allow you to insert:

    foreach($_POST['lastname'] as $i => $lastname)
    {
    echo 'Set: '.&i;
    echo $lastname;
    }

      leatherback;10984807 wrote:

      Make sure that the names of the form elements that might occur multiple times are part of an array. So instead of using <input type="text" name="lastname"> use <input type="text" name="lastname[]">.

      In your processing code you can now use the variable $_POST['lastname'] as an array. A foreach loop will allow you to insert:

      foreach($_POST['lastname'] as $i => $lastname)
      {
      echo 'Set: '.&i;
      echo $lastname;
      }

      Thank you.

      My next question however, is, how do I incorporate that variable into my INSERT statement, which already pertains to just one row? What do I need to change?

      
      $val = $_GET["val"];
      
      mysql_query("
      INSERT INTO val_variations (
      variation_val, variation_num, variation_detail, variation_ins, variation_cost) 
      VALUES(
      '".$val."', '".$_POST["varnum"]."', '".$_POST["vardesc"]."', '".$_POST["varins"]."', '".$_POST["varvalue"]."') 
      ")
      or die(mysql_error());
      echo "Variations submitted.";	
      
      

        After you have added that to your form, place a print_r($_POST); at the top of your processing script. THat will give you an idea of the data being submitted. Your insert you can do by looping through the POSTed values, or you can create one SQL statement to insert all in one query. Typically, the latter is somewhat faster, I think, but requires more code.

          My apologies for my ignorance, but I think I've become well and truly lost now in terms of how I should be approaching this.

          I have a few questions.

          1. When formatting a text box, how should it look? At present I have the input name reading as the following for each row:
          &nbsp;#&nbsp;<input name=\"varnum[]\" type=\"text\" size=\"5\"/>
          Desc:&nbsp;<input name=\"vardetail[]\" type=\"text\" size=\"30\" />
          Instr by:&nbsp;<input name=\"varins[]\" type=\"text\" size=\"8\" />
          &#163;&nbsp;<input name=\"varcost[]\" type=\"text\" size=\"8\" /><br>
          
          1. This brings me to my next Q. What are the [] used for in the input name?

          2. In my processing script, I now have:

          foreach($_POST['varnum'] as $i => $varnum)

          , as per the earlier post. Would I need to use this line for each field in the row? I.e. vardetail, varins, varcost?

          My apologies for so many questions, this should really be in the Newbies section.

            I'll try to get back to you later tonight, with an example if still needed.

            The [] tell the form that you are actually getting an array of values, nemaly, once you add an input block, you get a new value, for the same variable.

            You should be able to access those through:

            $POST['varname'][0]
            $
            POST['varname'][1]
            etc.

            The forach you only need once; The rest you index with the $i;

            As said: Try a print_r($_POST); at the start of your script, so you can see what the form sends to your script.

            J.

              Write a Reply...