I know this is a repost, and I have looked in mostly all other posts about the subject both using Ajax, jQuery and those not using a submit button in their form, but none of the answers doesn't seem to be working on my specific code.
I have the following form:
<ul id="btn_MyJobs" data-role="listview" data-inset="true">
<li id="MyJobs_List" class="push">
<form name="MyJobs" id="MyJobsForm" action="" method="post">
<?php do { ?>
<input type="hidden" name="MyJob" value="<?php echo $row_Recordset1['cargo']; ?>">
<input type="submit" name="submit" value="<?php echo $row_Recordset1['cargo']; ?>"><br/><br/>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</form>
</li>
</ul>
I want the form to be submitted to the code below (on the same page), so that I can get the SQL Value String to use in my WHERE clause:
mysql_select_db($database_Connection, $Connection);
$query_Recordset_MyJobs = "SELECT * FROM jobs WHERE cargo = ".GetSQLValueString($_POST['MyJob'], 'text');
$Recordset_MyJobs = mysql_query($query_Recordset_MyJobs, $Connection) or die(mysql_error());
$row_Recordset_MyJobs = mysql_fetch_assoc($Recordset_MyJobs);
$totalRows_Recordset_MyJobs = mysql_num_rows($Recordset_MyJobs);
But I want to submit the form without refreshing the page, so when the submit button is clicked, the form is sent to the SQL Value String but the page is not refreshing or loading. So I have tried using Ajax as following:
<script>
$(function () {
$('#MyJobsForm').on('submit', function (e) {
e.preventDefault();
$.ajax(
{
type: 'post',
data: $('#MyJobsForm').serialize(),
success: function (data)
{
alert('form was submitted');
}
});
});
});
</script>
When I run the code, it shows me the alert message, but the call to the database is not receiving the data from the form. I figure there is something wrong with the Ajax code, but I can't figure out what. Can someone please help me submit the form without without refreshing the page? Ajax isn't a necessity.
Below is an example of an ajax call that submits a form using a select change and will change the whole page from the response that it receives from the change. You can use something like this. You do not have to have a formal submit code. You do not even have to have a form. All you need is the values of the data that you want to submit. I would recommend to using a separate PHP file to actually call in your code and only replacing specific portions of the page.
$(document).on('change', '#insurecode', function(){
var val = $('#groupnumtxt').val(), insure = $('#insurecode').val(), mbid = $('#mbid').val();
$.ajax({
type: "POST",
data: { groupnum: val, insurecode: insure, mbid: mbid }, // serializes the form's elements.
success: function(data){
switch(data){
$(document).html(data);
}
}
});
});
Replace $(document).html(data);
with alert(data)
if it will make you more comfortable as to what I'm saying.