I have the following script which, prevents the form from being submitted and then uses ajax to make a call to a page
HERE is my form
<form method="post" action="makeBid.php" name="apply" id="makeBid">
<label for="amount">Bid Amount</label>
<input type="text" id="amount" name="amount" placeholder="Enter Bid Amount"/>
<label for="completionDate">Completion Date</label>
<input type="text" id="completionDate" name="completionDate" placeholder="Completion Date"/>
<label for="apply">Support Your Application</label>
<textarea name="msg" id="msg" class="application" placeholder="Enter A Message To Support Your Application"></textarea>
<button name="apply" id="apply" value="<?php echo $_POST['btnSubmit'] ?>" class="btn btndanger">Apply</button>
</form>
if(isset($_POST['apply'])) {
require_once('../controller/bids.php');
$bid = new Bid();
$bid->setAmount($_POST['amount']);
$amount = $bid->getAmount();
$bid->setDate($_POST['completionDate']);
$date = $bid->getDate();
$bid->setRemarks($_POST['msg']);
$msg = $bid->getRemarks();
echo $bid->processBid($_SESSION['userID'], $_POST['apply'],$amount, $date, $msg);
}
And then my Jquery and AJAX script which prevents default behavior.
$(function () {
var form = $('#makeBid');
var formMessages = $('#formResult');
// Set up an event listener for the contact form.
$(form).submit(function (e) {
// Stop the browser from submitting the form.
e.preventDefault();
// Serialize the form data.
var formData = $(form).serialize();
// Submit the form using AJAX.
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
}).done(function (response) {
// Make sure that the formMessages div has the 'success' class.
$(formMessages).removeClass('error').addClass('success');
// Set the message text.
$(formMessages).html(response); // < html();
// Clear the form.
$('').val('')
}).fail(function (data) {
// Make sure that the formMessages div has the 'error' class.
$(formMessages).removeClass('success').addClass('error');
// Set the message text.
var messageHtml = data.responseText !== '' ? data.responseText : 'Oops! An error occured and your message could not be sent.';
$(formMessages).html(messageHtml); // < html()
});
});
});
</script>
The console error im getting is uncaught reference error function is not defined in the first line of my script. As far as I can tell everything looks alright. Would greatly appreciate a second pair of eyes / opinion to scan over my script.
Much appreciated