使用PHP作为后端的jquery中的Ajax方法 [英] Ajax method in jquery using PHP as backend

查看:140
本文介绍了使用PHP作为后端的jquery中的Ajax方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本文件,其中已经实现了ajax方法,该脚本文件将记录添加到表单中...然后我有一个用作后端的php文件.我想说的是,我们在php中声明了记录变量,我们仅在jquery部分中声明了它.但是我们实际上是如何使用

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']);

脚本文件中的json_decode和json_stringify是什么.

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

action.php

switch ($action) {
case 'ajaxAddRecord':

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

        break;
}

推荐答案

在main.js中声明(创建)了动作变量.你说得对.
然后是一个使用并填充值的对象.

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

$.ajax({代码块将其 stringified 发送到action.php.
字符串化" 表示已转换为字符串.
必须将其发送到服务器端PHP,因为无法将对象(或数组)直接转换为未经转换的字符串.

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.

然后,接收到的具有$_POST['record']的字符串必须被解码"以访问值.
这就是json_decode所做的...它使用它创建一个数组.

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这些关键字以获取更多信息:
jQuery object
JSON.stringify()
json_decode()
PHP array
Ajax example tutorial

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

这篇关于使用PHP作为后端的jquery中的Ajax方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