biztalk-mapper

Put JavaScript variable into PHP with AJAX?


Here's a some of my JavaScript

function myFunction() {
    var finalMove = "a1";
    $.post("index.php", {postFinalMove:finalMove});
];

Here is my PHP code. The commented lines are just two of the things I've tried.

<?php
session_start();

$db = mysqli_connect("localhost","myUsername","myPassword","abraDB");

if (mysqli_connect_errno())
{
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
}

//if (isset($_POST['postFinalMove']))
//{
//  $turn = ($db, $_POST['postFinalMove']);     
//  echo $turn;
//}

//if ($_POST['postFinalMove'])
//{
//  $turn = ($db, $_POST['postFinalMove']);     
//  echo $turn;
//}     

if ($_POST['logout'])
{
    session_start();
    $_SESSION = array();
    session_destroy();
}

mysqli_close($db);
?>

As soon as I uncomment some of the code I've tried, my entire webpage is blank with no errors and no source code, which makes this hard for me to debug. Javascript function fires just fine, its my index.php page that crashes. PHP code was working great until this. Thanks in advance


Solution

  • You should be able to do the following in your PHP script:

    if(isset($_POST['postFinalMove'])){
       echo $_POST['postFinalMove'];
    }
    

    This line is probably causing the error:

    //  $turn = ($db, $_POST['postFinalMove']); 
    

    That statement does not make any sense. What are you trying to store in $turn? If you want to store the $_POST['postFinalMove'] you would have:

    $turn = $_POST['postFinalMove'];