PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 12, 2022

[FIXED] How can I move alert message div out of <form> tag

 July 12, 2022     alert, forms, html, javascript, message     No comments   

Issue

I am trying a code where there is a form to accept email address for newsletter subscription;

When the alert message is displayed on empty field or invalid email submit, the alert message div is created within the form tag; I want the message div out of the form tag. Below are codes in use;

JAVASCRIPT

// newsletter

    (function(){

        var subscribe = $('#subscribe');
        subscribe.append('<div class="message_container_subscribe"></div>');
        var message = $('.message_container_subscribe'),text;

        subscribe.on('submit',function(e){
            var self = $(this);

            if(self.find('input[type="email"]').val() == ''){
                text = "Please enter your e-mail!";
                message.html('<div class="alert_box color_red r_corners error"><i class="fa fa-exclamation-triangle"></i> '+text+'</div>')
                    .slideDown()
                    .delay(4000)
                    .slideUp(function(){
                        $(this).html("");
                    });

            }else{
                self.find('span.error').hide();
                $.ajax({
                    type: "POST",
                    url: "newsletter.php",
                    data: self.serialize(), 
                    success: function(data){
                        if(data == 1){
                            text = "Your email has been sent successfully!";
                            message.html('<div class="alert_box r_corners color_green success"><i class="fa fa-check-square"></i> '+text+'</div>')
                                .slideDown()
                                .delay(4000)
                                .slideUp(function(){
                                    $(this).html("");
                                })
                                .prevAll('input[type="email"]').val("");
                        }else{
                            text = "Invalid email address!";
                            message.html('<div class="alert_box color_red r_corners error"><i class="fa fa-exclamation-triangle"></i><p>'+text+'</p></div>')
                                .slideDown()
                                .delay(4000)
                                .slideUp(function(){
                                    $(this).html("");
                                });
                        }
                    }
                });
            }
            e.preventDefault();
        });

    })();

HTML

<form action="newsletter.php" method="post" name="subscribe-form" id="subscribe" class="subscribe-form">
                    <input type="email" placeholder="Your email address" id="emailnews" class="email" name="emailnews">
                    <button type="submit" value="Submit" class="submit">Subscribe</button>
                </form>

SNAPSHOT FROM CODE INSPECT IN CHROME


Solution

You can use jQuery after() instead of append. Like this:

$("form").after('<div class="message_container_subscribe"></div>');

// newsletter

    (function(){

        var subscribe = $('#subscribe');
        $("form").after('<div class="message_container_subscribe"></div>');
        var message = $('.message_container_subscribe'),text;

        subscribe.on('submit',function(e){
            var self = $(this);

            if(self.find('input[type="email"]').val() == ''){
                text = "Please enter your e-mail!";
                message.html('<div class="alert_box color_red r_corners error"><i class="fa fa-exclamation-triangle"></i> '+text+'</div>')
                    .slideDown()
                    .delay(140000)
                    .slideUp(function(){
                        $(this).html("");
                    });

            }else{
                self.find('span.error').hide();
                $.ajax({
                    type: "POST",
                    url: "newsletter.php",
                    data: self.serialize(), 
                    success: function(data){
                        if(data == 1){
                            text = "Your email has been sent successfully!";
                            message.html('<div class="alert_box r_corners color_green success"><i class="fa fa-check-square"></i> '+text+'</div>')
                                .slideDown()
                                .delay(4000)
                                .slideUp(function(){
                                    $(this).html("");
                                })
                                .prevAll('input[type="email"]').val("");
                        }else{
                            text = "Invalid email address!";
                            message.html('<div class="alert_box color_red r_corners error"><i class="fa fa-exclamation-triangle"></i><p>'+text+'</p></div>')
                                .slideDown()
                                .delay(4000)
                                .slideUp(function(){
                                    $(this).html("");
                                });
                        }
                    }
                });
            }
            e.preventDefault();
        });

    })();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="newsletter.php" method="post" name="subscribe-form" id="subscribe" class="subscribe-form">
                    <input type="email" placeholder="Your email address" id="emailnews" class="email" name="emailnews">
                    <button type="submit" value="Submit" class="submit">Subscribe</button>
                </form>



Answered By - Rohit
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing