MS-DDOS
MS-DDOS

Reputation: 576

HTML/JQuery form not posting to PHP

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>

Upvotes: 1

Views: 110

Answers (3)

FloatingRock
FloatingRock

Reputation: 7065

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

Upvotes: 0

SachinGutte
SachinGutte

Reputation: 7055

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.

Upvotes: 1

Populus
Populus

Reputation: 7680

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

The ID attribute does not replace the name attribute

Upvotes: 2

Related Questions