donmqryh49993
2018-06-28 13:13
浏览 372

使用jquery在表单提交上添加post变量值

I have to send some POST variables with values when submitting the form. I have to send variable_1 and variable_2

In result.php I need to get the value of $_POST['variable_1'] and $_POST['variable_2']. How can I do this?

<form action="result.php" method="post">      
  <input type="submit" name="submit">
</form>
$('form').submit(function () { 
  var variable_1 = "test";
  var varible_2 = "test2";       
});

Please help.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanjing9339 2018-06-28 13:52
    已采纳

    i got the answer for my question

    $('form').submit(function () {
        var input = $("<input>")
                       .attr("type", "hidden")
                       .attr("name", "variable_1").val("test1");
        var input_2 = $("<input>")
                       .attr("type", "hidden")
                       .attr("name", "variable_2").val("test2");
        $('form').append($(input));
        $('form').append($(input_2));
    });
    
    已采纳该答案
    打赏 评论
  • doujingxi3356 2018-06-28 13:37

    I achieve what you need using the below.

    Add a form and tag with an id. Add a standard button for submitting the form.

       <form id="frMyForm" method="post">      
            <input id="btnSubmit" type="button" name="submit">
        </form>
    

    Next capture the submit buttons click event, append the extra info to the form, then post the form data to the desired Controller/Action.

    $(document).on("click", "#btnSubmit", function () {
        //Serialize Form
        var myData = $('#frMyForm').serialize();
    
        //Append custom value
        var variable_1 = "test";
        myData = myData + "&var1=" + variable_1; //You could get this from a hidden field perhaps.
    
            $.ajax({
                type: "POST",
                url: "/Controller/Action",
                data: myData
            });
        });
    

    Hope that helps

    打赏 评论
  • dongxuxian1123 2018-06-28 13:44

    You can Use These :

    <form action="result.php" method="post">
          <input type="hidden" name="variable_1" value="test">
          <input type="hidden" name="variable_2" value="test2">
          <input type="submit" name="submit">
        </form>
    
    打赏 评论

相关推荐 更多相似问题