My ajax post is working fine when I pass it to my php with just a echoing the data that the ajax sent to it. I want it to insert now to my database and it seems my php code is not working...
html
<form id="member" method="post" action="../php/connect.php">
<fieldset style="width:400; border-width:6px;" align="center" >
<legend><h3>Member Log</h3><br><br></legend>
<p id="get_member"></p>
<input class="fname" type='text' name='firstname' id='firstname' placeholder="First Name" required/>
</br><input class="lname" type='text' name='lastname' id='lastname' placeholder="Last Name" required /></br>
</br><label for="male">Male </label><input type="radio" name="gender" id="male" required value="Male" />
<label for="female">Female </label><input type="radio" name="gender" id="female" value="Female" /><br />
<input type="hidden" name="date" id="date" value="">
<input type="hidden" name="time" id="time" value="">
<br><br><input class="submit" type="submit" name="submit">
</fieldset>
</form>
jquery ajax post..
<script type="text/javascript">
$(document).ready(function(){
var my_date_format = function(){
var d = new Date();
var month = ['Jan ', 'Feb ', 'Mar ', 'Apr ', 'May ', 'Jun ', 'Jul ', 'Aug ', 'Sep ', 'Oct ', 'Nov ', 'Dec '];
var date = d.getDate() + " " + month[d.getMonth()] + ", " + d.getFullYear();
var time = d.toLocaleTimeString().toLowerCase().replace(/([\d]+:[\d]+):[\d]+(\s\w+)/g, "$1$2");
return (date);
};
var my_time_format = function(){
var d = new Date();
var time = d.toLocaleTimeString().toLowerCase().replace(/([\d]+:[\d]+):[\d]+(\s\w+)/g, "$1$2");
return (time);
};
//console.log(my_date_format());
// output 6 Jul, 2014 11:28 am
$('#member').submit(function(){
$('#date').val(my_date_format);
$('#time').val(my_time_format);
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data){
$('#get_member').html(data);
}
});
$("#member")[0].reset();
event.preventDefault();
});
});
</script>
php connect.php
<?php
error_reporting(0);
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$gender = $_POST['gender'];
$time = $_POST['time'];
$date = $_POST['date'];
$servername = "localhost";
$username = "root";
$password = "";
$db = "rmsdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO walkin_member (id,firstname, lastname, gender, time, datestamp)
VALUES ('',$fname', '$lname', '$gender', '$time', '$date')";
if ($conn->query($sql) === TRUE) {
echo "Recent Log" . "</br>First Name: " . $fname . "</br>Last Name: " . $lname . "</br>Gender: " . $gender . "<br />Time: " . $time . "<br />Date: " . $date;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$connect->close();
?>
Please check
$fname'
here '$fname'
missing quote.
sql should be
$sql = "INSERT INTO walkin_member (id,firstname, lastname, gender, time, datestamp)
VALUES ('','$fname', '$lname', '$gender', '$time', '$date')";
0 comments:
Post a Comment