1

I've seen this many times before on Stack Overflow and other websites, but none of the previous posts seem to be helping.

Assume the following HTML code:

<input maxlength="50" size="50" id="searchTxt" />

And assume the following AJAX call:

$.ajax({
    type: "POST",
    url: "tdat.php",
    data: {userresponse: $('#searchTxt').val()},
    success: function(){
        $('#searchTxt').val("")
    }
})

What should happen is that the data "userresponse" gets posted to the server, having the value of whatever is inside the input box, and then the input in the box is emptied.

Later, I have the following PHP code in tdat.php:

<?php
    if(isset($_POST['userresponse'])){
        $userResponsePHP = $_POST['userresponse'];
        switch($userResponsePHP){
            case "yes":
            echo "alert(\"hi\")";
            break;
        case "no":
            echo "alert(\"negative!\")";
            break;
        default:
            echo "alert(\"nope.\")";
            break;
        }
    }
?>

The PHP does not respond, even if the AJAX call works perfectly. PHP does not even receive the value of "userresponse". I have checked the file paths, localhost connection, adding quotes to "userresponse", and practically all other minor issues stated in other threads, but the problem persists. The AJAX call clears the value of searchTxt, thus it is a successful send. No errors are shown in the console. Why is this so?

16
  • try instead of isset($_POST['userresponse']) use !empty($_POST['userresponse']) Commented May 10, 2016 at 4:17
  • 1
    I had same problem, I replaced key userresponse with "userresponse", try Commented May 10, 2016 at 4:20
  • Already read about that, and tried it. Nothing changed Commented May 10, 2016 at 4:21
  • what errors do you have in the console? Commented May 10, 2016 at 4:22
  • 1
    Please make some clarifications: 1.When the ajax call is triggered. Commented May 10, 2016 at 4:34

3 Answers 3

2

Try the following:

$.ajax({
    type: "POST",
    url: "tdat.php",
    data: {userresponse: $('#searchTxt').val()},
    success: function(data){
        alert(data.message);
        $('#searchTxt').val("");
    }
})

php:

<?php
    header('Content-Type: application/json');
    if(isset($_POST['userresponse'])){
        $userResponsePHP = $_POST['userresponse'];
        switch($userResponsePHP){
            case "yes":
            echo json_encode(["message"=>"hi"]);
            break;
        case "no":
            echo json_encode(["message"=>"negative"]);
            break;
        default:
            echo json_encode(["message"=>"nope"]);

            break;
        }
    }
?>

Note: if the page is refreshing you aren't doing any ajax

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

2 Comments

i think you need to set dataType:'json'
@guradio jquery will take care of that
0

The following code snippet works, however it's generally recommended not to use eval I'm using eval in this case because you have the JavaScript alert function on the server-side (in the PHP). What you should do is echo the text and then call the alert function on the Javascript side. However, in the interest of changing as little as possible to your code I'm leaving it as is and only making changes in the JavaScript.

2 things to make note of in this example I initialize the text box with one of your accepted values 'yes'. Of course you can change this so the ajax call is made on say a button click (wasn't sure what your use case was)

One small thing you forgot was to read in the data parameter in the success callback.

<script   src="https://code.jquery.com/jquery-2.2.3.min.js"   integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo="   crossorigin="anonymous"></script>
<script>
$( document ).ready(function() {
   $('#searchTxt').val("yes");
       $.ajax({
           type: "POST",
           url: "tdat.php",
           data: {userresponse: $('#searchTxt').val()},
           success: function(data, textStatus, jqXHR) {
              eval(data);
              $('#searchTxt').val("")
           }
        })
    });
</script>
<input maxlength="50" size="50" id="searchTxt" />

Comments

0

Modify your ajax function first as follows:

$('#searchTxt').change(function(){
    $.ajax({
        type: "POST",
        url: "tdat.php",
        data: {userresponse: $('#searchTxt').val()},
        success: function(res){
            $('#searchTxt').val("");
            console.log(res);
        }
     });
 });

On the change event of searchTxt your ajax call will be executed and then check the console.

2 Comments

What are you getting?
why do you have html in your php ?,do you have 2 separate files one with the ajax and one with the php?,how do you trigger your ajax?

Your Answer

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