I'm trying to insert into my MySQL database with an onSubmit() function... For the main part, my function works. However, it keeps inserting a 1
into my DB where the logbook_id
field is (the logbook_id
should NOT be a 1 every time). Here's my (relevant) html:
echo '<div class="logbook_box" id="logbook'.$id.'"
style="display: block; overflow: hidden;">';
echo '<form action="cgi-bin/read_log.php" method="post"
onSubmit="return ajaxSubmit(this, ' . $id . ');">' .
'<input type="hidden" value="'.$id.'" id="id" name="id" />'.
'<input style="float: right; width: 20%" type="submit" value="Mark as Read" />' .
'</form>';
Here's the onSubmit function:
<script type="text/javascript" src="cgi-bin/jquery-2.0.3.js"></script>
<script>
var ajaxSubmit = function(form, id) {
div = document.getElementById("logbook" + id);
div.style.display = "none";
// fetch where we want to submit the form to
var url = $(form).attr('action');
var id = $('#id').val();
// setup the ajax request
$.ajax({
url: url,
data: { id : id },
dataType: "json",
success: function() {
if(rsp.success) {
alert('Marked as read, thanks!');
}
}
});
// return false so the form does not actually
// submit to the page
return false;
}
</script>
And here's my PHP file:
<?php
include('/var/www/olin2/includes/functions.php');
$con = connect_db();
session_start();
$id = isset($_POST['id']);
$username = $_SESSION['username'];
$query = 'insert into read_logbook(logbook_id, username)
values("'.$id.'", "'.$username.'")';
mysqli_query($con, $query) or die(mysqli_error($con));
?>
So, the <div>
disappears like it should when Submit is clicked, which shows that the id
is getting passed correctly. The PHP script is called, but it either doesn't input anything into the database, or (even more strangely) it will submit my username and the value 1
into the database if I change the PHP from $_POST
to $_GET
without changing the method in the form.
I'm new at ajax/javascript, so I'm sure it has something to do with the function, but I'm confused and at a complete loss... Any ideas?