2014-09-23 63 views
2

好吧,在我的添加联系人表单中,我使用Jquery和Php将数据插入到Mysql数据库。它成功地将数据插入到数据库中。现在我要将成功的页面重定向到index.php?cdid = $ last_id。但我怎么能得到这个$ last_id在jquery成功方法?我只能显示成功的消息。任何帮助?如何在Jquery成功方法中获得“最后插入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);  
?> 

更新:

我用下面的代码到PHP APGE:

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); 
     } 
+0

哪个表你retrive最后一个ID? – 2014-09-23 07:00:19

+0

只是返回新的cdid,并用javascript构建新的url。 – aleha 2014-09-23 07:00:53

+0

在您的PHP页面中首先删除$ data = array();然后做我在我的评论 – 2014-09-23 07:19:42

回答

0

再传给你的回应嵌套数组:

$data['msg'] = $msg; 
$data['last_insert_id'] = $last_id; 

echo json_encode($data); 

然后在JS,你需要将它们调整成这样:

success: function (response) { 
    //   ^now this will hold both the messages and the last insert id 
    var data = response.msg; // separate them, messages does in data 
    var last_id = response.last_insert_id; // last_id has the last insert id 

}, 
+0

我如何获得jQuery成功方法中的值?你能告诉我吗? – Shibbir 2014-09-23 07:02:14

+0

@Shibbir它将在'data'参数(成功函数)中可用。 – GuyT 2014-09-23 07:02:49

+0

哦。我现在正在查看它。 – Shibbir 2014-09-23 07:02:52

0

通过使用mysql_insert_id()你将能够获得最后的ID。

$last_id = mysql_insert_id(); 

然后在您的PHP代码中,在成功消息后执行以下操作。

$data['msg'] = 'your message';     
$data['last_id'] = $last_id; 
echo json_encode($data); 

然后执行以下操作来获得从阿贾克斯

success: function (output) { 
    var data = output.msg; // this is message 
    var last_id = output.last_id; // this is the id 
}, 

更改您的JavaScript下面的响应:

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

TO

<script> 
    $('#addcontact').submit(function(event) { 
     event.preventDefault(); 
     $.ajax({ 
      type: 'POST', 
      url: 'add_contact_process.php', 
      data: $(this).serialize(), 
      dataType: 'json', 
      success: function(output) { 
       //alert(output); 
       var result = jQuery.parseJSON(output); 
       alert(result.last_id); //this will give the last id 
       alert(result.msg); //this will give the message 
      }, 
      error: function(output) { 
       alert("not working whole process"); 
      } 
     }); 
    }); 
</script> 
+0

检查我的更新问题plz。现在呢? – Shibbir 2014-09-23 07:14:29

+0

那么,现在一切工作正常。但是当我按下提交按钮时,它显示我的自定义成功消息与最后插入的ID。这应该只显示我的自定义成功消息。我怎样才能做到这一点 ? – Shibbir 2014-09-23 07:29:57

+0

检查我更新的JavaScript代码。日Thnx – 2014-09-23 07:30:35

0

你可以得到最后的插入ID

mysql_insert_id().... 

BT,而不是这个,你应该使用

PDO ......这是非常简单和良好的...

http://php.net/manual/en/book.pdo.php

在PDOü可以通过只执行查询。 。

创建您的配置文件PDO实例..

<?php 
/* Connect to an ODBC database using driver invocation */ 
$dsn = 'mysql:dbname=testdb;host=127.0.0.1'; 
$user = 'dbuser'; 
$password = 'dbpass'; 

try { 
    $dbh = new PDO($dsn, $user, $password); 
} catch (PDOException $e) { 
    echo 'Connection failed: ' . $e->getMessage(); 
} 

?> 

您可以通过...获取数据

$sth = $dbh->prepare('SELECT name, colour, calories FROM fruit 
    WHERE calories < :calories AND colour = :colour'); 
$sth->bindParam(':calories', $calories); 
$sth->bindParam(':colour', $colour); 
$sth->execute(); 

you can get last insert id by 

$dbh->lastInsertId(); 

PDO是简单和安全的.....

相关问题