2

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.

2
  • 3
    Assuming you're not using AJAX, add the values in hidden input fields within the form Commented Jun 28, 2018 at 13:16
  • A simple google will get you a lot of examples. Commented Jun 28, 2018 at 13:16

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

or use JSON, it's nice
1

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

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.