1

I am relatively new to jQuery and I am having a problem with dynamically adding form elements using jQuery then iterating through those form elements with PHP to add to a database.

Basically what I have is a table and upon clicking a div, a new table row is added which contains the form elements. The code looks like this:

$('.addRow1').click(function() {
      <?php $count++; ?>
       $('.mainTable > tbody:last').one().append('<tr><td>Chronic Pain Referral</td><td>Chronic referral</td><td><input type="hidden" name="count" value="<?php echo $count; ?>"><textarea name="notes"></textarea></td></tr>');
    });

As you can see, I attempted to add a counter using PHP so each time a row was added it would add 1 to $count and then I could simply do a foreach() statement to iterate through the form data for each row.

However this didn't work. I get a server error if I try to use foreach() with the counter, and if I simply add a lot of table rows and click submit, when I print the counter value, it always ends up as 1 no matter how many rows I add.

2
  • 1
    JS/jQuery is client side, and PHP is server side. Basically <?php $count++; ?> is running only one time on the server. Commented Apr 12, 2013 at 10:00
  • Learn about HTTP protocol: how jQuery performs HTTP requests and how PHP handles them [and generates HTTPresponses accordingly]. Commented Apr 12, 2013 at 15:48

3 Answers 3

1

The PHP code <?php $count++; ?> executes on the server, before the page is sent to the client; it does not execute when the JavaScript function fires. You'll need to use a JavaScript variable for the incremental field count.

Also, in your form, you'll just have repeated instances of a field named "count" with increasing values, and instances of a text area called "notes". The "count" fields will be pointless as you can't associate them with the text areas (form fields are not guaranteed to be submitted in any particular order by the form.)

You'll probably want to append the count to the field name on the dynamic fields, in order to be able to know which fields go together in a row. You can then iterate over the posted fields in your form handler and use the index at the end of the field name to match the related fields together. I've done this many times, it's a bit tricky, but absolutely doable.

The alternative solution is to name your text areas "notes[]" instead of "notes". Then, in your form handler, $_REQUEST['notes'] will be an array of all the "notes" fields in your form. This works fine if there's only one dynamic field to deal with; however, if each dynamically-added row has multiple fields related to one another, you won't be able to tell which fields go together in a row. In that case, you'll have to append the index to the form field name.

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

Comments

0

To my knowledge, the PHP script executes at server side and the code <?php $count++; ?> will not be executed in loop. It will be executed once at server side. You will have to do the other way round, iterate on the list in PHP script and attach the event accordingly.

I have no PHP skills, so can't provide a code sample.

Hope this helps.

Comments

0

PHP code is only executed server side before it is ever sent to the browser, you'll need to get a row count using javascript.

Try something like this:

var cnt = $('.mainTable > tbody > tr').length + 1;
var row = '<tr><td>Chronic Pain Referral</td><td>Chronic referral</td><td><input type="hidden" name="count" value="' + cnt + '"><textarea name="notes"></textarea></td></tr>';

Now, if you are submitting all rows at once you'll need to add a [] to each row's field names. This informs PHP that the data is an array. This is probably where your error is coming from when using foreach, you aren't passing it an array.

<textarea name="notes[]"></textarea>

Then when you get the form submission in PHP you don't need the count and can simply do:

<?php
if (!empty($_POST['notes'])) {
  foreach ($_POST['notes'] as $note) {
     ...
  }
}
?>

An example of Adrian's way:

<textarea name="notes_"' + cnt + '"></textarea>

PHP:

<?php
if (!empty($_POST)) {
  foreach ($_POST as $key => $value) {
    $pieces = explode('_', $key);
    $field = $pieces[0];
    if ($field == 'notes') {
      $row = $pieces[1];          
      // Process data... 
    }
  }
}
?>

1 Comment

Adrian is right about appending the count to the field name instead of a separate field. My way assumes you are simply inserting multiple rows of new data. If you need to uniquely identify rows you would need to append the cnt to the field name.

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.