调用使用AJAX PHP文件 [英] Calling PHP file using AJAX

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

问题描述

我试图调用使用jQuery中的$。阿贾克斯()函数,但它是不工作的PHP文件。下面code运行时点击页面上的按钮时:

I'm trying to call a php file using the $.ajax() function in jQuery however it is not working. The following code is run when a button on the page is clicked:

if($error === false) {
        alert($error);
        $.ajax({
            url: '/new-user.php',
            type: 'POST',
            data: {
                name: $('#name').val(),
                email: $('#name').val(),
                password: $('#name').val()
            }
        });

这是我的表格:

<form onClick="return false;" class="reg-form">

    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label for="name">First Name</label>
                <input type="text" id="name" class="form-control" autofocus="autofocus">
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label for="email">Email Address</label>
                <input type="text" id="email" class="form-control">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label for="password">Password</label>
                <input type="password" id="password" class="form-control">
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label for="password-confirm">Confirm Password</label>
                <input type="password" id="password-confirm" class="form-control">
            </div>
        </div>
    </div>

    <button class="btn trans reg-btn">Sign Up</button>

    <div class="reg-msg">
        <span id="password-error-msg">Passwords do not match.</span>
    </div>

</form>

我已经设置窗体的onClick返回false,这样的形式不重装交房时,这样的jQuery的可以运行的页面。

I have set the form's onClick to return false so that the form does not reload the page when submitted so that the jQuery can run.

任何帮助是极大的AP preciated。

Any help is greatly appreciated.

推荐答案

主要的原因,为什么人们使用形式是这样,你可以定义

The main reason why people use forms is so that you can define an

动作(在你的情况下,PHP脚本),

action (in your case a php script),

方法(GET或POST)和

method (GET or POST) and a

提交按钮,捕捉表单中的所有信息,并自动将其发送到服务器。

submit button that captures all the information in the form and automatically sends it to the server.

这是不错的,当你有20-30输入或你正在处理多个文件输入。在你的情况,你正在处理的数据检索中的AJAX功能,你有没有提交按钮,操作或方法定义的,所以你可以让事情更容易通过不使用窗体在所有....

This is nice when you have 20-30 inputs or you are handling multiple file inputs. In your case you are handling the data retrieval in your ajax function, you have no submit button, action or method defined so you could make things a lot easier by not using a form at all....

    $.ajax({
        url: '/new-user.php',
        type: 'POST',
        dataType: "json",
        data: {
            name: $('#name').val(),
            email: $('#email').val(),
            password: $('#password').val()
        }
    }).done(function(data){
            alert(JSON.stringify(data));
    });

我已经离开了格式的div为简单起见...

I've left out the formatting divs for simplicity...

<input type="text" id="name" class="form-control" autofocus="autofocus">
<input type="text" id="email" class="form-control">
<input type="password" id="password" class="form-control">

以上code会抢了数据的输入,将其发送到PHP脚本,输出是从你的PHP脚本发回的警报的数据。

The above code will grab the data in your inputs, send it to the php script and output the data that is sent back from your php script in the alert.

最重要的是你的页面将不会刷新!

And most importantly your page will NOT refresh!

这篇关于调用使用AJAX PHP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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