-
how to get questions with jquery,ajax,php
almost 9 years ago
-
over 8 years ago
Hi
Please try this Ajax call below:-
Note :- Please include jquery library file
$.getJSON('getQuestions.php', function(data) { $.each(data, function(index, element) { $('body').append($('', { text: element.name })); }); });
In getQuestions.php we will write the code to get questions from database and return data in json to the Jquery
getQuestions.php
function getQuestions() { include 'connect.php'; $query = "SELECT * FROM questions LIMIT 10"; $execute = mysqli_query($con, $query); if (mysqli_num_rows($execute) > 0) { while ($res = mysqli_fetch_assoc($execute)) { $questions [] = $res; } } return json_encode($questions); }
Make a new file for Connection i.e connect.php connect.php
$con = mysqli_connect('localhost', 'USERNAME', 'PASSWORD', 'DATABASE NAME') or die("Check Connection");
Please replace undercode code "_" with underscore symbol i.e _
Please let me know if it works or not
-
1 Answer(s)