1

I have a JavaScript variable, and I want to send via POST to a PHP page and I want to display the new page. I tried this solution, but it seems not work. I have checked within Wireshark, and the variable (key and value) are sent correctly, but the page, doesn't show anything.

$(document).ready(function() {
            $("#tbID tr").click(function() {
                $(this).addClass('selected').siblings().removeClass('selected');
                var value = $(this).find('td:nth-child(2)').html();

            });

        });
        function send(value) {
            $.post("newPhp.php", {
                key : value

            }).done(function(data) {
                location.href="newPhp.php";
            });
        }

The newPhp.php:`

    if(!empty($_POST["key"])){
        $value  = $_POST['key'];`
//something with $value
}

So what is the problem with this code ? Are there any simpler solution ?

5
  • You're running newPhp.php twice. $.post sends a POST request to it with a parameter. When it replies to that, location.href redirects to the page with a GET request and no parameters. Commented Apr 23, 2015 at 20:48
  • You should either submit the form normally, or the .done function should display the output in the current page instead of redirecting. Commented Apr 23, 2015 at 20:50
  • Yes, that's what I suppose is the key problem. So how Can I pass the parameter, and display the new page ? Commented Apr 23, 2015 at 20:50
  • That's what normal form submission does. Why are you using AJAX? Commented Apr 23, 2015 at 20:51
  • Looks like duplicate of stackoverflow.com/questions/2054705/… Commented Apr 23, 2015 at 20:55

1 Answer 1

-2
function send(value) {
    $.post( "newPhp.php",
        {key:value},
        function( data ) {
            location.href="newPhp.php";
        }
    );
}
Sign up to request clarification or add additional context in comments.

3 Comments

It doesn't work ! The newPhp.php doesn't display what I got from the post.
This code work. Check what Your ajax response is. And check browser console.
The problem, is that I open twice the page, but only the first time the post works/sends

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.