Alright, I have a similar question up, but wanted to expand on it to see if I can get a better answer:
I'm trying to make a Jquery Ajax Post call to a PHP file that stores an email address in a table. After the successful ajax call I'm trying to change a buttons html.
Here is my HTML/Javascript in its entirety:
<!DOCTYPE html>
<html>
<head>
<!--Meta tags-->
<meta charset="utf-8">
<!--Title-->
<title>Startup Winnipeg</title>
<!--Stylesheets-->
<link rel="stylesheet" href="css/master.css">
<!--Script-->
<script type="text/javascript">
function empty() {
document.getElementById("email").value="";
}
function determine() {
if (document.getElementById("email").value.length==0){
document.getElementById("email").value="Email Address";
}
}
function validate() {
document.getElementById("registerform").submit();
}
</script>
</head>
<body>
<div id="container"></div>
<div id="topbar"></div>
<div id="header"></div>
<div id="register">
<div id="formcontainer">
<form action="" id="registerform" name="registerform" method="post" >
<input type="text" id="email" name="email" value="Email Address" onClick="empty()" onBlur="determine()" />
<button type="submit" id="join" name="join" onClick="validate()">Sign Up</button>
</form>
</div>
<div id="info">
<a href="http://www.facebook.com" target="_blank"><div id="fb"></div></a>
</div>
<div id="bar"></div>
</div>
<!--jQuery-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
//listen for click
$('form').on('submit', function(e) {
$.post('register.php', $("#registerform").serialize(), function() {
$('#join').html("Success!")
});
//disable default action
e.preventDefault();
});
</script>
</body>
</html>
Here is the PHP file that is writing the email address to a table:
<?php include("database_connection.php");?>
<?php
$email = $_POST['email'];
$email = mysql_real_escape_string($email);
mysql_select_db("suw",$con);
$sql="INSERT INTO emails (email) VALUES ('$email')";
mysql_query($sql,$con);
echo mysql_error();
?>
<?php include ("close_database_connection.php");?>
I'm having a couple of problems:
if I type in a full email address it won't write to the table, however, any other gibberish will write to the table
The button's html does not change upon the data being written to the table
Let me know what I am doing wrong - I have two books and have scoured the jquery api and I can't quite figure out what I'm doing wrong.