Javascript ajaxpost的使用
编辑: 这是我设法建立的…但它仍然不起作用Javascript ajaxpost的使用,javascript,php,ajax,Javascript,Php,Ajax,编辑: 这是我设法建立的…但它仍然不起作用 $(document).ready(function(){ $("tr").click(function(){ txt=$("input[name=path]").val(); $.ajax({ type: "POST", url: contract.php, // the same page where i have the table data: txt, // the variable
$(document).ready(function(){
$("tr").click(function(){
txt=$("input[name=path]").val();
$.ajax({
type: "POST",
url: contract.php, // the same page where i have the table
data: txt, // the variable containing the dynamic id from the clicked row
success: success, // i have no idea what is this parameter for....
dataType: dataType // i have no idea what is this parameter for....
});
});
});
PHP:
$row=mysql\u fetch\u数组($query){
回声';
回声';
回声';
}
我想要的是,当用户单击一行时,必须获取行id(是动态的),并使用ajax post返回,这样我就可以在另一个查询中使用它。我必须这样做,而不必重新加载页面,这就是为什么我尝试使用ajax。
如果可以使用jQuery,我会使用类似这样的东西来构建我的表(在mysql_*旁边):
然后使用jQuery侦听器捕捉单击事件:
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
$(document).on('click','tr.listContractRow', function(e){
var path = $(this).data('path');
//Use the variable path here in your AJAX call
//Assuming you want a GET request, you can also use $.ajax or $.post here
$.get('YOUR_AJAX_URL_HERE?path='+path, function(){
//Do something after the ajax call
});
});
</script>
$(文档)。在('click','tr.listContractRow',函数(e){
var path=$(this.data('path');
//在AJAX调用中使用变量路径
//假设需要GET请求,也可以在此处使用$.ajax或$.post
$.get('YOUR\u AJAX\u URL\u HERE?path='+path,function(){
//在ajax调用之后做一些事情
});
});
编辑
在PHP中,您可以执行以下操作:
<?php
if(isset($_GET['path']))
{
//Query here with the variable $_GET['path'];
//Echo the results you want
//Perform an exit here, since it's an AJAX call. You probably don't want to echo the code below (if there is)
exit();
}
?>
如果您没有使用jQueryWhy,哦,为什么人们仍然使用mysql*
函数而不是mysqli*
或PDO?请注意,它们不再被维护,而是被删除了。请改为了解,并使用或。@Petru参考前3条评论中的链接-请至少尝试其中一条,如果您仍然有问题,请编辑您的问题,以包括您尝试过的内容以及为什么不起作用尽管显示的jQuery是正确的,“Second try to find your question at stackoverflow.”这句话是多余的,而且是居高临下的。但是我怎么能在我的javascript代码中插入这句话呢???@snotwaffle除了上面的陈述之外,即使javascript是不正确的。如图所示,这将导致OP出现语法错误…@War10ck,这就是我讨厌Javascript的原因:它的错误并不明显。仅供参考,data
参数可以包含一个对象。它无论如何都不必是字符串。如果您试图构建JSON字符串,那么我建议使用JSON.stringify()
来创建有效的JSON语法并防止可能的语法错误代码>这似乎更清楚/更简洁,但这是偏好的问题。这取决于具体情况。如果这样做,当表被AJAX调用等重新加载时,您的单击侦听器将无法工作$(document).on
将起作用,即使在AJAX调用之后也是如此。@snotwaffle的区别在于,在元素出现在页面上之后,您必须绑定它。答案中的语法的好处是,元素可以在click事件绑定后的某个时间出现,因此它是一种方便的语法know@S.Pols您的答案与我需要的非常接近,但我不明白在ajax调用之后,如何在另一个查询的同一页面中使用变量(包含id),或者其他一些操作…。@S.Pols不起作用…它只是不改变url…,我需要在该函数中写些什么吗??
<?php
$row=mysql_fetch_array($query){
echo '<tr class="listContractRow" data-path="'.$row['id'].'">';
echo '</tr>';
}
?>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
$(document).on('click','tr.listContractRow', function(e){
var path = $(this).data('path');
//Use the variable path here in your AJAX call
//Assuming you want a GET request, you can also use $.ajax or $.post here
$.get('YOUR_AJAX_URL_HERE?path='+path, function(){
//Do something after the ajax call
});
});
</script>
<?php
if(isset($_GET['path']))
{
//Query here with the variable $_GET['path'];
//Echo the results you want
//Perform an exit here, since it's an AJAX call. You probably don't want to echo the code below (if there is)
exit();
}
?>