Javascript 单击按钮后从数据库中洗牌元素
我在下面所做的代码是,它只在我刷新页面时洗牌元素。例如:“我弹钢琴”。我希望它是“我弹钢琴”,只有当我点击“扰码”按钮时,它才会洗牌元素 thirdChineseSentence.phpJavascript 单击按钮后从数据库中洗牌元素,javascript,php,shuffle,scramble,Javascript,Php,Shuffle,Scramble,我在下面所做的代码是,它只在我刷新页面时洗牌元素。例如:“我弹钢琴”。我希望它是“我弹钢琴”,只有当我点击“扰码”按钮时,它才会洗牌元素 thirdChineseSentence.php <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Sentence Scramble and Sequencer</title> <link rel
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Sentence Scramble and Sequencer</title>
<link rel="stylesheet" type="text/css" href="Second.css" />
<script src="https://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript">
</script>
<script src="chinesesentence.js" type="text/javascript"></script>
</head>
<body>
<center>
<img src = "http://imageshack.com/a/img842/1461/otd4.jpg"/>
<h1>Hello world</h1>
<?php
// Connect to database server
mysql_connect("localhost", "root", "password") or die (mysql_error ());
// Select database
mysql_select_db("login") or die(mysql_error());
// Get data from the database depending on the value of the id in the URL
$strSQL = "SELECT * FROM sentences WHERE id
ORDER BY RAND() LIMIT 1;";
//create an array with numbers 1-4
$order = array(1,2,3,4);
//shuffle them in random order
shuffle($order);
$rs = mysql_query($strSQL);
// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {
// Write the data of the person
//Display all the array values from 0-3 (array index starts from 0)
echo "<dt>Sentence:</dt><dd>" . $row[$order[0]] . " " . $row[$order[1]] . " " .
$row[$order[2]] . " " . $row[$order[3]] ."</dd>";
}
// Close the database connection
mysql_close();
?>
<button id="showcontent">Scramble</button>
<div id="content"></div>
</center>
</body>
</html>
thirdChineseSentence.php可能已被浏览器缓存。设置一个正确的http头使其不被缓存,或者添加一个随机参数,例如
thirdchinessentence.php?dummy=42567499823
,使其每次都刷新。您正在向self发送ajax请求(thirdchinessentence.php)
因此,首先从thirdChineseSentence.php中剪切php代码并粘贴到新的php文件(server.php)中
现在将您的chinesesentence.js更改为
$(document).ready(function() {
var url = 'server.php';
$.ajax({
type: 'POST',
url: url,
dataType: 'html',
beforeSend: function() {
$( "#content" ).html( "Requesting.." );
},
success: function(html) {
document.getElementById("content").innerHTML=html;
}
});
$('#showcontent').click( function(e) {
// randomize your code locally here no need to send server request
});
}
您得到的错误是什么?没有错误消息。只是“加扰”按钮不起作用。只有在我每次刷新页面后,它才会乱序@Mahasish ShomeAs@konghou提到您的网页由浏览器缓存。尝试从浏览器设置中清除浏览器缓存并尝试重新加载网页,我希望问题能够得到解决。我已经尝试过清除,但无效。@你的代码似乎是正确的。在重新加载后,你是否得到了预期的结果?我不明白你的意思,我已经厌倦了从设置中清除我的浏览器缓存,但它无法工作。我可以知道我的js文件是否有问题导致按钮无法运行吗@Konghou如果问题是因为缓存,清除浏览器缓存只会使其在第一次单击时工作,而不会在第二次单击时工作。要么你不在php中添加缓存http头,要么添加一个随机参数,要么干脆用javascript来洗牌。我可以知道如何使用javascript吗@孔府
$(document).ready(function() {
var url = 'server.php';
$.ajax({
type: 'POST',
url: url,
dataType: 'html',
beforeSend: function() {
$( "#content" ).html( "Requesting.." );
},
success: function(html) {
document.getElementById("content").innerHTML=html;
}
});
$('#showcontent').click( function(e) {
// randomize your code locally here no need to send server request
});
}