PHP重定向和负载的内容与jQuery / AJAX

问题描述 投票:1回答:1

我要创建一个简单的网站与各种页面(5-6)。现在我有一个“主”页名为index.php的是这样的:

<html>
    <head></head>
    <body>
        <div id="content"></div>
    </body>
</html>

本页面也已经上其他分区的,像在那里我将展示一些图片等,在此风格与CSS头。

现在,当用户点击菜单栏中的链接,任何网页我想说明通过jQuery / AJAX加载到内容DIV上。我做到这一点的位置:

$(document).ready(function () {
// Per default load home.php
var url= "home.php";
    $.ajax({
      url: url,
      success: function(data){
           $('#content').html(data);
      }
    });         
$('#register').click(function() {
    var url= "register.php";
    $.ajax({
      url: url,
      success: function(data){
           $('#content').html(data);
      }
    });
});
// more pages here..
});

这工作完全正常,我很高兴的结果,现在的问题。在一些网页(如同register.php)的我有我提交表单。现在,一旦我提交表单,并做各种事情,如数据库操作等。我想重定向到某个页面,也许表明短信息消息。我做的PHP文件重定向如下:

header('Location: ../app/index.php');

我从一个代码片断这个地方,所以我不知道这是否是这样做的正确方法。因为我每默认设置的index.php加载我home.php内容有,它总是重定向到我home.php内容,我很高兴与此有关。但是,如果我想重定向到另一页是什么,让我们说go.php?我会怎么做呢?这是甚至从PHP可能还是需要使用JavaScript / jQuery来做到这一点?

我有点失去了这里,试图寻找,但正是这里没遇到过这个问题。

php jquery ajax
1个回答
1
投票

对于提交表单和数据库连接:

您可以使用的onclick函数来处理提交按钮操作:

例如,在.js文件

$(document).ready(function(){ 
    $('#submit').click(function(){
        var input = $('#textfiel').val();
        $.post("ajaxHandle.php",
            {data:input }
          ).success(function(responce){
              $('#responceMessage').prepend(responce);
              /* This is the field on your index.php page where you want to display msg */
          });
    });
});

ajaxHandle.php文件:

if($_SERVER['REQUEST_METHOD'] == 'POST'){
 $inputData = $_POST['data'];

/* your database code will be done here you can find user input data in $inputData */

    echo "registration successful"; /* this string will be get in var responce in .JS file */
}

要动态地重定向页面:

你可以使用.load()函数,例如:

$('#content').load('../app/register.php');

使用此http://api.jquery.com/load/参考使用它。

© www.soinside.com 2019 - 2024. All rights reserved.