0

I have a simple jquery modal box on my site. I'm new at jquery so I'm not sure how to keep the box open if an error is posed on the form that is being submitted. If submitted correctly redirect.

As of now when there is an error it reloads the page and have to press pop up again to view error.

Thanks in advance.

if(isset($_POST['submit'])) {
    $email = $_POST['email'];
    if(empty($_POST['name']) || empty($_POST['email'])) { 
        $error = "Please enter the info in the fields that are marked <br /> with &#042;";
    }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $error_email = "Please enter a valid email address";
    }else{
        $success = true;
$success = "Thank You"; 



     mysql_query("INSERT INTO watch_list 
    (name, email) VALUES('".$_POST['name']."', '".$_POST['email']."')") 
    or die(mysql_error());  
    header( 'Location: /watch.php' );
    }

}       

<div class="play_wrapper">
                <div class="play">
                    <a href="#" class="topopup"><img src="/styles/images/lx_play.png"/></a>
                        <div id="toPopup">
                            <div id="popup_content">
                                <div class="vid_form_text">
                                    Please enter your name and email to watch the film. <span class="green">Thank you.</span> 
                                </div>
                                <?php if(isset($success)) { ?>
                                <div class="success_watch">Thank You!</div>
                                <?php } ?>

                                <?php if(isset($error)) { ?>
                                <div class="error_watch">
                                    <?php echo $error; ?>
                                </div>
                                <?php } ?>
                                <?php if(isset($error_email)) { ?>
                                <div class="error_watch">
                                    <?php echo $error_email; ?>
                                </div>
                                <?php } ?>
                                <form method="post" action="#">
                                    <div class="form_text_sign_up_required">
                                        All fields marked with an asterisk * are required
                                    </div>
                                    <div class="form_text_sign_up">
                                        Name&#042;
                                    </div>
                                    <input type="text" name="name" value="<?php if(isset($_POST['name'])) {echo $_POST['name'];} ?>" /> <br /><br />

                                    <div class="form_text_sign_up">
                                        Email&#042;
                                    </div>
                                    <input type="text" name="email" value="<?php if(isset($_POST['email'])) { echo $_POST['email'];} ?>" /><br /><br /> 
                                    <input type='submit' name='submit' value='submit' class='post'/>
                                </form>

                            </div> <!--your content end-->
                        </div> <!--toPopup end-->
                        <div class="loader"></div>
                    <div id="backgroundPopup"></div>
                </div>
            </div>

<script>
    jQuery(function($) {

        $("a.topopup").click(function() {
                loading(); // loading
                setTimeout(function(){ // then show popup, deley in .5 second
                    loadPopup(); // function show popup
                }, 500); // .5 second
        return false;
        });

        /* event for close the popup */
        $("div.close").hover(
                        function() {
                            $('span.ecs_tooltip').show();
                        },
                        function () {
                            $('span.ecs_tooltip').hide();
                        }
                    );

        $("div.close").click(function() {
            disablePopup();  // function close pop up
        });

        $(this).keyup(function(event) {
            if (event.which == 27) { // 27 is 'Ecs' in the keyboard
                disablePopup();  // function close pop up
            }
        });

            $("div#backgroundPopup").click(function() {
            disablePopup();  // function close pop up
        });

        $('a.livebox').click(function() {
            alert('Hello World!');
        return false;
        });

         /************** start: functions. **************/
        function loading() {
            $("div.loader").show();
        }
        function closeloading() {
            $("div.loader").fadeOut('normal');
        }

        var popupStatus = 0; // set value

        function loadPopup() {
            if(popupStatus == 0) { // if value is 0, show popup
                closeloading(); // fadeout loading
                $("#toPopup").fadeIn(0500); // fadein popup div
                $("#backgroundPopup").css("opacity", "0.7"); // css opacity, supports IE7, IE8
                $("#backgroundPopup").fadeIn(0001);
                popupStatus = 1; // and set value to 1
            }
        }

        function disablePopup() {
            if(popupStatus == 1) { // if value is 1, close popup
                $("#toPopup").fadeOut("normal");
                $("#backgroundPopup").fadeOut("normal");
                popupStatus = 0;  // and set value to 0
            }
        }
        /************** end: functions. **************/
    }); // jQuery End
    </script>
