0

I have a form with a row called target1 and a JavaScript which adds rows dynamically i.e. target2,target3 etc. and same for strategy and resource.

I want to enter these into a database using php and mysql. Because the amount of submitted rows is dynamic, i think i need a foreach statement or while loop.

I have attempted a foreach() with no luck.

Here is the code.

Dynamic row JavaScript/PHP File:

<?php
header('Content-type: application/x-javascript');
if($_REQUEST['type'] == "targets"){
?>
function addRowToTargetTable()
{
  var tbl = document.getElementById('targets');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);

  // left cell
  var cellLeft = row.insertCell(0);
  var textNode = document.createTextNode(iteration + ".");
  var targetid = document.createElement('input');
  targetid.type = 'hidden';
  targetid.name = 'target' + iteration;
  targetid.id = 'target' + iteration;
  targetid.value = 'target' + iteration;
  cellLeft.appendChild(textNode);
  cellLeft.appendChild(targetid);

  // right cell
  var cellRight = row.insertCell(1);
  var el = document.createElement('textarea');
  el.name = 'target' + iteration;
  el.id = 'target' + iteration;
  el.className = 'validate[required]';
  el.style.cssText="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;";
  cellRight.appendChild(el);

  // far right cell
  var cellFarRight = row.insertCell(2);
  var el2 = document.createElement('textarea');
  el2.name = 'targetcriteria' + iteration;
  el2.id = 'targetcriteria' + iteration;
  el2.className = 'validate[required]';
  el2.style.cssText="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;";
  cellFarRight.appendChild(el2);

}
function removeRowFromTargetTable()
{
  var tbl = document.getElementById('targets');
  var lastRow = tbl.rows.length;
  if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
<?php
}elseif($_REQUEST['type'] == "strategies"){
?>
function addRowToStrategyTable()
{
  var tbl = document.getElementById('strategies');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);

  // left cell
  var cellLeft = row.insertCell(0);
  var textNode = document.createTextNode(iteration + ".");
  var strategyid = document.createElement('input');
  strategyid.type = 'hidden';
  strategyid.name = 'strategy' + iteration;
  strategyid.id = 'strategy' + iteration;
  strategyid.value = 'strategy' + iteration;
  cellLeft.appendChild(textNode);
  cellLeft.appendChild(strategyid);

  // right cell
  var cellRight = row.insertCell(1);
  var el = document.createElement('textarea');
  el.name = 'strategy' + iteration;
  el.id = 'strategy' + iteration;
  el.className = 'validate[required]';
  el.style.cssText="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;";
  cellRight.appendChild(el);

}
function removeRowFromStrategyTable()
{
  var tbl = document.getElementById('strategies');
  var lastRow = tbl.rows.length;
  if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}

<?php }elseif($_REQUEST['type'] == "resources"){
?>
function addRowToResourceTable()
{
  var tbl = document.getElementById('resources');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);

  // left cell
  var cellLeft = row.insertCell(0);
  var textNode = document.createTextNode(iteration + ".");
  var resourceid = document.createElement('input');
  resourceid.type = 'hidden';
  resourceid.name = 'resource' + iteration;
  resourceid.id = 'resource' + iteration;
  resourceid.value = 'resource' + iteration;
  cellLeft.appendChild(textNode);
  cellLeft.appendChild(resourceid);

  // right cell
  var cellRight = row.insertCell(1);
  var el = document.createElement('textarea');
  el.name = 'resource' + iteration;
  el.id = 'resource' + iteration
  el.style.cssText="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;";
  cellRight.appendChild(el);

}
function removeRowFromResourceTable()
{
  var tbl = document.getElementById('resources');
  var lastRow = tbl.rows.length;
  if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
<?php
}
?>

Here is the form:

