0

When I tried to used ajax to post data from javascript file to php file, there was nothing displayed on php file after using

$_POST['userinput']

Here is the javascript file:

 searchBox.addListener('places_changed', function() {
        var places = searchBox.getPlaces();
        userinput = places[0].name.toString();  // Get user input from search box

        // Pass data to userinput.php via ajax
        $.ajax({
            url: 'userinput.php',
            data: {userinput : userinput},
            type: "POST",
            success: function (result) {
                alert(JSON.stringify(result));
            }
        });

    });

php file:

    if (isset($_POST)) {
        $servername = "localhost";
        $username = "XXXXXXX";
        $password = "XXXXXXXXX";
        $dbname = "CALIFORNIA";

        $city = $_POST['userinput']; // Nothing is posted here

        // Create connection
        $conn = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 1);


        $sql = $conn->prepare("SELECT State FROM  CITY as C WHERE   C.City_name=$city");
        $sql->execute();

        $result = $sql->fetchAll();
        $json = json_encode($result);
        echo $json;
    }

I was able to connect to the mysql database. However, there was no data posted from javascript file to php. I'm not sure what to do from this point. the value $city print out nothing. On the client side it printed out an empty object.

11
  • 1
    alert(userinput); and check what it returns . it will alert your search text input or not ? Commented Oct 24, 2016 at 5:57
  • 1
    data: {'userinput' : userinput}, quotes missed Commented Oct 24, 2016 at 5:57
  • 1
    @Anant quotes or no quotes do not change anything in this case. Quotes are useful only when setting a key name that is note a valid JS identifier (Ex: not-valid, because of the dash) Commented Oct 24, 2016 at 6:01
  • 1
    show us value of userinput Commented Oct 24, 2016 at 6:01
  • 1
    var places = searchBox.getPlaces(); userinput = places[0].name.toString(); varify userinput variable has proper value or not Commented Oct 24, 2016 at 6:02

1 Answer 1

1

in your ajax function try setting dataType property

$.ajax({
            url: 'userinput.php',
            data: {'userinput' : 'userinput'},
            type: "POST",
            dataType: "text", // add this property
            success: function (result) {
                alert(JSON.stringify(result));
            }
        });
Sign up to request clarification or add additional context in comments.

2 Comments

it turn out to be that PHP does not allow RAW_POST anymore. the code is fine though

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.