Javascript redirect page with post data

JavaScript redirect page with post data



The answers here a sample code that working fine with me.

This code work with Ajax response.

And for the example.

//---------- make sure to link to your jQuery library ----//

<script type="text/javascript" >

    var form = $(document.createElement('form'));
    $(form).attr("action", "testpage.php");
    $(form).attr("method", "POST");
    $(form).css("display", "none");

    var input_employee_name = $("<input>")
    .attr("type", "text")
    .attr("name", "employee_name")
    .val("shahin" );
    $(form).append($(input_employee_name));


    var input_salary = $("<input>")
    .attr("type", "text")
    .attr("name", "salary")
    .val("1000" );
    $(form).append($(input_salary));

    form.appendTo( document.body );
    $(form).submit();

</script>


If all is done well, you shall be redirected to testpage.php and you can use POST to read passed values of employee_name and salary.


On testpage.php you can get your values thus.

$employee_name = $_POST['employee_name'];
$salary = $_POST['salary'];

Make sure you sanitize your passed values.

Displaying an ajax response with jquery send output

success:function(data) {
    if(data) {   // DO SOMETHING     
        $('#myid').html(data[0].value);
    } else { // DO SOMETHING }
  }

<div id="myid"></div>

Javascript redirect page with post data Javascript redirect page with post data Reviewed by Md Shahinur Islam on September 21, 2022 Rating: 5

No comments:

Powered by Blogger.