开发者

Loading a thank you pop up div on form submit

开发者 https://www.devze.com 2023-04-12 13:36 出处:网络
I am having issues loading a jQuery modal window upon submit. I want a div to load upon submit to thank the user for entering a competition. The div opens fine if I add an a href like: <a href=\"

I am having issues loading a jQuery modal window upon submit.

I want a div to load upon submit to thank the user for entering a competition. The div opens fine if I add an a href like: <a href="#dialog" name="modal">click here"</a>

However I can't seem to make this popup once submit. Am I doing something wrong!?

This is my form php code:

<?php
if($_POST['formSubmit'] == "Submit")
{
    $errorMessage = "";

  if(empty($_POST['formName']))
  {
    $errorMessage .= "<li>You forgot to enter your name</li>";
  }
  if(empty($_POST['f开发者_如何学编程ormTown']))
  {
    $errorMessage .= "<li>You forgot to enter your town</li>";
  }

    $varName = $_POST['formName'];
    $varTown = $_POST['formTown'];
    $varAge = $_POST['formAge'];
    $varEmail = $_POST['formEmail'];

    $varOne = $_POST['hidden-one'];
    $varTwo = $_POST['hidden-two'];
    $varThree = $_POST['hidden-three'];
    $varFour = $_POST['hidden-four'];
    $varFive = $_POST['hidden-five'];

    if(empty($errorMessage)) 
    {
        $fs = fopen("mydata.csv","a");
        fwrite($fs,$varName . ", " . $varTown . ", " . $varAge . ", " . $varEmail . ", " . $varOne . $varTwo . $varThree . $varFour . $varFive . "\n");
        fclose($fs);

        header("Location: #dialog");
        exit;
    }
}
?>

This is the jquery code:

<script>

$(document).ready(function() {  

    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();

        //Get the A tag
        var id = $(this).attr('href');

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set heigth and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect     
        $('#mask').fadeIn(1000);    
        $('#mask').fadeTo("slow",0.8);  

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);

        //transition effect
        $(id).fadeIn(2000); 

    });

    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();

        $('#mask').hide();
        $('.window').hide();
    });     

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });         

});

</script>

This is my html:

<form action="enter.php" method="post" target="_self">      
    <input class="submit" type="submit" name="formSubmit" value="Submit">
</form>


<div id="boxes">

<div id="dialog" class="window">
    <p>Modal content here</p>
</div>
</div>

</div>
</div>

<!-- Mask to cover the whole screen -->
<div id="mask"></div>
</div>


It sounds like you may be slightly confused as to what's happening inbetween your web browser and your web server.

Your web browser is running a client which renders HTML/CSS and executes JavaScript. Your browser will submit a request to the web server which executes the PHP to generate a response.

Once your form has been submitted the request is made to the web server, which generates a response - usually another HTML page. This is where your thank you message should be.


If you have a submit button and do not call preventdefault or something like that, your form get's posted and your page refreshed. So your js code is not executed anymore. What you need to do is use a normal button or link and bind a onclick event to it that first shows the thank you popup and than 'manually' submits the form through JavaScript.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号