Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • how to get questions with jquery,ajax,php

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 718
    Answer it

    have a quiz script. My script contains quiz.js, getquestions.php, insertresults.php. In quiz.js:

    var i = 1;
    var sec = 20;
    var timetaken = 0;
    var answer;
    var points;
    var result = .5;
    var score = 0;
    var f = new Date();
    var duration;
    var containerWidth;
    var decrement;
    var CallingSpeed = 30;
    var questionid;
    var AnswerID;
    var userid = 0;
    var testid = 0;
    var defaulttime;
    var maxRatingScale = 2000;
    var minRatingScale = 1000;
    var answer1;
    var answer2;
    var answer3;
    var AnswerID = "1";
    
    function getQuestion() {
        testid = document.getElementById("Test").value;
        userid = document.getElementById("User").value;
        $.get("Questions.php", { no: 1, testid: testid, userid: userid }, function (data) {
            if (data != null) {
                $("#divTest1").html(data);
                onSuccess(data);
            }
            else {
                getSummary();
    
            }
    
    
        })
    }
    

    I am new to php. How to get questions from getquestion.php and what is code for getquestion.php?

 1 Answer(s)

  • 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

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: