0

I want to send POST request with jquery AJAX to PHP function but it don't give any respond.

JQuery

$.ajax({
    url: 'ListProduk.php/SelectData',
    type: 'POST',
    success: function(data)
    {
      console.log(data);
    }
  });

PHP

<?php
include 'database.php';

function SelectData(){
  $sql = "SELECT * FROM MsProduk";

  $result = $conn->query($sql);
  while($row = $result->fetch_assoc()){
    $arr[] = $row;
  }
  return json_encode($arr);
}
?>

Any ideas how to fix it?

3
  • url: 'ListProduk.php/SelectData', that doesn't make sense. Look at your developer console, use php's error reporting and check for errors on the query. You'll see what's (not) happening here. Commented May 12, 2017 at 22:02
  • url: 'ListProduk.php/SelectData', that translates to having a folder called ListProduk.php with a sub-folder called SelectData. The SelectData() method's usage is unknown. You've a variable scope issue here. Commented May 12, 2017 at 22:05
  • You can maybe send the function name through GET variable and just call it from within your php file. Commented May 12, 2017 at 22:06

1 Answer 1

3

When not using url rewriting (google mod_rewrite and pretty URL's), your parameters typically are going to be passed as a normal HTTP GET request. Here is an example of how your URL structure might look:

url: 'ListProduk.php?action=SelectData'

And then, in your PHP, you might handle it based on the action that is requested (Note: The action parameter is not something specific to web development, it's just an arbitrary name I assigned. It could be foo=SelectData as well)

if ($_POST['action'] == 'SelectData') {
   // Run the code for selecting data
}

Finally, you wouldn't want to "return" the JSON data. You need to output it with the correct headers. It would look something like this:

header('Content-Type: application/json');
echo json_encode($data);
Sign up to request clarification or add additional context in comments.

Comments

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.