简体   繁体   中英

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>

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

The ID attribute does not replace the name attribute

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.

Woops,你错过了<input type='hidden'> :)中的name='count'发生在我们最好的人身上

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM