如何获取以下脚本生成的值
$(document).ready(function() {
$("#nextBtn a").click(function() {
$('.slideCount').html(function(i, val) {
return val * 1 + 1
});
});
$("#prevBtn a").click(function() {
$('.slideCount').html(function(i, val) {
return val * 1 - 1
});
});
});
然后将该值传递给这个php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
// get the slide number to set
if (isset($_POST['txtSlideNbr']))
{
$gotoSlideNbr = $_POST['txtSlideNbr'];
// build our query
$query = "UPDATE `skdemo`.`demo` SET slide_nbr = " . $gotoSlideNbr . " WHERE demo_name = 'culture';";
$mysqli->query($query);
// clear the request
$gotoSlideNbr = NULL;
}
}
每当您想将此号码发送到服务器时,都会发出AJAX请求:
$.post('script.php', {txtSlideNbr: $('.slideCount').text()}, function(data) {
proccess server responce if any
});
在您的PHP脚本中使用:
$gotoSlideNbr = (int)$_POST['txtSlideNbr'];
这将阻止SQL注入。
您可以使用jquery中的$.post()
函数,它很容易设置,可能类似于
var nrOfClicks = 0;
$("#prevBtn a").click(function() {
nrOfClicks++; //no need to save it in a div, can just use a variable
$.post('your_url',{'clicks':nrOfClicks});
});
通过ajax调用传递它-http://api.jquery.com/jQuery.ajax/(或者如果你喜欢,可以使用get)-你可以将你想要发送的数据发送到你定义的的URL