php - Retrieve record ID after insert -
admittedly, bit out of league here, attempting project anyway.
background:
i developing page allow users enter data database. database has multiple tables have data inserted , updated. first action user takes select option drop down , click "create". clicking create button create record in 1 of tables. also, upon clicking "create" larger form displayed. once larger form complete, user can "save" work. planning on using update query allow user save work.
question:
using ajax post data (see example below), want return record id (primary key) page when visitor saves additional information, database knows record update.
//script
$(function(){ $('#create').click(function(event) { event.preventdefault(); $.ajax({ type: "post", url: "url", data: {"optimizationtype": $("#optimizationtype").val()}, success: function(results){ $("#mvt-experimentdetails").css("display", "inline-block"); $("#create").attr('value', 'save'); $("#create").attr('id', 'save'); } }); }); });
//php includes
$expid = mysqli_insert_id($con);
know suppose return in success area of ajax, not sure how that...any appreciated.
on php side, after run insert, you'll want last_insert_id
, echo that.
for example (using pdo , pseudocode):
insert table values (x,y) $id = $db->lastinsertid(); echo $id;
on client side, let me recommend $.post instead of $.ajax - find easier use. however, theory same both.
$.post('url', { data-key: data-value, next-data-key: next-data-value }, //now callback function (recordid) { //do recordid alert(recordid); });
Comments
Post a Comment