[英]Post variables with ajax won't work
I have some data, processed with javascript that I want to write to a database. 我有一些要用JavaScript处理的数据要写入数据库。 So I'm trying to code something with ajax. 所以我正在尝试用ajax编写代码。 But with no luck... 但是没有运气
What I need: I have two variables (id, naam) that I want to pass to a php-script. 我需要什么:我有两个变量(id,naam)要传递给php脚本。 Nothing more... 而已...
<script type='text/javascript'>
function geefNaam(id, naam)
{
$.ajax({
type: "POST",
url: "schrijfrecord.php",
data: { id: id, naam: naam },
})
}
</script>
and I call this function... 我称之为这个功能
geefNaam(id, naam);
my php 我的PHP
<?php
$id = $_POST['id'];
$naam = $_POST['naam'];
echo $id;
echo $naam;
?>
First remove ,
after data: { id: id, naam: naam },
. 先删除,
后data: { id: id, naam: naam },
Also you aren't doing anything after your $.ajax
call. 同样,在$.ajax
调用之后,您什么也不做。 Use success: function(){}
or .done()
. 使用success: function(){}
或.done()
。 I would use $.post()
: 我会用$.post()
:
//<![CDATA[
<script type='text/javascript'>
function geefNaam(id, naam, where){
$.post('schrijfrecord.php', {id: id, naam: naam},
function(result){
where.html(result);
}
});
}
$('#someElement').click(function(){
geefNaam('someId', 'someName', $('#wherever'));
});
//]]>
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.