sending post data with jquery on form submission

后端 未结 2 718
梦毁少年i
梦毁少年i 2021-02-09 00:52

If I have this form which submits the post data to the page itself:

相关标签:
2条回答
  • 2021-02-09 01:11

    Rather than using a submit button, use a regular button and control the submission via Javascript. In your javascript, you need to serialize the form and pass it as data along with anything else you want.

    <script>
    
        $(document).ready(function() {
    
           var extra_data = "This is more stuff to send";
    
           $('#submit').click(function() {
              $.ajax({
                 type: "POST",
                 url: "form.php",
                 data: {'form': $("#my_form").serialize(), 'other': extra_data},
                 success: function(msg) {
                    alert("Form Submitted: " + msg);
                 }
              });
    
           });
    
        });
    
    </script>
    
    <form id="my_form" method="post">
      <input type="text" name="name" />
      <input type="button" id="submit" />
    </form>
    
    0 讨论(0)
  • 2021-02-09 01:13
    <head>
        <script src="jquery/jquery-1.11.1.min.js"></script>
        <script>
        $(function () {
            $('#form_id').on('click','#submit', function(e){
                e.preventDefault();
                $.ajax({
                    type: "POST",
                    url: "get_Ajax.php",
                    data: $('#form_id').serialize(),
                    success: function(data) {
                        alert(data);
                    }
                });
                return false;
            });
        });
        </script>
    </head>
    <form method="post" id="form_id">
    <input type="text" name="ime">
    <input type="button" id="submit" name="submit" value="Send">
    </form>
    

    ** get_ajax.php**

    <?php
    $ime = $_POST['ime'];
    
    echo "Your name is $ime";
    ?>
    
    0 讨论(0)
提交回复
热议问题