<div id="non-indent">
<form action="complete-iep-operations.php?operation=create&action=insert" method="post" id="validate" enctype="multipart/form-data">
<table border="1" class="listing full" id="register" style="border-collapse:collapse;">
  <tr class="no-pointer">
    <th scope="col" width="48">
      <h4>IEP No.</h4>
    </th>  
    <th scope="col" width="153">
      <h4>IEP Date</h4>
    </th>
    <th scope="col" width="124">
      <h4>Renewal Date</h4>
    </th>
    <th scope="col" width="116">
      <h4>IEP Meeting Conducted</h4>
    </th>
  </tr>
  <tr>
    <td>
    <p><input name="iep_number" type="text" id="iep_number" maxlength="2" class="validate[required]" style="margin-top:5px; width:40px;" /></p>
    </td>
    <td>
    <p>
      <input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="iep-day" id="iep-day" maxlength="2" />
      -
      <input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="iep-month" id="iep-day" maxlength="2" />
      -
      <input type="text" style="margin-top:5px; width:60px;" class="validate[required]" name="iep-year" id="iep-day" maxlength="4" />
    </p>
    </td>
    <td>
    <p>
      <input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="renewal-day" id="renewal-day" maxlength="2" />
-
<input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="renewal-month" id="renewal-day" maxlength="2" />
-
<input type="text" style="margin-top:5px; width:60px;" class="validate[required]" name="renewal-year" id="renewal-day" maxlength="4" />
    </p>
    </td>
    <td>
    <p>
      <input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="completed-day3" id="completed-day3" maxlength="2" />
-
<input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="completed-day3" id="completed-day3" maxlength="2" />
-
<input type="text" style="margin-top:5px; width:60px;" class="validate[required]" name="completed-day3" id="completed-day3" maxlength="4" />
    </p>
    </td>
  </tr>
</table>
<br />
<h3>Targets</h3>
<br />
<table border="1" class="listing full" id="targets" style="border-collapse:collapse;">
  <tr class="no-pointer">
    <th scope="col" width="102">
      <h4>Target Number</h4>
    </th>  
    <th scope="col">
      <h4>Target Details</h4>
    </th>
    <th scope="col">
      <h4>Target Achievement Criteria</h4>
    </th>
  </tr>
  <tr>
    <td>
    <p>1.</p>
    <input type="hidden" value="target1" id="target1" />
    </td>
    <td>
      <label for="target1"></label>
      <textarea name="target1" id="target1" class="validate[required]" style="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"></textarea>
    </td>
    <td>
      <label for="targetcriteria1"></label>
      <textarea name="targetcriteria1" id="targetcriteria1" class="validate[required]" style="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"></textarea>
    </td>
  </tr>
</table>
<br />
<table border="1" class="listing full" id="register" style="border-collapse:collapse;">
<tr>
<td>
<div align="right">
<input type="button" value="Remove Target Row" id="submit" onclick="removeRowFromTargetTable();" style="margin:0px; margin-right:10px; float:right;" />
<input type="button" value="Add Another Target" id="submit" onclick="addRowToTargetTable();" style="margin:0px; margin-right:20px; float:right;" />
</div>
</td>
</tr>
</table>
<br />
<h3>Strategies</h3>
<br />
<table border="1" class="listing full" id="strategies" style="border-collapse:collapse;">
  <tr class="no-pointer">
    <th scope="col" width="102">
      <h4>Strategy Number</h4>
    </th>  
    <th scope="col">
      <h4>Strategy Details</h4>
    </th>
  </tr>
  <tr>
    <td>
    <p>1.</p>
    <input type="hidden" value="strategy1" id="strategy1" />
    </td>
    <td>
      <label for="strategy1"></label>
      <textarea name="strategy1" id="strategy1" class="validate[required]" style="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"></textarea>
    </td>
  </tr>
</table>
<br />
<table border="1" class="listing full" id="register" style="border-collapse:collapse;">
<tr>
<td>
<div align="right">
<input type="button" value="Remove Strategy Row" id="submit" onclick="removeRowFromStrategyTable();" style="margin:0px; margin-right:10px; float:right;" />
<input type="button" value="Add Another Strategy" id="submit" onclick="addRowToStrategyTable();" style="margin:0px; margin-right:20px; float:right;" />
</div>
</td>
</tr>
</table>
<br />
<h3>Resources</h3>
<br />
<table border="1" class="listing full" id="resources" style="border-collapse:collapse;">
  <tr class="no-pointer">
    <th scope="col" width="102">
      <h4>Resource Number</h4>
    </th>  
    <th scope="col">
      <h4>Resource Details</h4>
    </th>
  </tr>
  <tr>
    <td>
    <p>1.</p>
    <input type="hidden" value="resource1" id="resource1" />
    </td>
    <td>
      <label for="resource1"></label>
      <textarea name="resource1" id="resource1" style="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"></textarea>
    </td>
  </tr>
