好吧,在我的添加联系人表单中,我使用 Jquery 和 Php 将数据插入到 Mysql 数据库。已成功将数据插入数据库。现在我要将成功的页面重定向到index.php?cdid=$last_id。但我怎样才能得到这个jquery成功方法中的$last_id?我只能显示成功的消息。有帮助吗?
Jquery代码:
<script>
$('#addcontact').submit(function(event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: 'add_contact_process.php',
data: $(this).serialize(),
dataType: 'json',
success: function (data) {
$('#success').html('');
$.each( data, function( key, value ) {
if(key !== 'error') {
$('#success').append('<p>'+value+'</p>');
}
});
if( ! data.error) {
$('#hide').hide();
setTimeout(function () {
$('input[type=submit]').attr('disabled', false);
window.location.href = "../index.php";
//window.location.href = "../index.php?redcdid=<?php echo $cdid; ?>";
// need something like above
}, 5000);
}
}
});
});
</script>
php代码:
<?php
ob_start();
@session_start();
require_once("../config.php");
$company_name = ucfirst($_POST['company_name']);
$family_name = ucfirst($_POST['family_name']);
$msg = array();
$msg['error'] = false;
if(empty($company_name)){
$msg[] = "<font color='red'>Company name required. </font>";
$msg['error'] = true;
}
if(empty($family_name)){
$msg[] = "<font color='red'>Family name required. </font>";
$msg['error'] = true;
}
if($msg['error'] === false){
$query_2 = "INSERT INTO contact_details (cdid, family_name, given_name, work_phone, mobile_phone, email, email_private, user_id, created_date, cid) VALUES('', '$family_name', '$given_name', '$work_phone', '$mobile_phone', '$email', '$email_private', '$user_id', '$date', '$cid')";
$query_2 = mysql_query($query_2);
$last_id = mysql_insert_id();
if($query_2){
$msg[] = '<font color="green"><strong>Successfully added a new contact</strong>. </font>';
$another = "close";
}
else{
$msg[] = '<font color="red">Sorry we can not add a new contact details. </font>';
$msg[] .= mysql_error();
$another = "close";
}
}
echo json_encode($msg);
?>
Update:
我在 PHP 页面中使用以下代码:
if($query_2){
$msg[] = '<font color="green"><strong>Successfully added a new contact</strong>.
</font>';
$msg['last_id'] = $last_id;
}
我正在使用以下 jquery 代码:
if( ! data.error) {
$('#hide').hide();
setTimeout(function () {
$('input[type=submit]').attr('disabled', false);
var last_id = data.last_id;
window.location.href = "../index.php?redcdid=last_id";
}, 5000);
}