加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何不断更新页面的一部分

发布时间:2020-12-13 13:20:23 所属栏目:PHP教程 来源:网络整理
导读:http://pastebin.com/dttyN3L6 处理表单的文件名为upload.php 我从来没有真正使用过jquery / js所以我不确定如何做到这一点或者我会把代码放在哪里. 它与setInterval(loadLog,2500)有关; 另外,如何让用户可以在没有页面刷新的情况下提交表单? $.ajax({ type
http://pastebin.com/dttyN3L6

处理表单的文件名为upload.php

我从来没有真正使用过jquery / js所以我不确定如何做到这一点或者我会把代码放在哪里.

它与setInterval(loadLog,2500)有关;

另外,如何让用户可以在没有页面刷新的情况下提交表单?

$.ajax({  
  type: "POST",url: "upload.php",data: dataString,success: function() {  

  }  
});  
return false;  `

<?php 
 $conn1 = mysqli_connect('xxx') or die('Error connecting to MySQL server.');
 $sql = "SELECT * from text ORDER BY id DESC LIMIT 1";
 $result = mysqli_query($conn1,$sql) or die('Error querying database.');
 while ($row = mysqli_fetch_array($result)) {
      echo  '<p>' . $row['words'] . '</p>';
 }
 mysqli_close($conn1);

 ?>

 </div>

 <?php     
 if (!isset($_SESSION["user_id"])) {

 } else {
      require_once('form.php'); 
 }

 ?>
您可以在不刷新页面的情况下提交表单,如下所示:

form.php的:

<form action='profile.php' method='post' class='ajaxform'>
 <input type='text' name='txt' value='Test Text'>
 <input type='submit' value='submit'>
</form>

<div id='result'>Result comes here..</div>

profile.php:

<?php
      // All form data is in $_POST

      // Now perform actions on form data here and 
      // create an result array something like this
      $arr = array( 'result' => 'This is my result' );
      echo json_encode( $arr );
?>

jQuery的:

jQuery(document).ready(function(){

    jQuery('.ajaxform').submit( function() {

        $.ajax({
            url     : $(this).attr('action'),type    : $(this).attr('method'),dataType: 'json',data    : $(this).serialize(),success : function( data ) {
                        // loop to set the result(value)
                        // in required div(key)
                        for(var id in data) {
                            jQuery('#' + id).html( data[id] );
                        }
                      }
        });

        return false;
    });

});

如果你想在特定时间之后调用ajax请求而不刷新页面,你可以尝试这样的事情:

var timer,delay = 300000;

timer = setInterval(function(){
    $.ajax({
      type    : 'POST',url     : 'profile.php',data    : $('.ajaxform').serialize(),success : function(data){
                  for(var id in data) {
                    jQuery('#' + id).html( data[id] );
                  }
                }
    });
},delay);

你可以随时停止计时器,如下所示:

clearInterval( timer );

希望这能为您提供完成任务的方向.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读