发布到PHP,不重新加载页面和更新DIV [英] Post to PHP, do not reload page and update div

查看:103
本文介绍了发布到PHP,不重新加载页面和更新DIV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:这工作现在,code更新。非常感谢马丁安培!

所以,我一直在玩这一个星期现在已经破解了众多的例子,但它没有工作,所以我不会打扰张贴code,因为它是一个烂摊子!我是个万事通,掌握没有,所以我不知道我要去哪里错了。

So I've been playing with this for a week now and have hacked together many examples but it hasn't worked, so I won't bother posting code since it's a mess! I'm a jack of all trades, master of none, so I'm not sure where I'm going wrong.

这是我所需要的。

posttest.php

<html>
<head>
<script type="text/javascript" src="./js/jquery-1.7.2.min.js"></script>
</head>
<body>

<form name="processForm" id="processForm" action="process.php" method="POST">
  <input type="text" id="textField" name="textField" />
  <input type="hidden" id="sessionID" name="sessionID" value="myusername" />
<input type="submit" value="Process!" />
</form>

<div id="mydiv"></div>

FILLER<BR><BR>FILLER<BR><BR>FILLER<BR><BR>FILLER<BR><BR>FILLER<BR><BR>FILLER<BR><BR>FILLER<BR><BR>FILLER<BR><BR>FILLER<BR><BR>FILLER<BR><BR>
FILLER<BR><BR>FILLER<BR><BR>FILLER<BR><BR>FILLER<BR><BR>FILLER<BR><BR>FILLER<BR><BR>FILLER<BR><BR>FILLER<BR><BR>FILLER<BR><BR>FILLER<BR><BR>

<script type="text/javascript">

$('#processForm').submit(function() {
  var text = $('#textField').val();
  var sid  = $('#sessionID').val();

$.ajax({
  url: "process.php",
  type: "POST",
  data: { 'text' : text, 'sessionID' : sid },
  success: function(data) {
    var result = $.parseJSON(data);
    if (result.success) {           
        // Handle your result and update your form accordingly  
        // result.data.someKey  
        $('#mydiv').text('Success! With ' + result.data);

    }
    else {
        // Error handling as applicable
    }
  }
});

    return false;
});

</script>

</body>
</html>

process.php

<?php
$someText=$_POST['text'];

$return = array('success' => false, 
            'data' => $someText
           );

if (isset($_POST['text'], $_POST['sessionID'])) {
    // Do your queries

    $return['success'] = true;
    //$return['data'] = array('someKey' => 'someValue');
    $return['data'] = $someText;
}

echo json_encode($return);
?>


我已经试过这样的例子很多,并试图使自己的从无到有,似乎无法得到它的工作!什么,我试图完成将在您添加Facebook上的评论,或一个新的职位就像一个例子。


I've tried so many examples, and tried making my own from scratch and can't seem to get it to work! An example of what I'm trying to accomplish would be like when you add a comment on facebook, or a new post.

我希望有人能帮助,因为我觉得我已经浪费了一个星期在这里这个简单的任务。

I hope someone can help out because I feel like I've already wasted a week here on this simple task.

在此先感谢!

推荐答案

您不必会话变量保存在您的网页,因为它是存储在cookie中了。假设你正在使用JQuery,这里有一个快速的示例:

You don't need to store the session variable in your page, as it's stored in a cookie already. Assuming you're using JQuery, here's a quick sample:

的标记

<form id="processForm" action="process.php" method="POST">
   <input type="text" id="textField" name="textField" />
   <input type="hidden" id="sessionID" name="sessionID" value="<?=session_id()?>" />
   <input type="submit" value="Process!" />
</form>

中的JavaScript

$('#processForm').submit(function() {
  var text = $('#textField').val();
  var sid  = $('#sessionID').val();

  $.ajax({
    url: "process.php",
    type: "POST",
    data: { 'text' : text, 'sessionID' : sid },
    success: function(data) {
        var result = $.parseJSON(data);
        if (result.success) {           
            // Handle your result and update your form accordingly  
            // result.data.someKey  
        }
        else {
            // Error handling as applicable
        }
    }
   });

   return false;
});

process.php

<?php

$return = array('success' => false, 
                'data' => array()
               );

if (isset($_POST['text'], $_POST['sessionID'])) {
    // Do your queries

    $return['success'] = true;
    $return['data'] = array('someKey' => 'someValue');
}

echo json_encode($return);
?>

注意

您应该使用绝对的URL,并意识到的形式/ JavaScript是不突兀,所以你应该让功能没有Ajax了。

You should use absolute url's and be aware the form/javascript is non obtrusive so you should make it function without ajax too.

这篇关于发布到PHP,不重新加载页面和更新DIV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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