2

I have a site on which I have two values of which I want to submit to a PHP script. I would like this to happen whenever a specific action is done by the user. It has to be "silent", as in it has to just run in the background, hence the reason I want to submit it from AJAX to PHP. I have a code, which seems right, but somehow doesn't work. I have used the line

success: function () {
                        alert('data was submitted');
                    }

To check whether it even submits the data and it doesn't reach this, as the alert isn't showing up.

HTML / Ajax

<!DOCTYPE html>
<html lang="en">

  <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script>
        function ch() {

            var customID = document.getElementById('hiddenCustomerValue').innerHTML;    
            var customEmail = document.getElementById('customerEmail').innerHTML;   
            var dataString = 'customID='+customID+'&customEmail='+customEmail;

            $.ajax({
                    type:"POST",
                    url: "testSilence.php",
                    data:dataString,
                    cache: false,
                    success: function () {
                        alert('data was submitted');
                    }
            });
            return false;
        }
    </script>

  </head>

  <body>

       <p id="hiddenCustomerValue" style="color:white;">customerKeyID</p>
       <p id="customerEmail" style="color:white;">test</p>
       <button id="send_btn" onclick="ch()">Submit</button>

 </body>

PHP

<?php

$customerID = $_POST['customID'];
$customerEmail = $_POST['customEmail'];

?>
5
  • 1
    "the alert isn't showing up" - Then what is happening? Take a look at your browser's debugging tools. Is there an error on the console? Is the JavaScript code executing at all? What happens when you step through it in the debugger? Is the AJAX request made? Does it contain the data you expect? What is the server's response? There's quite a bit of debugging you can do here, you'd do well overall to familiarize yourself with the debugging tools in your browser. Commented May 29, 2018 at 15:05
  • 1
    If the success handler does not run then you are not getting a 200 response from the server. Use the network tab of the console to see the response of the request along with the status code and exact response text. Please let us know what that is. Commented May 29, 2018 at 15:05
  • 1
    Instead of passing a data string, consider passing an object. Commented May 29, 2018 at 15:06
  • 1
    Secondly, those spaces in the data string might not be helpful either. Commented May 29, 2018 at 15:07
  • 1
    Thank you for all the suggestions, @Script47 I have changed the datastring to not have spaces and the alert is now showing. Now there are just some issues with the PHP it seems, but that is out of this question. Again, thank you. Commented May 29, 2018 at 15:12

1 Answer 1

1

For sake of marking this as answered, as suggested in my comment, remove the spaces in your data string that is being passed as that changes what the keys will represent on the server side.

As suggested in my earliest comment too, it would be better to use an object instead:

data: {
    'key' : 'val',
    'key2' : 'val-2'
}

In your instance it would be:

data : {
    'customID' : customID,
    'customEmail' : customEmail
}

Note: Regarding your PHP code, I'd definitely do some validation there to ensure that data is being passed by using isset, empty and trim along with other validation that you feel would be suitable. Remember, requests can be spoofed.

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.