vivin
vivin

Reputation: 1002

Update MY SQL DB Table from jQuery

Based on the user input's, i calculate some values on my submit action of my form. I have to persist these values in my backend DB. I use PHP for my server side scripting. Please let me know the best practice for doing this. It is a single page application and i use .load("Report.html"); to show the summary page.

Just thinking aloud, can i fetch the row(to be updated) from DB, json_encode, update the json object in jQuery, decode it, then update in DB?

Please help...

My submit button code...

$('form').on('submit', function(event)
    {
        event.preventDefault();

        //CALCULATE SCORE
        var noOfCorrectAnswers = 0;
        var noOfQuestionsViewed = 0;
        $.each(questionsArray, function(i, item)
        {
            if(item.correctOption == item.selectedAnswer)
            {
                noOfCorrectAnswers++;
            }

            if(item.isQuestionViewed == 'YES')
            {
                noOfQuestionsViewed++;
            }

        }); 

        alert(noOfQuestionsViewed);
        $('#sampleDiv').load("UserReport.html");        
    });

Upvotes: 0

Views: 1127

Answers (2)

Bilal Akil
Bilal Akil

Reputation: 4755

Run some AJAX passing all of the information you need (which may even be none depending on your use case) from the client-side to your server-side PHP. Your PHP script can fetch things from the database if necessary, make any calculations and/or manipulations and then store the information back in the DB.

If you need to return information to your client-side after updating the database then try returning a JSON object (by just printing the code out in the proper format) from your PHP script before exiting with whatever your JS needs.

Do note that this should be all done asynchronously, so you need to setup your AJAX callback function to handle any information that's returned from your PHP script. If you want to do it synchronously, go for it - but you asked for best practices :P

Looks like you're using jQuery - here's the documentation on AJAX

Raunak Kathuria's answer provides some same code

Upvotes: 1

Raunak Kathuria
Raunak Kathuria

Reputation: 3225

On form submit make ajax call to set database in the db and access the json

$('form').on('submit', function(event)
{ ...
 alert(noOfQuestionsViewed);

$.ajax({
    url: "yourphp.php", // php to set the data
    type: 'POST',
    data: 'yourparams', // all the input selected by users
    dataType: json
    success: function(json){

        //here inside json variable you've the json returned by your PHP
        // access json you can loop or just access the property json['sample']
         $('#sampleDiv').load("UserReport.html", function () {
            // its callback function after  html is loaded
            $('#someid').html(json['sample'));
         });

    }
})

You can also use the done callback of ajax

PHP

yourphp.php

Set the values here in db running the desired query and return values using

<?php

// your db ooperations will come here

// fetch the db record 

// return the db records in json

$responseVar = array(
       'message'=>$message,
       'calculatedValue'=>$calculated
  );
echo (json_encode($responseVar)); 
?>

Upvotes: 0

Related Questions