最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - sending post data with jquery on form submission - Stack Overflow

programmeradmin3浏览0评论

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

<form method="post">
   <input type="text" name="name"/>
   <input type="submit"/>
</form>

How will I be able to use JavaScript/jQuery to inject an additional POST data. This would be easier if I only submit the form via ajax using any of the functions like $.post(), $.get(), or $.ajax(). But what I want to do here is to submit the form not via ajax.

The additional data is in JavaScript. I'm thinking of just injecting a new hidden field into the DOM in which I will pass in the data so that it gets submitted with the form, but is there other way of doing it?

Please comment if you need more code/explanation.

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

<form method="post">
   <input type="text" name="name"/>
   <input type="submit"/>
</form>

How will I be able to use JavaScript/jQuery to inject an additional POST data. This would be easier if I only submit the form via ajax using any of the functions like $.post(), $.get(), or $.ajax(). But what I want to do here is to submit the form not via ajax.

The additional data is in JavaScript. I'm thinking of just injecting a new hidden field into the DOM in which I will pass in the data so that it gets submitted with the form, but is there other way of doing it?

Please comment if you need more code/explanation.

Share Improve this question asked Jun 2, 2012 at 14:26 Wern AnchetaWern Ancheta 23.3k38 gold badges105 silver badges140 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 13

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>
<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)

  1. 暂无评论