i have a php form im trying to submit using ajax and its saving data properly but its always returning the error message not the success, any help please to go about it.
What I have tried:
this is the jquery ajax file.
$(document).ready(function() {
var loader='<img src="https://media.giphy.com/media/11ASZtb7vdJagM/giphy.gif" />';
$('#submit').click(function () {
$('.loading').html(loader).fadeIn();
var prog_description = $('input[name=prog_description]').val();
var type_of_contrib = $('select[name=type_of_contrib]').val();
var benefit_factor = $('input[name=benefit_factor]').val();
var budgeted_spend = $('input[name=budgeted_spend]').val();
var actual_amount = $('input[name=actual_amount]').val();
var current_status = $('select[name= current_status]').val();
var anticipated_completion = $('input[name= anticipated_completion]').val();
var form_data =
'prog_description='+prog_description+
'&type_of_contrib=' +type_of_contrib+
'&benefit_factor='+benefit_factor+
'&budgeted_spend='+budgeted_spend+
'&actual_amount='+actual_amount+
'¤t_status='+current_status+
'&anticipated_completion='+anticipated_completion;
$('.text').attr('disabled','true');
$.ajax({
url: "process.php",
type: "POST",
data: form_data,
success: function (html) {
if (html===1) {
$('#fupForm').fadeOut('slow');
$('.loading').fadeOut();
$('.message').html('Successfully Registered ! ').fadeIn('slow');
} else alert('Sorry, unexpected error. Please try again later.');
}
});
return false;
});
});
</script>
process.php file
<?php
session_start();
$link = mysqli_connect("localhost", "root", "", "socio_economic");
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "INSERT INTO socio_spends (prog_description,type_of_contrib,benefit_factor,budgeted_spend,actual_amount,current_status,anticipated_completion) VALUES (?, ?, ?,?,?,?,?)";
if ($stmt = mysqli_prepare($link, $sql)) {
mysqli_stmt_bind_param($stmt, "sssssss", $prog_description, $type_of_contrib, $benefit_factor, $budgeted_spend, $actual_amount, $current_status, $anticipated_completion);
$prog_description = (isset($_POST['prog_description']) ? $_POST['prog_description'] : '');
$type_of_contrib = (isset($_POST['type_of_contrib']) ? $_POST['type_of_contrib'] : '');
$benefit_factor = (isset($_POST['benefit_factor']) ? $_POST['benefit_factor'] : '');
$budgeted_spend = (isset($_POST['budgeted_spend']) ? $_POST['budgeted_spend'] : '');
$actual_amount = (isset($_POST['actual_amount']) ? $_POST['actual_amount'] : '');
$current_status = (isset($_POST['current_status']) ? $_POST['current_status'] : '');
$anticipated_completion = (isset($_POST['anticipated_completion']) ? $_POST['anticipated_completion'] : '');
if (mysqli_stmt_execute($stmt)) {
echo '1';
$socio_id =mysqli_insert_id($link);
$_SESSION['socio_id'] =$socio_id;
} else {
echo "ERROR: Could not execute query: $sql. " . mysqli_error($link);
}
} else {
echo "ERROR: Could not prepare query: $sql. " . mysqli_error($link);
}
header('Location:index.php');
mysqli_stmt_close($stmt);
mysqli_close($link);
exit();