Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从jquery中的链接提交带有参数的表单_Javascript_Php_Jquery_Mysql - Fatal编程技术网

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

Javascript 从jquery中的链接提交带有参数的表单,javascript,php,jquery,mysql,Javascript,Php,Jquery,Mysql,我试图通过一个链接将数据保存在后台的数据库中,并为前端的链接提供下载功能。但它给出了一个错误 我的剧本是- <script> $(document).ready(function(){ $("#download").click(function(){ var me = $(this), data = me.data('params'); saveData(me); }); function saveData(me){ $.ajax({

我试图通过一个链接将数据保存在后台的数据库中,并为前端的链接提供下载功能。但它给出了一个错误

我的剧本是-

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

$(文档).ready(函数(){
$(“#下载”)。单击(函数(){
var me=$(this),data=me.data('params');
保存数据(me);
});
函数saveData(me){
$.ajax({
类型:“POST”,
url:“下载_counter.php”,
数据:{client_id:,候选者_id:me}
});
}
});
这是链接(看起来不错)


下载_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);  
}
?>
检查jquery,它说

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

将参数传递给函数saveData时出错,因此不会发生ajax请求:

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

数据库正在更新,但它没有获取候选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>

$(文档).ready(函数(){
$(“#下载”)。单击(函数(){
$(“#计数”).hide();
var me=$(this),data=me.data('params');
saveData(data);//是我
});
函数saveData(data){
$.ajax({
类型:“POST”,
url:“counter.php”,
数据:{client_id:“2”,候选者_id:data.ca_id}
});
}
});

这很可能是因为浏览器取消了AJAX请求,因为用户被发送到新页面(下载)。一种方法是在onclick函数中执行
e.preventDefault()
(添加e参数),然后在完成ajax时重定向用户。或者在php中创建一个重定向页面,该页面计数,然后重定向到下载。这也适用于没有JS的用户。查询正在运行,但数据库尚未更新:(转义变量值可能会有问题,请检查此链接--‘是的。数据库正在根据您的建议进行更新。但它没有获取候选id的值。。我正在执行此操作—$(“#下载”)。单击(函数(){$(“#计数”).hide();var me=$(此),data=me.data('params');saveData(数据);//was me type:“POST”,url:“counter.php”,数据:{client_id:“2”,candidate_id:data.ca_id}是的,因为php在变量$_POST['candidate_id']中有一个数组。在您的情况下,不需要在数据参数中有与数组的链接:data params=“{ca_id':''}”,而只需要data caid=“”在js$(..).data('caid');这个param1和param2是什么。我想从我的链接中获取值。如何操作请控制台。在saveData函数中记录
数据
,并将其粘贴到这里。那是什么?以及如何操作…?我说的是
控制台.log()
函数,它将把传递的参数记录到控制台。所以我想让你做的是在$.ajax({-})之前的saveData函数中;编写
console.log(data);
然后查看控制台中记录的内容并粘贴输出。我做了。但是这个控制台在哪里呢?在chrome中打开你的链接并按键
F12
<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.