skip to Main Content

I have an input field that I am trying to fill with an AJAX request to my SQL server using PHP, but it is not working. The console.log spits out the proper array title: “”
description: “”, but it doesnt add the data to the text input field so I can see it. Ive looked at other answers, but they do not suffice. Thanks!
HTML

<input type="text" class="inputtext" name="task-title-reg" value="">
<textarea class="textbox" name="task-description-reg" rows="4" cols="50" value=""></textarea>

AJAX REQUEST

                $(".task-listing").click(function() {
                $.ajax({
                    type: 'POST',
                    url: 'task-info-get.php',
                    dataType: 'json',
                    data: 'pid=' + $(this).attr("id"),
        success: function (response) {
            console.log(response);
            $("task-title-reg").val(response);
            $("task-description-reg").val(response);
        }
                })
            });
            });

PHP

<?php
include 'sqlconnection.php';
$conn = OpenCon();

$stmt = $conn->prepare('SELECT title,description FROM tasks WHERE pid='.$_POST['pid']);
$stmt->execute();
$stmt->bind_result($title,$description);
while($stmt->fetch()) {
    $output[]=array(
        'title' => $title,
        'description' => $description

    );
}
$json=json_encode($output);
echo $json;

$stmt->close();
CloseCon($conn);
?>

2

Answers


  1. I recommend you use the class to to input the value.

    $('.inputtext').val('Your value here!')

    Login or Signup to reply.
  2. $json=json_encode($output);
    echo $json;
    

    Due to above code and dataType:'JSON', response is javascript Object.

    Access strings by providing keys to javascript Object:

    function (response) {
                console.log(response);
                $(".task-title-reg").val(response.title);
                $(".task-description-reg").val(response.description);
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search