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 ?

I love coding
  • 1,183
  • 3
  • 18
  • 47
  • 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. – Barmar Apr 23 '15 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. – Barmar Apr 23 '15 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 ? – I love coding Apr 23 '15 at 20:50
  • That's what normal form submission does. Why are you using AJAX? – Barmar Apr 23 '15 at 20:51
  • Looks like duplicate of http://stackoverflow.com/questions/2054705/non-ajax-jquery-post-request – Artem Apr 23 '15 at 20:55

1 Answers1

-2
function send(value) {
    $.post( "newPhp.php",
        {key:value},
        function( data ) {
            location.href="newPhp.php";
        }
    );
}
Adam Kaczmarek
  • 146
  • 2
  • 10