简体   繁体   English

PHP使用jQuery / AJAX重定向和加载内容

[英]PHP redirect and load content with jQuery / AJAX

I have to create a simple website with various pages (5-6). 我必须创建一个包含各种页面(5-6)的简单网站。 Now I have one "master" page called index.php like this: 现在,我有一个名为index.php的“主”页面,如下所示:

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

This page has also other div's on it, like a header where I show some images etc. and is styled with a css. 此页面上还有其他div,例如标头,其中显示了一些图片等,并以CSS样式设置。

Now when the user clicks on a link in the menu bar, whatever page I want to show is loaded via jQuery/AJAX into the content div. 现在,当用户单击菜单栏中的链接时,我要显示的任何页面都将通过jQuery / AJAX加载到内容div中。 I do this here: 我在这里这样做:

$(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..
});

This works perfectly fine and I am happy with the result, now on to the problem. 这工作得很好,我对结果感到满意,现在解决问题。 On some of these pages (like register.php) I have forms that I submit. 在其中某些页面上(例如register.php),我有提交的表格。 Now once I submit the form and do various things like database operations etc. I want to redirect to one of the other pages and maybe show a short information message. 现在,一旦提交表单并执行各种操作(如数据库操作等),我想重定向到其他页面之一,并可能显示一条简短的信息消息。 What I do to redirect in the php file is the following: 我在php文件中重定向的操作如下:

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

I got this somewhere from a code snippet, so I am not sure if this is the proper way to do this. 我是从代码段获得的,所以我不确定这是否是正确的方法。 Because I have per default set the index.php to load my home.php content, it always redirects to my home.php content and I was happy with this. 因为我默认情况下将index.php设置为加载home.php内容,所以它总是重定向到home.php内容,对此我感到很满意。 But what if I want to redirect to another page, lets say go.php? 但是,如果我想重定向到另一个页面,比如go.php,该怎么办? How would I do this? 我该怎么做? Is this even possible from php or do I need to do this with javascript/jQuery? 这甚至可能来自php还是我需要使用javascript / jQuery来做到这一点?

I am a bit lost here, have tried searching for it but didn't come across exactly this issue here. 我在这里有点迷茫,曾经尝试寻找它,但是在这里并没有碰到这个问题。

For submit form and database connection : 对于提交表单和数据库连接:

you can use onclick function to handle actions for submit button : 您可以使用onclick函数来处理提交按钮的操作:

example in .JS file .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 file: 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 */
}

To redirect page dynamically : 要动态重定向页面:

you can use .load() function for example : 您可以使用.load()函数,例如:

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

use this http://api.jquery.com/load/ reference to use it. 使用此http://api.jquery.com/load/参考进行使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM