我正在寻找一个PHP函数(或者" 无论是什么工作"函数),每次{{1} 0
时,值DIV
都会上升单击并同时将新值保存在PHP file
上。
帮帮我这些:
1.点击" #myDiv"使价值上升1
2.当"#myDiv"点击保存PHP文件的新值(所以我可以在其他地方执行)
3.使 new 保存的值出现在另一个'DIV" #otherDiv"当" #myDiv"点击。
我之所以这样做是因为我一直在寻找一个可以节省价值的计数器,但它并不适用于javascript或jquery所以我希望它可以通过PHP发生
我愿意尝试其他更好的方法(可能使用AJAX吗?)
任何帮助表示赞赏。如果您不了解我要找的内容,请向我提问,谢谢!
答案 0 :(得分:1)
您需要执行以下操作:
myNestedDictionary["cubes"]
db.php中
<?php
include_once('db.php');
$result = mysqli_query($con,"select divcount from user");
$row = mysqli_fetch_assoc($result);
?>
<html>
<title>Count DIV Click</title>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript" >
$(function() {
$("#myDiv").click(function() {
$.ajax({
type: "POST",
url: "data_update.php",
data: "curcnt="+ $('#display').text(),
cache: false,
success: function(html){
$("#display").text(html);
}
});
});
});
</script>
</head>
<body>
<div id="myDiv" style="cursor:pointer">My div</div>
<div id="display"><?php echo $row['divcount']; ?></div>
</body>
</html>
data_update.php
<?php
$con = mysqli_connect("localhost","root","","users");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
SQL:
<?php
include_once('db.php');
if(isSet($_POST['curcnt']))
{
$curcnt = $_POST['curcnt'] + 1;
if($_POST['curcnt']=='0' || $_POST['curcnt']=='')
{
mysqli_query($con,"insert into user(divcount) values('".$curcnt."')");
}
else
{
mysqli_query($con,"update user set divcount='".$curcnt."'");
}
echo $curcnt;
}
else
{
echo '0';
}
exit;
?>
答案 1 :(得分:1)
保持简单的基本思路:page1.php,点击按钮。数据库已更新。 number可在page2.php上查看
演示:http://themooliecommunity.com/TEST/page1.php
只有下面列出的页面在此文件夹中。
dbconnect.php
<?php
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
?>
page1.php中
<?php
include 'dbconnect.php';
?>
<form method="post" action="increment.php">
<input type="submit" Value="increment" name="increment" />
</form>
increment.php
<?php
include 'dbconnect.php';
$query = "UPDATE `table` SET value = value+1";
mysqli_query($mysqli, $query);
mysqli_close($mysqli);
header("Location: ./page2.php");
?>
访问getdata.php
<?php
include 'dbconnect.php';
$query = "SELECT value FROM `table`";
$data = mysqli_query($mysqli, $query);
$row = mysqli_fetch_assoc($data);
$number = $row['value'];
mysqli_close($mysqli);
?>
使page2.php
<?php
include 'getdata.php';
echo $number;
?>
SQL:
CREATE TABLE IF NOT EXISTS `table` (
`id` int(11) NOT NULL,
`value` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `table`
--
INSERT INTO `table` (`id`, `value`) VALUES
(0, 0);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `table`
--
ALTER TABLE `table`
ADD PRIMARY KEY (`id`);