</table>
<br />
<table border="1" class="listing full" id="register" style="border-collapse:collapse;">
<tr>
<td>
<div align="right">
<input type="button" value="Remove Resource Row" id="submit" onclick="removeRowFromResourceTable();" style="margin:0px; margin-right:10px; float:right;" />
<input type="button" value="Add Another Resource" id="submit" onclick="addRowToResourceTable();" style="margin:0px; margin-right:20px; float:right;" />
</div>
</td>
</tr>
</table>
<br />
<table border="0" class="full" id="register" style="border-collapse:collapse;">
<tr>
<td>
<div align="right">
<input name="submit" type="submit" id="submit" style="margin:0px; margin-right:10px; float:right;" /></div>
</td>
</tr>
</table>
</form>
</div>

And the php code:

<?php
mysql_connect('host', 'XXXXX', 'XXXXX') or die(mysql_error());
mysql_select_db('db401728075') or die(mysql_error());

if($_REQUEST['operation']=="create" && $_REQUEST['action']=="insert") {

$targeti = 1;
while(true) {
    if(isset($_POST['target'.$targeti])) {
        $target = $_POST['target'.$targeti];
        echo $target;
        echo "<BR>";
        $targeti++;
    } else {
      break;
    }
}

Any help you can give me would be highly appreciated.

2
  • 7
    Do not post your database connection information like host, username and password on a public webpage! Commented Mar 9, 2012 at 9:45
  • use name="targets[]" for alle the target inputs, and it return an array where you can loop trough Commented Mar 9, 2012 at 9:48

3 Answers 3

1

You can't iterate over $_POST['target'] because it doesn't exist. You need something like that:

$targeti = 1;
while(true) {
    if(isset($_POST['target'.$targeti])) {
        $target = $_POST['target'.$targeti];
        // process $target value here
        $targeti++;
    } else {
      break;
    }
}

and the same code for other types

EDITED: If you don't want to process value of $target inside the loop, you can save it in array ($targets[] = $target). The main idea of processing it in loop or saving in array is that if you'll create variables dynamically (${'target'.$targeti} = $_POST[...]) then you'll need to save counter variable ($targeti) and use it later to find all created variables, and such code it not clear.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for this code. However this causes the site to loop and not load the page Error 500. Any Ideas why?
Please look on updated code. I forgot to add break statement. Sorry about that
Can you add var_dump($_POST) to your question? I can't understand now why is doesn't work
Add dump just like another piece of code. Also try to add die('end') after while loop to see is problem occurs after loop or inside it.
Fixed it my server was automatically quiting. Do not know why but rectified it now thanks
0

In your php code you are not doing any mysql insert actions. Also $targeti = "2"; should be something like $targeti++; otherwise you always loop with target2.

1 Comment

the reason i have not included the insertions because it is no identifying the $_post's in an echo which i do further down the page. that is when it inserts it. (if the posts exist)
0

I might be wrong but I'll confirm.

HTML should be written as:

<textarea name="target []" id="target" class="validate[required]" style="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"></textarea>

that way your values are uploaded as an array.

and the PHP for that would be:

 <?php
 if(isset($_POST['target'])) {  
         $target = $_POST['target'];            
         foreach($target as $targetvalue){  
            echo $targetvalue; // do something with the target value here.  
        }
     } 
?>

3 Comments

Why are you assigning the values to a local variable, only for looping? There really isn't a reason, right?
Name attribute must be "target[]" not id, because browsers send name to server
My bad. Wrote that in a rush . Thanks . Does it solve the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.