0

I'm trying to pass two number and check if their product is true or false. I can see call made successfully in network tab and when i click that link, output is correct to. But i m stuck at retrieving that result. It doesn't show anything in data1.

function call(){
    console.log(fun);
$.ajax({
  url: "http://localhost/mt2/checkanswer.php",
  dataType: "jsonp",
  type: "POST",
           //window.alert("what");
  data: {
    num1:2,
      num2:2,
      answer:5

  },
  success: function( data1 ) {
     console.log(data1);
    $( "#timeDiv" ).html( "<strong>" + data1 + "</strong><br>");
  }

<?php

  // get two numbers and the answer (their product) and return true or false if the answer is correct or not. 
  // using this as an api call, return json data
  // calling <your host>/checkanswer.php?num1=4&num2=5&answer=20 will return true
  // calling <your host>/checkanswer.php?num1=4&num2=5&answer=21 will return false

  if(isset($_GET['num1']) && isset($_GET['num2']) && isset($_GET['answer']) && is_numeric($_GET['num1']) && is_numeric($_GET['num2']) && is_numeric($_GET['answer'])) {

    $product = $_GET["num1"] * $_GET["num2"];

    if ($product === intval($_GET['answer'])) {
      $result = true;
    } else {
      $result = false;
    }
    header('Content-type: application/json');
    echo json_encode($result);

  } 
?>

https://drive.google.com/open?id=1ocF344ZxG3HXJR0WQha1kOoVM9bCepnI "console"

11
  • Are you outputting anything is checkanswer.php? What does console.log(data1); show? Maybe include the code in the ajax'd file too. Commented Apr 13, 2018 at 17:03
  • if data1 is empty then checkanswer.php doesnt output anything Commented Apr 13, 2018 at 17:05
  • data1 doesn't show anything in console, but if try calling directly with php fit showsoutput. Even network tab shows call being made. Commented Apr 13, 2018 at 17:09
  • 3
    Your ajax is submitting as a POST request and your PHP is looking for a GET request- $_GET[..] should be $_POST[..]. Commented Apr 13, 2018 at 17:19
  • 2
    You can not do jsonp with post Commented Apr 13, 2018 at 17:26

1 Answer 1

1

The issue is your Javascript is submitting the data via JS as a post request and your PHP is looking for a get request.

if(isset($_GET['num1']) && isset($_GET['num2']) && isset($_GET['answer']) && is_numeric($_GET['num1']) && is_numeric($_GET['num2']) && is_numeric($_GET['answer'])) {
 ..
}

So either change method: 'POST' to method: 'GET' or change $_GET[..] to $_POST[..].

Also that's one wild if statement. You could break it up so it's not so long and isn't as hard to read. This also allows you to add some additional information based on where your code 'fails.'

if ( isset($_GET['num1'], $_GET['num2'], $_GET['answer']) ) {

  if ( !is_numeric([$_GET['num1'], $_GET['num2'], $_GET['answer']]) ) {
    // Our numbers aren't numeric!
    $message = 'Not all variables are numeric';
    $result = false;
  } else {
    $message = 'We did it!';
    $result = $_GET['num1'] + $_GET['num2'] == $_GET['answer'];
  }

} else {
  // We didn't have all of our request params passed!
  $message = 'We didn\'t have all our variables';
  $result = false;
}

header('Content-type: application/json');
echo json_encode([ 'message' => $message, 'result' => $result]);

Edit

Based on epascarello's comment remove dataType: 'jsonp'.

Sign up to request clarification or add additional context in comments.

3 Comments

Its not even getting call, when i remove datattype: 'jsonp'
@ManjinderGrewal that is left for you to debug. The question is have you modified your ajax code to send a GET? or rather modify your PHP code to receive a POST instead of GET?
I changed call method to GET

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.