i am making a school book library web project where students can select books and send request to librarian to request book to borrow. Unfortunately i am new to jquery so its hard for me to handle problems .
my code is
viewbook.php
<form method="post" action="" id="contactform">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name">
</div>
<div class="form-group">
<label for="email">Email Address:</label>
<input type="email" class="form-control" id="email">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<div id="code" class="result">
</div>
javascript code is
<script>
$(document).ready(function () {
$('.btn-primary').click(function (e) {
e.preventDefault();
var name = $('#name').val();
var email = $('#email').val();
$.ajax
({
type: "POST",
url: "submit.php",
data: { "name": name, "email": email },
success: function (data) {
$('.result').html(data);
//$('#contactform')[0].reset();
}
});
});
});
submit.php
<?php
require_once "db.php";
//insert into database
if(!empty($_POST['email'])) {
$name = htmlspecialchars(trim($_POST['name']));
$email = htmlspecialchars(trim($_POST['email']));
if (strlen($name) < 4) {
$errors['name'] = "*Name Is Short ! atleast 2 character*";
}
if (count($errors) === 0) {
$insert_user = "INSERT INTO book_requests (`name`, `email`, `bookid`, `user`)VALUES (:name,:email,:bookid,:userid)";
$result = $conn->prepare($insert_user);
$result->bindParam(':name', $name, PDO::PARAM_STR);
$result->bindParam(':email', $email, PDO::PARAM_STR);
$result->bindParam(':bookid', $bookid, PDO::PARAM_STR);
$result->bindParam(':user', $user, PDO::PARAM_STR);
$name = htmlspecialchars(trim($_POST['name']));
$email = htmlspecialchars(trim($_POST['email']));
$bookid= $_GET['bookid'];
$user = $_SESSION['login'];
$result->execute();
if ($result) {
echo "done<br>" ;
} else {
echo "not done<br>";
}
}
}
}
?>
my problem is bookid which is stored in $bookid= $_GET['bookid'] and userid which is stored in $user = $_SESSION['login'] which are on page 'viewbook.php' how to store them into database with form data using ajax.
please help me in this
My Answer:
Rate this post
4 of 5 based on 3892 votesComments