0

I have a script file where the ajax method has been implemented, which adds a record to a form ...and then i have a php file which serves as the backend. What I am trying to say is we dint declare the record variable in php, we only declared it in jquery part. But how did we actually accessed it using

$record = json_decode($_POST['record']);

What is json_decode and json_stringify in the script file.

main.js

$add_form.submit(function(e) {

        e.preventDefault();

        var fields = ['id', 'name', 'subject', 'theory', 'practical'];
        var record = {};

        for (var index in fields) {

            var field = fields[index];

            if (field == 'id' || field == 'theory' || field == 'practical')
                record[field] = parseInt( $('input#add_'+field).val() );

            else
                record[field] = $('input#add_'+field).val();

        }


        record.total = record.theory + record.practical;



        $.ajax({
            url: '/ab_batch/practice/db/action.php',
            type: 'POST',
            data: {
                action: 'ajaxAddRecord',
                record: JSON.stringify(record)
            },

            success: function(result) {

                if ( 'true' == result.trim() ) {

                    $add_modal.find('.ajax_add_result').text('Student Record Added...').css({
                        color: 'green',
                        display: 'block'
                    }).fadeOut(2500);



                }
                else {
                    $add_modal.find('.ajax_add_result').text('Error Adding Student Record!').css({
                        color: 'red',
                        display: 'block'
                    }).fadeOut(2500);
                }

            },

            error: function() {}

        });


    });

action.php

switch ($action) {
case 'ajaxAddRecord':

        $record = json_decode($_POST['record']);
        print ( $student->addRecord($record) ) ? 'true' : 'false' ;

        break;
}
3
  • Everything seems okay ! did you get any errors ?!! what's really the problem ? Commented Jul 4, 2016 at 15:21
  • You want someone to explain to you how this code works? Commented Jul 4, 2016 at 15:22
  • Yes....I want somebody to explain me some part of the code...we dint declare the record variable in php, we only declared it in jquery part. But how did we actually accessed it using $record = json_decode($_POST['record']); Commented Jul 4, 2016 at 15:24

1 Answer 1

2

The action variable is declared (created) in main.js. You're right.
It is an object which is then used and filled with values.

The $.ajax({ code block sends it stringified to action.php.
"stringified" means converted to a string.
It has to be done to send it to the server-side PHP because an object (or an array) can't be sent directly without converting it to a string.

Then this string, received has $_POST['record'], has to be "decoded" to access the values.
This is what json_decode does... It creates an array with it.

Google theses keywords for more:
jQuery object
JSON.stringify()
json_decode()
PHP array
Ajax example tutorial

Sign up to request clarification or add additional context in comments.

2 Comments

Okay...so the data field in the ajax method is the part that we are sending to action.php or the part that we recieved from action.php ??
Yes, exactly data is the part sent. And the success is the "callback" actions to do with the result from action.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.