-1

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:

  1. if I type in a full email address it won't write to the table, however, any other gibberish will write to the table

  2. 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.

koral
2,9873 gold badges44 silver badges71 bronze badges
asked May 30, 2012 at 22:46
3
  • 1
    .html() should be changing the button's text. Are you certain the callback function is being invoked? Commented May 30, 2012 at 23:00
  • Have you taken a look at the response code from the AJAX call in your console? Chances are the PHP is failing at some point and not invoking the callback function. Commented May 30, 2012 at 23:01
  • you have to remove the line document.getElementById("registerform").submit(); then the $('#join').html("Success!") command will work Commented May 30, 2012 at 23:12

1 Answer 1

4

Not sure it will help, but start by getting rid of all that inline JS, and multiple functions for same events etc.

Place the javascript in the head, and remove the inline handlers:

<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
 <script type="text/javascript">
 $(function() {
 $("#email").on({
 click: function() {
 this.value = "";
 },
 blur: function() {
 if (this.value.length===0) this.value = "Email Address";
 }
 });
 $("#join").on('click', function(e) {
 e.preventDefault();
 $.post('register.php', $("#registerform").serialize(), function(data) {
 $('#join').html(data) 
 });
 });
 });
 </script>
</head>
<body>
 <form action="" id="registerform" name="registerform" method="post" >
 <input type="text" id="email" name="email" value="Email Address" />
 <button type="submit" id="join" name="join">Sign Up</button>
 </form>
</body>

In your PHP, the includes could be inside the tags, and just return success or error:

<?php 
 include("database_connection.php");
 mysql_select_db("suw",$con);
 $email = mysql_real_escape_string($_POST['email']);
 $sql="INSERT INTO emails (email) VALUES ('$email')";
 mysql_query($sql,$con);
 if (mysql_errno()) {
 echo mysql_error(); // or just "error"
 }else{
 echo "Success!";
 }
 include ("close_database_connection.php");
?>
answered May 30, 2012 at 23:05
Sign up to request clarification or add additional context in comments.

2 Comments

wow, that worked beautifully - A follow up question - even though you refactored, most of that code is the same - what was the difference maker? Was my positioning of the Jquery at the bottom of the document causing issues?
No, probably not. I think the main problem is as commented by timaschew above, you had an onclick handler attached directly to the button that submitted the form, and then you had a jQuery function doing stuff on the submit function and preventing default etc. It's much easier to keep track of things when they are in the same place, and you see it all side by side, and not have a few inline JS handlers, some plain JS at the top, and then a little bit of jQuery sprinkled at the bottom.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.