1

So I've built hundreds of forms before, all with no issues, but for some reason unknown to me this one doesn't seem to be working. This form is in its infancy to later be plugged into a larger web application. I've checked my code over and over again, checked if PHP is functioning properly, etc, yet I've had no luck fixing my issue.

The problem is that I cannot access any of the data within my form from PHP. Even further, once the form is posted it does not append the form data to the URL. Instead I just get www.example.com/list.php?

Hopefully I've just overlooked something, but the only other thing I can possibly think of is it has something to do with JQuery. If you guys could just take a look at my code and see if there are any glaring errors I would really appreciate it.

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>list</title>

  <script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.js'></script>
  <script>
  $(window).load(function(){
    var counter = 1;

  $('button.remove').click(function(){
    if(counter >= 1){
       $("#cnt" + counter).remove();
       counter--;
       $('#count').attr('value', counter);
    }
  });

  $('button.add').click(function(e){
    e.preventDefault();
    counter++;
$('#count').attr('value', counter);

    var newData = '<tr style="display:none" id="cnt' + counter + '"><td><input type="text" value="Content' + counter + '"/></td></tr>';
    $(newData).appendTo('#myTable').fadeIn('slow').slideDown('slow');
   });
   });  
   </script>
   </head>

   <body>
<button class="add"> + </button><button class="remove"> - </button>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">

  <table id="myTable">
    <tr>
          <td><input type="text" value="Content1" id="cnt1" /></td>
           </tr>
  </table>

    <input type="hidden" id="count" value="10" />
    <input type="submit" value="Submit" />
   </form>

<?php
echo $_GET['count'];
?>

</body>
</html>

3 Answers 3

2

You left out the "NAME" attribute for the input fields....

The ID attribute does not replace the name attribute

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

1 Comment

There we go, I knew it was something stupid. Thanks so much for your help.
1

Well, this works for me after making few changes like -

<td><input type="text" value="Content1" id="cnt1" name="some[]" /></td>

added name to be an array since they are added dynamically.

And from script I changed following line -

var newData = '<tr style="display:none" id="cnt' + counter + '"><td><input type="text" value="Content' + counter + '"/></td></tr>';

to

var newData = '<tr style="display:none" id="cnt' + counter + '"><td><input type="text" value="Content' + counter + '" name="some[]"/></td></tr>';

While printing, as text fields will be an array, you need to use print_r($_GET) instead of echo $_GET;

Just added name=some[] in above line.

You didn't have mentioned name attribute in text field.

Comments

0

Woops, you missed the name='count' in the <input type='hidden'> :) Happens to the best of us

Comments

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.