I have a single user who is getting this error while using a MAC, but I am unable to replicate it. He has lost changes several times. I am looking for suggestions on how to better code this so there is no duplicate call. I have seen suggestions for things like (not using onclick inside the HTML, using things like stoppropogation or prevent event default, but do not really know what any of this does).

I think I have an AJAX call (to a DB Update function) that is getting double called, and in the second instance, it is wiping out the data saved in the first instance. I’m not positive about the double call, but I know that the initial update is successful as it updates the screen inside the ‘success’ response from the AJAX call, but then if I refresh the screen later, the updates are gone and then I check the DB where they are also gone.


1. There is a ‘Save Changes’ button on the page (inside of a modal window) that gets clicked when some data pertaining to a photo (caption, which building it is assigned to) is changed and needs to be saved:

<button type="button" class="btn btn-success" data-dismiss="modal" style="display:inline-block; border-color: none; color: white;text-shadow: none;" onclick="savephoto()">Save Changes</button>

2. When the button is clicked, it calls this function that populates a few variables and then makes an AJAX POST call to save the changes to the DB.

function savephoto(){
var subid = $('#previmgid').val().split('_')[2];
var pid = $('#picid_'+subid).val();
var readonly = $('#picreadonly_'+subid).val();
if(pid!=""){
var bid = $('#buildinglist').val(); //building ID
var desc = $('#imgdiscpre').val(); //caption
var keys = $('#imgkeys').val(); //keywords
var sharephoto = ""; //this determines if the user has readonly or write permission
if($('#sharewithcustomer_cb').is(':checked')){
sharephoto = 0;
}else{
sharephoto = 1;
}
$.ajax({
url: "<?=APPLICATION_URL.'vpics/updatephoto/pid/'?>"+pid, type: "POST",
data: {bid: bid, desc: desc, keys: keys, readonly: readonly, sharephoto:sharephoto, },
dataType : 'json',
success: function(data)
{
//update desc, building info
$('#issuepic_disc_'+subid).html(desc.replace(/\r\n|\n|\r/g, '<br />'));
$('#pickeys_'+subid).val(keys);
if (bid != null){
var bname = '';
if ($('#buildinglist option[value="'+bid+'"]').text() != 'N/A'){
bname = $('#buildinglist option[value="'+bid+'"]').text();
}
$('#buildingname_'+subid).html(bname);
$('#imgbuilding_'+subid).val(bid);
}
}
});
}

3. This is the function that the AJAX POST calls in the PHP controller file:

public function updatephotoAction(){
global $sqldb,$mySession, $site_errmsg, $site_successmsg;
$this->_helper->viewRenderer->setNoRender();
$arr=$this->getRequest()->getParams();
$db = new Db();
$pid = $arr['pid'];
$desc = $arr['desc'];
$bid = $arr['bid'];
$keys = $arr['keys'];
$readonly = $arr['readonly'];
$sharephoto = $arr['sharephoto'];
$condition=" mvp_id ='$pid'";
$qry = "select * from $sqldb[VENDORPICS] where ".$condition."";
$roles = $db->runQuery($qry);
$vendorid = $mySession->user['vendor_id'];
if(isset($roles[0])){
if ($readonly != 1){
$data = array(
'mvp_disc' => $desc,
'mvp_building_id' => $bid
);
$db->modify($sqldb['VENDORPICS'], $data, $condition);
if ($bid != '' && $sharephoto == 0){
$qry = "SELECT mbs_user FROM $sqldb[BUILDINGSHARE] WHERE mbs_owner = '$vendorid' AND mbs_building = '$bid'";
$res = $db->runQuery($qry);
if (isset($res[0])){
//add copy to each user account
foreach ($res as $user){
$uid = $user['mbs_user'];
$data = array(
'created' => time(),
'mvps_owner' => $vendorid,
'mvps_user' => $uid,
'mvps_pic' => $pid
);
$res3 = $db->save($sqldb['VENDORPICSSHARE'], $data);
}
}
}
}
if (strlen($keys) > 0){
$keys = explode(',', $keys);
$cond = " mvpk_user = '$vendorid' AND mvpk_img = '$pid'";
$db->delete($sqldb['KEYWORDS'], $cond);
foreach ($keys as $key){
if (strlen($key) > 1){
$data = array(
'mvpk_keyword' => trim($key),
'mvpk_user' => $vendorid,
'mvpk_img' => $pid
);
$res4 = $db->save($sqldb['KEYWORDS'], $data);
}
}
}
echo '1';exit;
}
echo '0';exit;
}