我正在尝试通过链接在后台保存数据库,并在前端为该链接提供下载功能。但它会出错。
我的剧本是 -
<script>
$(document).ready(function(){
$("#download").click(function(){
var me = $(this), data = me.data('params');
saveData(me);
});
function saveData(me){
$.ajax({
type: "POST",
url: "download_counter.php",
data: { client_id: "<? echo $client_id;?>", candidate_id: me }
});
}
});
</script>
这是链接(看起来很好)
<a href="upload/<? echo $download;?>" id="download" data-params="{'ca_id':'<? echo $ca_id;?>'}"><button name="download"></button></a>
download_counter.php看起来像 -
<?
if (isset($_POST['candidate_id'])) { // Form has been submitted.
$candidate_id = $_POST['candidate_id'];
$client_id= $_POST['client_id'];
$date = date("Y-m-d");
echo "client - ".$client_id;
echo "candidate - ".$candidate_id;
$query = "INSERT INTO `downloads`(`client_id`, `candidate_id`, `download_date`) VALUES ('".$client_id."', '".$candidate_id."', '".$date."')";
$result = mysql_query($query);
}
?>
当我点击链接时,它允许下载文件,但数据库不会更新。 请帮忙。
答案 0 :(得分:0)
检查jquery click event handler,其中包含
// say your selector and click handler is somewhat as in the example
$("some selector").click({param1: "Hello", param2: "World"}, some_function);
// then in the called function, grab the event object and use the parameters like this--
function some_function(event){
alert(event.data.param1);
alert(event.data.param2);
}
答案 1 :(得分:0)
将参数传递给函数saveData时出错,因此不会发生ajax请求:
$(document).ready(function(){
$("#download").click(function(){
var me = $(this), data = me.data('params');
saveData(data); // was me
});
答案 2 :(得分:0)
数据库现在正在更新,但它没有获得candidate_id的值。
我这样做了 -
<script>
$(document).ready(function(){
$("#download").click(function(){
$("#count").hide();
var me = $(this), data = me.data('params');
saveData(data); // was me
});
function saveData(data){
$.ajax({
type: "POST",
url: "counter.php",
data: { client_id: "2", candidate_id: data.ca_id }
});
}
});
</script>
答案 3 :(得分:0)
I think on click the data you are reading is a string and in the given format.
And you are passing that as data, but since it is not an valid id,
that value in the database is not updated.