I'm working on a webpage that features a popup window that prompts the user for his/her email address. The data should then be uploaded to a database upon submission.
However, the email address is not uploaded to the database and I am not sure why. A database connection is definitely established and no error messages are yielded.
I'm a real beginner at using AJAX/jQuery.
Inside index.php:
<form form id="email_submit" action="insert.php" method="post" target="_top">
<label>Email Address</label>
<input type="text" name = "emailaddress" />
<input type="submit" value="Add Email" onClick="send_data_to_server()">
</form>
Inside insert.php:
$username = "root";
$password = "root";
$hostname = "localhost";
$database = "emails";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("0");
echo "Connected to MySQL <br>";
$selected = mysql_select_db("emails", $dbhandle)
or die("0");
echo "connected to db <br>";
$youremail = $mysqli->real_escape_string($_POST["emailaddress"]);
echo "$youremail";
mysql_query("INSERT INTO email_table (emailAddress) VALUES ('$youremail')");
echo json_encode($youremail);
The AJAX script (placed at the end of the index page body):
function send_data_to_server() {
$("email_submit").submit(function (ev) {
ev.preventDefault();
$.ajax({
type : 'POST',
url : "insert.php",
data : { "formData" : $("#email_submit").serialize() },
datatype : 'html',
success: function(data) {
alert(data);
}
});
});
}
Output on the webpage:
Connected to MySQL
connected to db
Any help is appreciated.