简体   繁体   中英

Send text from textbox to PHP using AJAX

I'm trying to send input from a textbox to my PHP script, but it's not working.

When I change (in my PHP file)

$naam = $_POST["naam"];

to

$naam = "Marvin";

It's working!

AJAX/HTML

<td><lable>Naam</lable></td>
<td><input name="serarchName" class="tagert" type="text" id="searchName"/></td>   

    <script>
             $(document).ready(function(){
            $("#searchName").change(function(){
                 var name = $("#searchName").val();
        $.ajax({
            'url': 'ontwikkelpunten.php',
            'method': 'post',
            'data': 
            {
                 naam: $("#searchName").val()
            },
            'dataType': 'json'
        }).done(function(data){
            console.log(data);

                });
            });
        });

PHP

    $naam = $_POST["naam"];

$stmt = $conn ->prepare("SELECT * FROM ontwikkelpunten WHERE naam = "$naam");
$stmt ->execute();
$myarr = array();
while($data = $stmt -> fetch()){
    $myarr[] = $data;
}
echo json_encode($myarr);

You're not substituting the parameter into the query correctly.

$naam = $_POST["naam"];

$stmt = $conn ->prepare("SELECT * FROM ontwikkelpunten WHERE naam = :naam");
$stmt->bindParam(":naam", $naam);
$stmt ->execute();
$myarr = array();
while($data = $stmt -> fetch()){
    $myarr[] = $data;
}
echo json_encode($myarr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM