-2

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

Jacobjanak
  • 307
  • 1
  • 2
  • 10
  • 1
    What are you trying to accomplish with `$turn = ($db,$_POST['postFinalMove']);` ? That's pretty unusual syntax. I don't think it's valid, but it could just be something I've never seen before. – JakeParis Feb 21 '17 at 02:08
  • this cause the error `$turn = ($db, $_POST['postFinalMove']);` – Beginner Feb 21 '17 at 02:08
  • $db is an object so echoing it why? –  Feb 21 '17 at 02:09
  • If you just want to be sure the variable is sent, just do `echo $_POST['postFinalMove'];` – JakeParis Feb 21 '17 at 02:09
  • you must also on `display_errors` in your php.ini – Beginner Feb 21 '17 at 02:10
  • And just as the other members said that the `$turn` variable line is pretty much unusual as it's not making any sense that what you are trying to do in there??? :D – Umair Shah Feb 21 '17 at 02:11
  • Replace your `$turn` line with this one as and then try again : `$turn = $_POST['postFinalMove'];` – Umair Shah Feb 21 '17 at 02:13
  • Also make sure that you have `display_errors = on` in your `php.ini` or if not so then add this code at the start of your php file and then try again and let us know if you get any error as : `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);` – Umair Shah Feb 21 '17 at 02:15
  • is `myFunction` called as a result of a form submit? – Jaromanda X Feb 21 '17 at 02:18
  • Please before posting question here in `SO` please make sure on your end that the question is just not some syntax issue related question so the members time is not wasted here on such questions..Always take a close look on your code and alongside googling will also help like more than 90% to help you out with your issue and also to let you know how to properly write code..! :D I hope you won't mind it..! – Umair Shah Feb 21 '17 at 02:24
  • it's not polite to ignore people's comments – Funk Forty Niner Feb 21 '17 at 02:35

2 Answers2

0

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'];
Shawn Northrop
  • 5,826
  • 6
  • 42
  • 80
  • Okay thanks for mocking me guys lol I had a functioning login and registration code block so I just kind of copied and pasted a section from that knowing that it probably wouldn't work but youtube/stack overflow didn't offer me much guidance. I'll delete this question soon before I get bullied more. – Jacobjanak Feb 21 '17 at 02:25
  • @Jacobjanak : No offence Dear..May have even happened to us even in the start but it's OK..I appreciate your enthusiasm that you are trying to ask for help which is mostly not the case when it comes to lots of people..! So no offence again..! – Umair Shah Feb 21 '17 at 02:27
  • 1
    I think the real thing to take away here would be to get your error logging / display working (see the original question comments). It will save you lots of headaches in the future! – Shawn Northrop Feb 21 '17 at 02:28
  • Yea thanks for that tip.. I've been using cPanel's text editor instead of my own IDE for my very short php code since it was the easiest way (oops) – Jacobjanak Feb 21 '17 at 02:30
  • Regarding `cPanel` as I think normally the `display_errors = on` but if in your case the errors are off so then you can do exactly just as I said in the above comment to be able to see errors in future..! :D – Umair Shah Feb 21 '17 at 02:32
  • Text editors are one thing but you should have settings in your `php.ini` file that will enable/disable error logging and error displaying on the actual site. If you don't know where your `php.ini` file is you can always run `phpinfo()` on one of your pages. Load that page and it should tell you which `php.ini` file is being loaded. Also checkout: http://php.net/manual/en/errorfunc.configuration.php – Shawn Northrop Feb 21 '17 at 02:33
0

Take a look at the GIF image and you will see for yourself..!

enter image description here

Replace your code with this line as and you are good to go then :

$turn = $_POST['postFinalMove']; 
  echo $turn;

It's just a simple Syntax Issue nothing more than that Dear..! :D

Umair Shah
  • 2,305
  • 2
  • 25
  • 50