简体   繁体   中英

How can I direct a form's action attribute to itself when it is in a popup window?

I am trying to create a sign up pop-up window. The sign up functionality is done by a form. The form works just fine if it has its own page.

Now I try to embed it into a pop-up window. This is basically a hidden div with the form inside, that fades in if needed (jQuery). So we are talking about one single, let's say, index.php page.

If the sign up is successful, than its fine. But if the user's input is not acceptable (eg. too short password), then I need to inform the user.

The problem is, that the form's action attribute leads to itself, the page will be loaded again, and the pop-up window will be hidden again. How can I prevent this?

I tried this:

<?php
    if(isset($_POST['submit'])){
        echo "<script>$('.popupInfo').show(200);</script>";
    }
?>

but it doesn't work... Thank you!

Using a similar approach you can create a variable that determines whether or not to display the popup.

<?php
if(isset($_POST['button'])){    //check if form submitted via submit button
  //form submitted, show popup
  $showPopup = 'style="display: block;"';
  echo "Form Submitted";
}else{
  //form hasn't been submitted, dont show popup
  $showPopup = 'style="display: none;"';
  echo "Form NOT Submitted";
}

?>

<div id="popup" <?php echo $showPopup; ?> >

</div>

I have also added echo statements to the if else so that you can see if the php code is running as you would expect

<form onsubmit="event.preventDefault();return login();">

Apply the javascript function on the form fields, with onchange or onblur events , I guess this will work before form loading and if it doesn't work then,

Instead of form's action you can do this and give your form action="#" :

if(isset($_POST['submit']))
{
   // apply all the code you need to do on form submission
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM