I am using AJAX and PHP to process a form and I've tried for 3 hours and I can't figure out why it doesn't insert the data into the mysql server.
The connection to the server is working. The PHP and html form works when I combine it to one page. This signals to me that its the AJAX but the AJAX seems fine. I really don't know whats going on here. Please help me.
The php to process the code:
<?php
require_once 'config/config.php';
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$que1 = $_POST['que1'];
$sql = "INSERT INTO question_answers
(
user_id, que1, que2
)
VALUES
(
'99' ,'$que1', '3345'
)";
$conn->close();
?>
And the AJAX is here:
$(document).ready(function() {
$("#submit1").click(function() {
var que1 = $('#que1').val();
if (que1 == '')
{
alert("Please provide a response.");
}
else {
$.ajax({
type: "POST",
url: "process.php",
data: {
que1: que1
},
success: function(msg) {
alert($que1);
var url = "process.php";
$(location).attr('href', url);
},
error: function() {
alert('Error');
}
});
}
});
});
My form is standard:
<form action="" method="POST">
Question
<textarea class="form-control" name="que1" rows="3" id="que1"></textarea>
<button type="submit" class="btn btn-primary" id="submit1" name="submit1">Submit response</button>
</form>