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

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 '

' . $row['words'] . '

'; } mysqli_close($conn1); ?>

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

form.php的:

 
Result comes here..

profile.php:

  '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', dataType: 'json', data : $('.ajaxform').serialize(), success : function(data){ for(var id in data) { jQuery('#' + id).html( data[id] ); } } }); }, delay); 

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

 clearInterval( timer ); 

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

这很简单。 要使用Jquery访问元素,可以使用css选择器,例如,获取名为“foo”的输入字段的值,执行以下操作:

 var fooVal = $("input[name=foo]").val(); 

要将其发送到服务器,您需要将事件侦听器(例如,单击)附加到提交按钮/任何其他元素

 var data = { varName : fooVal }; var url = "http://example.com"; var responseDataType = "json"; function parseResponse(JSON) { // your code handling server response here, it's called asynchronously, so you might want to add some indicator for the user, that your request is being processed } $("input[type=submit]").on('click', function(e){ e.preventDefault(); $(this).val("query processing"); $.post(url,data, parseResponse, responseDataType); return false; }); 

如果您想要不断更新,当然可以添加定时器或其他逻辑。 但我希望你能了解如何处理这类案件;

要回答部分问题,您可以使用ajax。