Bookmark mail using jquery ajax php
Here is an example of how you can use jQuery, AJAX, and PHP to bookmark an email:
HTML
<a href="#" id="bookmark-btn">Bookmark</a>
JavaScript (using jQuery)
$('#bookmark-btn').on('click', function() {
var email = '[email protected]'; // replace with the email address you want to bookmark
$.ajax({
type: 'POST',
url: 'bookmark.php',
data: { email: email },
success: function(data) {
if (data === 'success') {
alert('Email bookmarked successfully!');
} else {
alert('Error bookmarking email. Please try again.');
}
}
});
});
PHP (bookmark.php)
<?php
// Connect to your database
$db = mysqli_connect('your_host', 'your_username', 'your_password', 'your_database');
// Check connection
if (!$db) {
die('Connection failed: '. mysqli_connect_error());
}
// Get the email address from the AJAX request
$email = $_POST['email'];
// Check if the email is already bookmarked
$query = "SELECT * FROM bookmarks WHERE email = '$email'";
$result = mysqli_query($db, $query);
if (mysqli_num_rows($result) > 0) {
// Email is already bookmarked, do nothing
} else {
// Add the email to the bookmarks table
$query = "INSERT INTO bookmarks (email) VALUES ('$email')";
mysqli_query($db, $query);
}
// Close the database connection
mysqli_close($db);
// Return a success message to the AJAX request
echo 'success';
?>
Database schema
Create a table called bookmarks
with the following columns:
id
(primary key, auto-incrementing integer)email
(varchar)
How it works
- When the user clicks the "Bookmark" button, the JavaScript code sends a POST request to the
bookmark.php
script using jQuery's AJAX function. - The
bookmark.php
script connects to the database and checks if the email address is already bookmarked. If it is, the script does nothing. - If the email is not bookmarked, the script adds it to the
bookmarks
table. - The script returns a success message to the AJAX request.
- The JavaScript code receives the success message and displays an alert box to the user.
Note: This is a basic example and you should consider implementing additional security measures, such as validating user input and using prepared statements to prevent SQL injection.