Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    80
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Question Newsletter Signup submit problems

    Ok Guys I am wanting to code a newsletter signup script. What I have so far is just a Box for them to enter their email and a submit button but the submit button doesn't do anything it just sits there. here is the code on the main page

    Code:
    div class="left">
    			<label class="grey" for="username">Your Email:</label>
                <input class="field" type="text" name="email" value="" id="email" size="23" />
    
            <input type="button" name="submit" onclick="submit_me()" value="GO" />
            <script type="text/javascript" charset="utf-8">
    
                function submit_me() {
    
                    var email_value = $('#email').val();
    
                    $.post("ajax_subscribe.php", { email: email_value }, function(response) {
    
                        if (response!='') {alert(response)};
                        alert('Thank You !');
    
                    });
    
                }
    			</script>
                </div>
    here is the ajax_subscribe.php code

    PHP Code:
    <?php 
        $host 
    "****";
        
    $user "****";
        
    $password "****";
        
    $database "****";
        
    $server mysql_connect($host$user$password);
        
    $connection mysql_select_db($database$server);

        function 
    sql_quote($value) {
            
    $value str_replace('<?','',$value);
            
    $value str_replace('script','',$value);   
            if (
    get_magic_quotes_gpc()) {
                
    $value stripslashes($value);
            }
            if (!
    is_numeric($value)) {
                
    $value "'" mysql_real_escape_string($value) . "'";
            } else {
                if ((string)
    $value[0] == '0') {
                    
    $value "'" mysql_real_escape_string($value) . "'";
            }}
            return 
    $value;
        }
        
    $q "INSERT INTO newsletter_emails (email,category) VALUES (".sql_quote($_POST['email']).",'1')";

        
    mysql_query($q);

    ?>
    Any help is greatly appreciated!

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    well, your using JQuery to submit the data via Ajax, what happens when you click on the button? do you have the jquery api included somewhere in the file?

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    80
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by angst View Post
    well, your using JQuery to submit the data via Ajax, what happens when you click on the button? do you have the jquery api included somewhere in the file?
    Thanks for the reply.

    When I clik to GO button nothing happens. The page doesnt reload no thank you comes up and nothing gets sent to my database.

    I do have jquery already called on the page in the <head>

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    it's an ajax call, so the page won't reload. did you check the database to see if the email was inserted?

  • #5
    New Coder
    Join Date
    Aug 2011
    Posts
    80
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Yea and nothing is getting placed in it.

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    have you tried testing your php script outside of the ajax, like a direct call to the page?

    it looks like your insert is wrong:

    PHP Code:
        $q "INSERT INTO newsletter_emails (email,category) VALUES (".sql_quote($_POST['email']).",'1')"
    sql_quote($_POST['email']) is text and should be wrapped in single quotes like:

    PHP Code:
        $q "INSERT INTO newsletter_emails (email,category) VALUES ('".sql_quote($_POST['email'])."','1')"

  • Users who have thanked angst for this post:

    red71chevelle (04-19-2012)

  • #7
    New Coder
    Join Date
    Aug 2011
    Posts
    80
    Thanks
    19
    Thanked 0 Times in 0 Posts
    I changed that but it still didnt change anything thank you for catching it.

    How can I try the direct call? Just type the path to the file in manually in the address bar?

  • #8
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    to call it directly, just switch your $_POST to $_GET in your Insert statement and send the url & query string in your browser, like;

    PHP Code:
    http://YourDomain.com/ajax_subscribe.php?email=test@test.com 

  • Users who have thanked angst for this post:

    red71chevelle (04-19-2012)

  • #9
    New Coder
    Join Date
    Aug 2011
    Posts
    80
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Wow... Ok I am sorry for wasting your time. I realized I misspelled the file name on the server it is ajax_subcribe.php all I had to do was rename it and also set this line
    PHP Code:
    $q "INSERT INTO newsletter_emails (email,category) VALUES (".sql_quote($_POST['email']).",'1')"
    Back to the way it was and it all worked fine after that...

    If you wouldnt mind continuing to help me with this... do you happen to know a easy way to validate that they enter an actual email address? Right now you can type anything in and it sends it to the database.

  • #10
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    no problem, I would use both client side and server side validation;

    javascript:
    PHP Code:
    function validate(form_id,email) {
     
       var 
    reg = /^([A-Za-z0-9_-.])+@([A-Za-z0-9_-.])+.([A-Za-z]{2,4})$/;
       var 
    address document.forms[form_id].elements[email].value;
       if(
    reg.test(address) == false) {
     
          
    alert('Invalid Email Address');
          return 
    false;
       }

    and the php way: http://php.net/manual/en/filter.examples.validation.php
    PHP Code:
    if (filter_var($email_aFILTER_VALIDATE_EMAIL)) {
        echo 
    "This (email_a) email address is considered valid.";


  • Users who have thanked angst for this post:

    red71chevelle (04-19-2012)

  • #11
    New Coder
    Join Date
    Aug 2011
    Posts
    80
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Ok so where I have
    PHP Code:
    <script type="text/javascript" charset="utf-8">

                function 
    submit_me() {

                    var 
    email_value = $('#email').val();

                    $.
    post("ajax_subscribe.php", { emailemail_value }, function(response) {

                        if (
    response!='') {alert(response)};
                        
    alert('Thank You For Signing Up!');

                    });

                }
                
    </script> 
    can I just place
    PHP Code:
    function validate(form_id,email) {
     
       var 
    reg = /^([A-Za-z0-9_-.])+@([A-Za-z0-9_-.])+.([A-Za-z]{2,4})$/;
       var 
    address document.forms[form_id].elements[email].value;
       if(
    reg.test(address) == false) {
     
          
    alert('Invalid Email Address');
          return 
    false;
       }

    Right after it?

  • #12
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    you would need to update that function for your script and add it to your ajax call;

    PHP Code:
    function validate(email) { 
      
       var 
    reg = /^([A-Za-z0-9_-.])+@([A-Za-z0-9_-.])+.([A-Za-z]{2,4})$/; 
       if(
    reg.test(email) == false) { 
          
    alert('Invalid Email Address'); 
          return 
    false
       } 

    PHP Code:
                function submit_me() { 

                    var 
    email_value = $('#email').val(); 
                    if(
    validate(email_value) == false){
                
    alert("Invalid email address");
                    } else {

                $.
    post("ajax_subscribe.php", { emailemail_value }, function(response) { 

                    if (
    response!='') {alert(response)}; 
                    
    alert('Thank You For Signing Up!'); 

                });

                    } 

                } 

  • #13
    New Coder
    Join Date
    Aug 2011
    Posts
    80
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Ok so this is what I need then?
    PHP Code:
    <div class="left">
                <
    label class="grey" for="username">Your Email:</label>
                <
    input class="field" type="text" name="email" value="" id="email" size="23" />

            <
    input type="button" name="submit" onclick="validate(email); submit_me()" value="GO" />
            <
    script type="text/javascript" charset="utf-8">

                  function 
    validate(email) { 
      
       var 
    reg = /^([A-Za-z0-9_-.])+@([A-Za-z0-9_-.])+.([A-Za-z]{2,4})$/; 
       if(
    reg.test(email) == false) { 
          
    alert('Invalid Email Address'); 
          return 
    false
       } 
    }  

                  function 
    submit_me() {

                    var 
    email_value = $('#email').val();
                    if(
    validate(email_value) == false){
                 
    alert("Invalid email address");
                    } else {

                    $.
    post("ajax_subscribe.php", { emailemail_value }, function(response) {

                        if (
    response!='') {alert(response)};
                        
    alert('Thank You For Signing Up!');

                    });
                        }
                     }
            
    </script>
                </div> 

  • #14
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    no, you should execute it like this:

    onclick="submit_me();"

    I added "validate(email);" to your function "submit_me()", so on submit it will first validate the email address, than if successful it will continue with the insertion.

  • #15
    New Coder
    Join Date
    Aug 2011
    Posts
    80
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Ok sorry I am slow at this.. this is what I have. and now when you select go it just sits there again and doesnt do anything no pop up

    PHP Code:
    <div class="left">
                <
    label class="grey" for="username">Your Email:</label>
                <
    input class="field" type="text" name="email" value="" id="email" size="23" />

            <
    input type="button" name="submit" onclick="submit_me()" value="GO" />
            <
    script type="text/javascript" charset="utf-8">

                  function 
    submit_me() {

                    var 
    email_value = $('#email').val();
                    if(
    validate(email_value) == false){
                 
    alert("Invalid email address");
                    } else {

                    $.
    post("ajax_subscribe.php", { emailemail_value }, function(response) {

                        if (
    response!='') {alert(response)};
                        
    alert('Thank You For Signing Up!');

                    });
                        }
                     }
            
    </script>
                </div> 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •