0

I am trying to create the echo via AJAX on a simple form but i do not succeed in it.

This is the form and js:

<script src="http://code.jquery.com/jquery-1.6.2.min.js" type="text/javascript"></script>

<script>
$(document).ready(function()
{   
    $(document).on('click', '.subscribe_newsletter', function()
    {       

        $('#subscribe_newsletter').val($(this).val());
            var data = $("#subscribe").serialize();

        $.ajax({

        type : 'POST',
        url  : 'email_subscribe.php',
        data : data,
        success :  function(data)
               {                        
                    $("#subscribe").fadeOut(500).hide(function()
                    {
                        $(".subscribe_wrapper").fadeIn(500).show(function()
                        {
                            $(".subscribe_wrapper").html(data);

                        });


                    });

               }
        });
        return false;
    });  

});
</script>
<div id="form" class="subscribe_wrapper">
   <form id="subscribe" method="POST">
      <input name="email_subscribe" type="text" />
      <input class="subscribe_newsletter" type="submit" name="submit" value="Subscribe">
   </form>
</div>

This is email_subscribe.php

// subscribe 
if (isset($_POST['email_subscribe'])) {   
   $email_add = $_POST['email_subscribe'] . ',' . "\n";
   $store = file_put_contents('database-email.txt', $email_add, FILE_APPEND | LOCK_EX);
   if($store === false) {
     die('There was an error writing to this file');
   }
   else {
     echo "$email_add successfully added!";
   }
}

The echo does not appear and neither the email is stored in the text file. Can someone tell me what i am doing wrong here?

5
  • Well then maybe $_POST['email_subscribe'] wasn’t set, in which case your PHP code would do nothing else whatsoever ... Commented Jan 29, 2018 at 15:45
  • 1
    Are you getting any errors on the server side or in the console? Commented Jan 29, 2018 at 15:45
  • 1
    I think your form is submitting before your AJAX request is firing. try adding a 'e.preventDefault() to your JS, and pass in e to the click handler function Commented Jan 29, 2018 at 15:46
  • I agree, this was a mistake. But it still does not work Commented Jan 29, 2018 at 15:53
  • Remove the form tags and use only the inputs Commented Jan 29, 2018 at 15:56

3 Answers 3

3

Try to change

$(document).on('click', '.subscribe_newsletter', function()

to

$(".subscribe_newsletter").click(function (e)

and add

e.preventDefault();
Sign up to request clarification or add additional context in comments.

Comments

1

Replace your Javascript code with this

$(document).ready(function()
{
    $('.subscribe_newsletter').click(function(e) {

      e.preventDefault();

        $('#subscribe_newsletter').val($(this).val());
            var data = $("#subscribe").serialize();

        $.ajax({

        type : 'POST',
        url  : 'email_subscribe.php',
        data : data,
        success :  function(data)
               {
                    $("#subscribe").fadeOut(500).hide(function()
                    {
                        $(".subscribe_wrapper").fadeIn(500).show(function()
                        {
                            $(".subscribe_wrapper").html(data);

                        });


                    });

               }
        });
        return false;
    });

});

Note that I have replaced $(document).on('click', '.subscribe_newsletter', function() with $('.subscribe_newsletter').click(function(e) and added e.preventDefault(); on the line below

1 Comment

Glad I could help
0

Try modify your code in this way:

<script src="http://code.jquery.com/jquery-1.6.2.min.js" type="text/javascript"></script>

<script>
$(document).ready(function()
{   
    $(document).on('click', '.subscribe_newsletter', function()
    {       
      var data =  $('#email_subscribe').val();
        $.ajax({
        type : 'POST',
        url  : 'email_subscribe.php',
        data : {email_subscribe: data},
        success :  function(data)
               {                        
                    $("#subscribe").fadeOut(500).hide(function()
                    {
                        $(".subscribe_wrapper").fadeIn(500).show(function()
                        {
                            $(".subscribe_wrapper").html(data);
                        });
                    });
               }
        });
        return false;
    });  
});
</script>
<div id="form" class="subscribe_wrapper">
   <form id="subscribe" method="POST">
      <input id="email_subscripe" name="email_subscribe" type="text" />
      <input class="subscribe_newsletter" type="submit" name="submit" value="Subscribe">
   </form>
</div>

Comments

Your Answer

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.