I have fetched the variables from the php file through Ajax and now I want to write the values to a div tag. But Ajax doesn't seem to be working.
Here's my script.
<script>
var card_number;
var book_issued;
function here(card_numb) {
alert("pk!");
$.ajax({
url: 'details.php', //This is the current doc
type: "GET",
dataType: 'json', // add json datatype to get json
data: ({
card_number: card_numb
}),
success: function(data) {
console.log('card_number:' + data.card_number + 'book_issued:' + data.book_isued);
$('#adiv').html('hello world!');
}
});
}
For now I am trying to pass 'hello world' string but it ain't working.
This is my details.php
<?php
if (isset($_GET['card_number'])) {
$card_number = $_GET['card_number'];
$query = "Select * from users where card_number = '" . $card_number . "'";
$query_run = mysqli_query($link, $query);
$row_numb = @mysqli_num_rows($query_run);
if ($row_numb == 0) {
echo "<div class='bdiv1'>No such number found!</div>";
} else {
$row = mysqli_fetch_assoc($query_run);
$book1 = $row['user_name'];
$arr = array(
'isued_book' => $book1,
'card_number' => $card_number
);
print_r(json_encode($arr));
}
HTML page card_number1.php
<input type="text" id="lab_card_number" name="lab_card_number" placeholder="Card Number" class="input" maxlength="5">
Then again a script tag to send the request to ajax script.
<script>
document.getElementById('lab_card_number').onkeydown = function(event) {
if (event.keyCode == 13) {
var card_numb = document.getElementById("lab_card_number").value;
// alert(card_numb);
here(card_numb);
}
}
</script>