PHP + jQuery的阿贾克斯+表单提交 - 在同一页面上返回结果 [英] PHP + jQuery + Ajax form submission - return results on the same page

查看:120
本文介绍了PHP + jQuery的阿贾克斯+表单提交 - 在同一页面上返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有以下几点:

用户通过点击提交表单(的index.php )的形式输入由一个外部的PHP文件(的search.php处理)和结果在一个div贴出原始页面(上的index.php )。

User submits a form by a click (index.php), the form input is processed by an external PHP file (search.php) and results are posted on the original page (index.php) in a div.

我已经聚在一起大部分code。它提出在点击一个表单并将其发送到一个PHP脚本。

I have already got together most of the code. It submits a form on a click and sends it to a PHP script.

我现在需要的是,从PHP脚本,结果被归还给原来的页面(的index.php )。

What I would need now is that the result from the PHP script are given back to the original page (index.php).

下面是我的code到目前为止:

Here is my code so far:

function submit() {
    $(function() {
        var id = "id";
        var dataString = 'id=' + id;
        $.ajax({
            type: "POST",
            url: "inc/search.php",
            data: dataString,
            success: function() {
                goToByScroll("result");
                $('#result').html("<br><br><br><br><br><br><br><br><br><br><
                                   div class='center'><img src='img/loader.gif' /></div>").hide().fadeIn(2500, function() {
                $('#result').html("Finished");
                });
            }
        });
        return false;
    });
}

我的PHP文件(用于测试)是:

My PHP file (for testing) is:

<?php function safe($value)
{
    htmlentities($value, ENT_QUOTES, 'utf-8');
    return $value;
}
if (isset($_POST['id'])) {
    $id = safe($_POST['id']);
    echo ($id);
}
elseif (!isset($_POST['id'])) {
    header('Location: error?id=notfound');
} ?>

我想有从的search.php 的结果(在这种情况下,ID)贴到#result,但我无法弄清楚如何:•

I would like to have results from search.php (in this case "id") posted into #result, but I can't figure out how :S

推荐答案

我想你已经得到了几乎所有的东西。回调函数必须在成功需要它代表的结果,从参数的search.php

I think you've got almost everything. The callback function you have under success needs an argument which stands for the results from search.php

     success: function(res) {
             goToByScroll("result");
             $('#result').html("<br><br><br><br><br><br><br><br><br><br><div class='center'><img src='img/loader.gif' /></div>").hide().fadeIn(2500, function() {
                 $('#result').html(res + "<br /><br /> Finished");
             });
         } 

RES 输出为的search.php 的一切。回声,PHP标签之外的东西,等等任何你会看到,如果你装的search.php 本身。

res is everything outputted be search.php. Echo, stuff outside of php tags, etc Anything you'd see if you loaded search.php itself.

我不知道,如果你想完成仍然存在。把它拿出来,如果​​你没有。

I don't know if you wanted 'Finished' to still be there. Take it out if you dont.

这篇关于PHP + jQuery的阿贾克斯+表单提交 - 在同一页面上返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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