【发布时间】:2014-02-27 17:16:05
【问题描述】:
我想在成功事件上使用 lastInsertId。请求工作正常,我可以插入一个新元素,但没有任何返回。
函数.js
function add_elem(i)
{
var mydata ="e1="+0+"&e2="+0+"&e3="+100+"&e4="+100+"&e5="+i+"&addelem=1";
$.ajax({
url:"edit.php",
type:'POST',
data: mydata,
success: function(f) {
alert(f);
},
error: function() {
alert('error try again');
}
});
edit.php
<?php include"inc/config.php";
$positionx=$_POST['e1'];
$positiony=$_POST['e2'];
$lar=$_POST['e3'];
$haut=$_POST['e4'];
$typ=$_POST['e5'];
if(isset($_POST['addelem']))
{
$req_add=$bdd->prepare('insert into elem (element_position_x,element_position_y,element_width,element_height,element_type)
values(:posx,:posy,:width,:height,:type)');
$req_add->execute(array(
'posx' =>$positionx,
'posy' =>$positiony,
'width' =>$lar,
'height' =>$haut,
'type' =>$typ
)
);
return $bdd->lastInsertId();
} ?>
我错过了什么吗?
编辑:
通过更改解决:
return $bdd->lastInsertId();
至 :
echo $bdd->lastInsertId();
感谢 Michael Berkowski。
【问题讨论】:
-
从 PHP 脚本中,而不是
returning 您需要将值发送到echo $bdd->lastInsertId();以将其发送到 JavaScript 接收它的输出缓冲区。