Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 4329
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 3, 20222022-06-03T17:27:59+00:00 2022-06-03T17:27:59+00:00

html – Mail subscription not working with Ajax & PHP

  • 0

[ad_1]

What I want to implement

I implemented a subscription form for my Mail Chimp. At first, I only used PHP and it worked.

After that, I wanted to upgrade my project so that the page won’t reload every time someone subscribes and in return it shows a nice image with a “thank you” message. For this job, I chose Ajax.

I followed some tutorials (from YouTube and other websites) but the form is not sending the data to Mail Chimp.

Bugs

  • If I click ‘Subscribe’ without any email entered, it behaves just like if it was a success and it displays me the ‘Thank You’ image.

  • If I enter a valid email and hit ‘Subscribe’ it reloads the page and after that it updates my URL with this: https://primus2.eu/?email=toni_mol%40yahoo.com&submit=

My conclusion

I think that the PHP script is not called, because as I said above, if I submit the form with empty fields it behaves just like it was a success.

PHP Script

Which, again, it works without the Ajax ‘upgrade’..

<?php
    if (isset($_POST['submit'])) {
        
        $email = $_POST['email'];

        $list_id = 'hidden';
        $api_key = 'hidden';
        
        $data_center = substr($api_key,strpos($api_key,'-')+1);
        
        $url="https://". $data_center .'.api.mailchimp.com/3.0/lists/'. $list_id .'/members';
        
        $json = json_encode([
            'email_address' => $email,
            'status'        => 'subscribed', //pass 'subscribed' or 'pending'
        ]);
        
        try {
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key);
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
            $result = curl_exec($ch);
            $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);
        
            if (200 == $status_code) {
                echo "The user added successfully to the MailChimp.";
            }
        } catch(Exception $e) {
            echo $e->getMessage();
        }

    }
?>

HTML Form

I removed the action="" and method="" from the <form> after I implemented the Ajax upgrade.


    <form>
                    <div class="form-group pt-4" id="form-inputs">
                      <input type="email" name="email" id="email" placeholder="Enter your email.." required="required">
                      <button class="btn" name="submit" id="subscribe-btn">Subscribe</button> 
                      <small id="emailHelp" class="form-text text-success"><i class="fa fa-check" aria-hidden="true"></i> We promise not to spam your inbox. </small> 
                    </div>

                    <div class="thank-you-image" id="thank-you-subscription">
                      <img src="assets/img/subscribe_success.png" alt="subsribe_success">
                    </div>
                  </form>

Ajax Script

Which is in the bottom of the same file with my HTML code.

<!-- Script to manage subscription form
       Using AJAX -->

  <script type="text/javascript">
    $(document).ready(function(){
      //Hide the thank you for subscription div
      $('#thank-you-subscription').hide();

      $('#subscribe-btn').click(function(){
        var email = $('#email').val();

        $.ajax({
          url: "assets/php/subscribe-mailchimp.php",
          method: "POST",
          data: {
            email:email
          },
          success:function(){
            $('#form-inputs').fadeOut('slow'); //Hides the form
            $('#thank-you-subscription').fadeIn('slow'); // Makes the thank you div appear
          }
        });

      });

    });
  </script>

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 8037 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1840 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1918 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.