我有一个简单的PHP脚本,它从文本文件中读取特定的短语,并在网页上随机显示一个。为了显示另一个随机短语,目前必须刷新整个页面。我的问题是--有没有办法只更新包含短语的页面区域,在每次点击按钮时显示一个新的短语?目前,下面的PHP包含在一个h1标签中,该标签已经使用CSS设置了样式。然后,我在它下面添加了一个注释,告诉用户刷新页面以显示另一个随机短语。理想情况下,我希望这个评论被改为一个按钮,这样用户就不必每次刷新整个页面。
以下是用于显示随机短语的简单PHP:
$str = file_get_contents('words.txt'); //Take the contents from the file to the variable
$result = explode(',',$str); //Split it by ','
echo $result[array_rand($result)]; //Return a random entry from the array.任何帮助都将不胜感激。
干杯,汤姆
发布于 2012-06-08 23:27:07
例如,我曾经开始学习PHP/AJX,摘自Using jQuery…
创建一个php脚本来接收http请求并从数据库中获取数据
//--------------------------------------------------------------------------
// Example php script for fetching data from mysql database
//--------------------------------------------------------------------------
$host = "localhost";
$user = "root";
$pass = "root";
$databaseName = "ajax01";
$tableName = "variables";
//--------------------------------------------------------------------------
// 1) Connect to mysql database
//--------------------------------------------------------------------------
include 'DB.php';
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);
//--------------------------------------------------------------------------
// 2) Query database for data
//--------------------------------------------------------------------------
$result = mysql_query("SELECT * FROM $tableName"); //query
$array = mysql_fetch_row($result); //fetch result
//--------------------------------------------------------------------------
// 3) echo result as json
//--------------------------------------------------------------------------
echo json_encode($array);客户端示例
输出:
this element will be accessed by jquery and this text replaced
<script id="source" language="javascript" type="text/javascript">
$(function ()
{
//-----------------------------------------------------------------------
// 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
//-----------------------------------------------------------------------
$.ajax({
url: 'api.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php
//for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var id = data[0]; //get id
var vname = data[1]; //get name
//--------------------------------------------------------------------
// 3) Update html content
//--------------------------------------------------------------------
$('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname); //Set output element html
//recommend reading up on jquery selectors they are awesome
// http://api.jquery.com/category/selectors/
}
});
});
</script>
</body>
发布于 2012-06-08 23:20:18
我可以想到两种方式:
发布于 2012-06-08 23:26:20
使用jQuery,这非常简单:
html:
<div id="random"></div>
<input type="button" id="update" />javascript:
<script>
$("#update").on("click", function() {
$.ajax({
url: "/randomwords.php",
success: function(data) { $("#random").html(data); }
});
});
</script>randomwords.php是上面的代码片段,不包含任何其他内容。
https://stackoverflow.com/questions/10951591
复制相似问题