我正在一個小型網站,我可以在我的表單域中輸入數字。該號碼保存在我的MySQL數據庫中,並且該值在id爲「show」的div中返回。我在這裏做了一個jQuery的數字立即顯示,所以這是工作正常。更新jquery
我已經制作了另一個div標籤,其中id是「showhot」。在這裏,我希望看到在大多數時間投入的1 - 100之間的數字。這也正常工作我的SQL字符串,但其中的JQuery不起作用。如果我更新我的頁面,則數字會上下移動,如果某個特定的數字已被多次使用。我試圖爲它做一些Jquery,但它是錯誤的。
有沒有人有線索我怎麼能做到這一點?
我希望我的問題是可以理解的,否則我會重新制定它。
問候 的Mads
HTML:
<div id="show">
<div class="numberheader">
<p>Tal</p>
</div>
<ul class="latestnumbers" style="list-style:none;padding-top: 60px;">
<?php include('response.php');?>
</ul>
</div>
<div class="content">
<p>Number</p>
<div class="form">
<fieldset>
<legend>Record Number</legend>
<form id="myForm" action="select.php" method="post">
<input type="number" name="numbervalue" id="numberinput"/>
<button id="sub">Save</button>
</form>
</fieldset>
</div>
<span id="result"></span>
</div>
<div id="#showhot">
<div class="hotnumbersheader">
<p>Hot</p>
</div>
<ul class="hotnumbers" style="list-style:none; padding-top: 60px;">
<?php include('hotnumbers.php');?>
</ul>
</div>
<div id="#showhot">
<div class="coldnumbersheader">
<p>Cold</p>
</div>
<ul class="coldnumbers" style="list-style:none; padding-top: 60px;">
<?php include('coldnumbers.php');?>
</ul>
</div>
JS:
// Insert function for number
function clearInput() {
$("#myForm :input").each(function() {
$(this).val('');
});
}
$(document).ready(function(){
$("#sub").click(function(e) {
e.preventDefault(); // remove default action(submitting the form)
$.post($("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(info){
$("#result").html(info);
});
clearInput();
});
});
// Recieve data from database
$(document).ready(function() {
setInterval(function() {
$('.latestnumbers').load('response.php')
}, 3000);
});
/* *** THIS IS WHAT I HAVE BEEN TRYING TO MAKE, THAT IS NOT WORKING ****/
$(function()
{
//Send a HTTP Request
$.ajax({
url: 'hotnumbers.php', //the script to call to get data
data: "dbconfig.inc.php", //Url argumnets to pass to dbconnection
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var numbers = data[0]; //get numbers
// 3) Update html content
$('#showhot').html("<b>Number</b>"+numbers); //Set output element html
}
});
});
// Recieve data from database
$(document).ready(function() {
setInterval(function() {
$('hotnumbers').load('hotnumbers.php')
}, 3000);
});
hotnumbers.php
<?php
include ('session.php');
include 'dbconfic.inc.php';
// '?' er placeholders for variabler
$stmt = $mysqli->prepare("SELECT numbers FROM numbertable GROUP BY numbers ORDER BY COUNT(*) DESC LIMIT 10;");
// execute prepared statement
$stmt->execute();
// Make variables ready
$number = null;
$n_id = null;
// bind result variabler
$stmt->bind_result($number);
// fetch values for each row
while ($stmt->fetch()) {
echo "<li>".$number."</li>";
}
// close statement
$stmt->close();
// close connection
$mysqli->close();
?>
嘿,現在正在工作。非常感謝 :) – KrMa