Pass data to database using javascript Onclick

后端 未结 3 1454
甜味超标
甜味超标 2021-01-16 14:32

I am a real noob when it comes to javascript/ajax, so any help will be very appreciated. In reference to this question:

Updating a MySql database using PHP via an o

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-16 15:24

    Phil is not passing any values to the script. He's simply sending a request to the script which most likely contains logic to 'update' the score. A savvy person taking his test though could simply look at the HTML source and see the answer by checking to see what the anchor is doing.

    To further nitpick about his solution, a set of radio buttons should be used, and within the form, a button or some sort of clickable element should be used to send the values to the server via an ajax request, and the values sent to the server can be analyzed and the status of the answer sent back to the page.

    Since you're using jQuery, the code can be made unobtrusive as seen in the following example:

    $('#submit_answer').click(function() {
        var answer = 'blah' // With blah being the value of the radio button
        $.get('updatescore.php',
        {'value': answer},
        function(d) {
            alert('Your answer is: ' + d') // Where d is the string 'incorrect' or 'correct'
        }
    });
    

    Enjoy.

提交回复
热议问题