I’m wondering why my code is not updating the the ajax call when I click on a button. It shows only after I do page refresh. My Code below:
Here is the complete jquery/javascript code:
$(document).ready(function() {
$(“#savesubmainbutton”).live(‘click’,function(){
$.post('save-sub-main.php', $("#submainform").serialize(), function(data) {
$('#submain_save_message').html(data);
});
$('#submain').hide();
// Load data after submission
$.get('getsubmain.php', { mainid: "<?php echo $_GET['id']; ?>" },
function(data) {
$('.submain_message').html(data);
});
});
}); //end
This is the HTML form part
<div id="submain">
<form id="submainform" name="submainform" method="post" action="" enctype="multipart/form-data" >
<table><input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<!--<tr><td width="289" align="right">Jou naam:</td><td><input type="text" name="yourname" id="yourname" style="background-color:lightblue;" /></td></tr>
<tr><td width="289" align="right">Jou e-posadres:</td><td><input type="text" name="email" id="email" style="background-color:lightblue;"/></td></tr>-->
<tr><td width="289" align="right">Naam van die geskiedkundige figuur (bv. Jan van Riebeeck):</td><td><input type="text" name="name" id="name" /></td></tr>
<tr><td width="289" align="right">Foto van die geskiedkundige figuur :</td><td><input id="file_upload" name="file_upload" type="file" /></td></tr>
<tr><td width="289" align="right"><div id="filname"></div></td><td><input id="filename2" name="image" type="hidden" value="" /></td></tr>
<!--<tr><td width="289" align="right">Naam van jou inskrywing :</td><td><input type="text" name="title" id="title" /></td></tr>-->
<tr><td width="289" align="right">Datum waarop die inskrywing gemaak is (bv. 1652):</td><td><input type="text" name="date" id="date" /></td></tr>
<tr><td width="289" align="right">Status update (bv. Wat sien ek daar in die verte? Is dit 'n tafel?):</td><td><textarea name="message" id="message"></textarea></td></tr>
<input type="hidden" name="mainid" value="<?php echo $_GET['id']; ?>" />
<tr><td></td><td><div id="savesubmainbutton" style="color:blue"><input type="button" value="Save" /></div></td></tr>
</table>
</form>
Remember your ajax call is async – so the post has not returned the data before your get is executing – change your code as shown.
}); //end