Ajax功能学习 [英] Ajax function learning

查看:73
本文介绍了Ajax功能学习的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要Ajax的帮助. 我在网上获得了此代码. 此功能是检查contact.php

I need help in Ajax. I got this code online. This function is to check the contact.php

我有几个问题,以便有人可以帮助我.
我的问题:
1.此代码是否正确并且可以运行?
2.有人可以解释一下第4行和第5行中的功能是什么.似乎它会将数据发送到contact.php,但返回的是什么?

I have some few question so someone could assist me.
My questions :
1. Is this code good and possible to run ?
2. Can someone explain me what does the function in line 4 and line 5 does.It seems it send data to the contact.php but what is it returning?

Ajax:

var validateEmailForm = {
dataType: 'json',
        submit: function(form) {
        var redirect = false;
         $.ajax('contact.php', {data:{'email':form.email.value}}).done(function(data) {
       if ( typeof(data) == 'object' ) {
                    if ( data.status == 'valid') {
                        form.submit();
                    } else if(data.status !=='valid' {     
               alert('The e-mail address entered is wrong.');
                      }
                } else {
                    alert('Failed to connect to the server.');
                    }
            }
        }
    }

Contact.php:

Contact.php:

<?php
error_reporting(0);

$email = $_POST['email'];
if (isset($_$POST['email']))
{
  // How to return valid to the ajax
} else {
  // How to return invalid to the ajax.
}

?>

推荐答案

AJAX比听起来容易得多.您只需要看几个很好的例子.

AJAX is much easier than it sounds. You just need to see a few good examples.

尝试以下方法:

一个简单的示例

更复杂的示例

根据下拉列表1中的选择填充下拉列表2

https ://stackoverflow.com/questions/25945137/php-fetch-content-from-one-form-and-update-it-in-other-form/25954450#25954450

以上示例演示了一些事情:

The above examples demonstrate a few things:

(1)一个AJAX请求有四种格式-完整的$.ajax()结构和三种快捷方式结构($.post()$.get()$.load())

(1) There are four formats for an AJAX request - the full $.ajax() structure, and three shortcut structures ($.post(), $.get(), and $.load() )

直到您精通AJAX,我建议您使用正确格式的$.ajax()代码块,上面的示例演示了这一点.这样的代码块如下所示:

Until you are pretty good at AJAX, I suggest using a correctly formatted $.ajax() code block, which is what the above examples demonstrate. Such a code block looks like this:

$('#formID').submit({
    $.ajax({
        type: 'post',
         url: 'contact.php', 
    dataType: 'json',
        data: 'email=' + form.email.value
    }).done(function(data) {
        if ( typeof(data) == 'object' ) {
            if ( data.status == 'valid') {
                form.submit();
            } else if(data.status !=='valid' {     
                alert('The e-mail address entered is wrong.');
                return false;
            } else {
                alert('Failed to connect to the server.');
                return false;
            }
        }
    });
});

(2)在$.ajax()代码块中,data:行指定了发送到PHP处理器文件的数据.

(2) In an $.ajax() code block, the data: line specifies the data that is sent to the PHP processor file.

(3)dataType:行指定aemx代码块期望从 PHP处理器文件接收回的数据类型.除非另有说明,否则默认的dataType为html.

(3) The dataType: line specifies the type of data that the ajax code block expects to receive back from the PHP processor file. The default dataType is html, unless otherwise specified.

(4)在PHP处理器文件中,数据通过echo命令返回到AJAX代码块.不管该数据是以html,文本还是json的形式返回,它都会被echo返回到AJAX例程,如下所示:

(4) In the PHP processor file, data is returned to the AJAX code block via the echo command. Whether that data is returned as html, text, or json, it is echoed back to the AJAX routine, like this:

<?php

//perform MySQL search here. For eg, get array $result with: $result['firstname'] and $result['lastname']

$out =  '<div id="myresponse">';
$out .= 'First Name: <input type="text" value="' .$result['firstname']. '" />';
$out .= 'Last Name: <input type="text" value="' .$result['lastname']. '" />';
$out .= '</div>';

echo $out;

请亲自尝试上述几个示例,您将了解其工作原理.

Please try a couple of the above examples for yourself and you will see how it works.

不必使用json发送/返回数据.但是,json是发送 array 数据的有用格式,但是如您所见,您可以在PHP端构造完整的html响应并回显完成的标记.

It is not necessary to use json to send/return data. However, json is a useful format to send array data, but as you can see, you can construct a full html response on the PHP side and echo back the finished markup.

因此,要明确回答您的第二个问题,您只需echo返回一些数据. PHP文件的工作是:

So, to definitively answer your second question, you just need to echo back some data. It is the job of the PHP file to:

(1)从AJAX例程接收数据,

(1) receive the data from the AJAX routine,

(2)使用该数据进行某种形式的查找(通常在数据库中),

(2) Use that data in a look up of some kind (usually in a database),

(3)构建响应,然后

(4)echo(不是return)返回对AJAX例程的success:.done()函数的响应.

(4) echo (NOT return) the response back to the AJAX routine's success: or .done() functions.

这篇关于Ajax功能学习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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