ajax passing parameter to php file not working

425

UPDATE: the php and javascript was updated/changed, based on the intial user comments:

This javascript does not get the response... what to change? How to make the javascript get the success or failed response from the php?

       jQuery('.btndel').on('click', function(e){
          e.preventDefault();
          alert("click");
          jQuery.ajax({
            url: '/file.php',
            type:'POST',
            dataType: 'json',
            data: { p: 'test' },
            success: function( data ) {
                response = JSON.parse(data);
                if (response['success']){
                     alert("success");
                } else {
                     alert("fail");
                }
            },
        });
    });

file.php

   <?php

    if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(404);
    error_log( '404 Not Found');
    exit;
   }

    header('Content-type: application/json');
    error_log("=====".$_POST['p']);
    if (!isset($_POST['p'])) {
       http_response_code(400);
       error_log( "NO PARAM");
       exit;
    }

   $response = array();
   if ($update_success === false) {
       $response['status'] = 'error';
   } else {
       $response['status'] = 'success';
   }

    exit(json_encode($response));
    ?>

what is wrong or missing?

40

Answer

Solution:

jQuery(document).on("click", ".button", function(e) {
   e.preventDefault();
   jQuery.ajax({
      url: '/file.php',
      type: 'post',
      data: {
         p: "work"
      },
      success: function(){
          alert("not here");
      }
    });
});

try to pass parameters like this!

https://qiita.com/art_porokyu/items/ffb08251a83e4fe46d57

People are also looking for solutions to the problem: php - Limit the number of loops through array

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.