LightningWrist
  • 937
  • 4
  • 20
  • 37
  • You'll have to use AJAX to accomplish what you want, unless you want to serve up the previous page pre-filled with the values submitted. It's pretty easy if you use jQuery, it's pretty much just `$.post` or `$.get`. – AlbertEngelB Jun 13 '13 at 21:17
  • @Dropped.on.Caprica Do you think it would be pretty easy to add AJAX to my existing code? Seems like jquery would be easier, although not sure what you are saying to do with the $.post and $.get – LightningWrist Jun 13 '13 at 21:28
  • It will be easy-ish. Easier than pre-populating pages with PHP! Give me a sec and I'll write up a short answer of how you'd handle this. – AlbertEngelB Jun 13 '13 at 21:31
  • Also: you can use jQuery to do AJAX calls. That's what `$.post()`, `$.get()`, and `$.ajax()` is for. – AlbertEngelB Jun 13 '13 at 21:46

1 Answers1

1

You'll want to handle this using AJAX. If you send the response back in JSON format, it makes it super easy to parse out if it was successful or not.

$('form').on('submit', function(e) {

    var form = $('form')[0];
    var data = new FormData(form);

    // http://api.jquery.com/jQuery.ajax/
    $.ajax({
        url : 'Your URL',
        data : data,
        type: 'POST',
        errror : function(xhr, status, err) {console.log(xhr, status, err);}, // This only gets thrown if form submits unsucessfully
        success: function(response, status) {
                // This assumes the response you're sending back from PHP is JSON
                var data = JSON.parse(response);

                if (data.error) {
                    // Handle form being unsuccessful
                } else {
                    // Handle success
                }
            }
    });

    // This is to prevent the form from actually submitting.
    e.preventDefault();
    return false;
});
AlbertEngelB
  • 16,016
  • 15
  • 66
  • 93
  • Your saying to add this in addition to the script I already have I'm assuming? Also not sure what to put here $('form') as well as for the method and action of the actual form, assuming this is in addition to my existing script. thanks – LightningWrist Jun 13 '13 at 22:00
  • Basically, I'm saying build the form, and set the form up to post to your URL correctly. On the PHP/ server side of things, you'll want to conduct business as normal, doing any of your DB work or whatever you need. Once you're done with your server-side logic, [echo](http://stackoverflow.com/questions/6739871/php-create-array-for-json) the result in [JSON](http://www.php.net/manual/en/function.json-encode.php) back. This front-end JS will catch the server response, and then you can use JS to show errors on the forms or update the page if successful. – AlbertEngelB Jun 14 '13 at 14:10
  • Ok I understand that, but your still not answering my question which I'm confused about. Is this in addition to what I already have script wise? Or do I need to integrate this in to what I have, or do I need to make a whole new script? thanks – LightningWrist Jun 14 '13 at 14:54
  • Basically, move your PHP post check to a new file, and have the POST URL send there. The JS code I included you'll want to add to your page's JS. You'll also have to manually add success/ failure code there as well. – AlbertEngelB Jun 14 '13 at 15:22
  • so I have the 'success failure code' in my php post check as is. Your saying replicate that in the designated areas in the code you provided? If yes, I don't know what kind of syntax to use for that. thanks for all the help. – LightningWrist Jun 14 '13 at 19:43
  • I'm saying to echo back a JSON object with success or fail for the $.ajax call to use. Your 'php post check' doesn't pass anything back to the client, it only creates a variable and does nothing with it. – AlbertEngelB Jun 14 '13 at 21:28
  • If you can't figure out how to send back a response you can use for the call or if you can't use jQuery well enough to display errors to the user, then I'm unable to help you. – AlbertEngelB Jun 14 '13 at 21:29