简体   繁体   English

从jQuery中的链接提交带有参数的表单

[英]submitting form with parameter from a link in jquery

I am trying to save data in database in background through a link, and to give download functionality to that link in front end. 我正在尝试通过链接在后台将数据保存在数据库中,并在前端为该链接提供下载功能。 but it gives an error. 但它提供了一个错误。

my script is - 我的剧本是-

<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>

this is the link (It looks fine) 这是链接(看起来不错)

<a href="upload/<? echo $download;?>" id="download" data-params="{'ca_id':'<? echo $ca_id;?>'}"><button name="download"></button></a>

download_counter.php looks like - 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);  
}
?>

when i click the link, it lets download the file but database do not updates. 当我单击链接时,它允许下载文件,但数据库不更新。 Please help. 请帮忙。

Check jquery click event handler , which says 检查jQuery click事件处理程序 ,它说

// 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);
}

There is an error with passing parameter to function saveData, so your ajax request not occur: 向函数saveData传递参数时出错,因此不会发生ajax请求:

$(document).ready(function(){
    $("#download").click(function(){
        var me = $(this), data = me.data('params');
        saveData(data); // was me
});

database is updating now, but it is not getting value of candidate_id. 数据库现在正在更新,但是没有获取候选人编号的值。

i did this - 我做到了-

<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>
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.

Check this out 看一下这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM