PHP重定向和负载的内容与jQuery / AJAX [英] PHP redirect and load content with jQuery / AJAX

查看:90
本文介绍了PHP重定向和负载的内容与jQuery / AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的编程/设计网站让裸陪我:)

I am relatively new programming/designing websites so bare with me :)

对于一个项目,我要创建一个简单的网站使用不同的页面(5-6)。现在,我已呼吁的index.php像这样的大师页面:

For a project I have to create a simple website with various pages (5-6). Now I have one "master" page called index.php like this:

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

该网页还具有其他分区的就可以了,像一个头,我展示一些图片等,在此风格与CSS。

This page has also other div's on it, like a header where I show some images etc. and is styled with a css.

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

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

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

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. 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:

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

我得到这个地方从code段,所以我不知道这是正确的方式来做到这一点。因为我每默认设置的index.php加载我home.php的内容,它总是重定向到我的home.php的内容,我很高兴这一点。但是,如果我想重定向到另一页是什么,可以说go.php?我将如何做到这一点?这是从PHP甚至有可能还是我需要用JavaScript / jQuery的这样做呢? 我在这里有点失落,试图寻找,但没遇到过究竟这个问题在这里。请问AP preciate任何帮助!

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. But what if I want to redirect to another page, lets say go.php? How would I do this? Is this even possible from php or do I need to do this with javascript/jQuery? I am a bit lost here, have tried searching for it but didn't come across exactly this issue here. Would appreciate any help!

推荐答案

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

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

you can use onclick function to handle actions for submit button :

例如,在.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文件:

ajaxHandle.php file:

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()函数,例如:

you can use .load() function for example :

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

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

这篇关于PHP重定向和负载的内容与jQuery / AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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