Link to home
Start Free TrialLog in
Avatar of kosmas
kosmasFlag for Greece

asked on

How to force a new pop-up window when 'Submit' button is clicked

Hello to everyone,

I have a php/html form script where the user enters some details and a result is returned. Here's part of the form used:

  <form id="form1" name="form1" method="post" action="">
    <p>Encrypted Password:
      <input name="encrypted" maxlength="128" size="50" type="text" id="encrypted" />
&nbsp;&nbsp;&nbsp;<input name="dec" type="submit" id="dec" value="Submit" />

What I want to do is when the user presses the 'Submit' button, I'd like a new window to pop up, that will load a specific URL I will provide.

Any help is much appreciated.

Thank you.
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Add
target="_blank"
To the form tag
Avatar of xterm
xterm

Try this:

<form method=post target=_self>
<input type=submit value=Submit onClick="return openWindow('$url',500,300);">
</form>
Avatar of kosmas

ASKER

Xterm,  would you be kind enough to actually inset your suggested code into the one I have already pasted?   The code you suggest, should not modify the functionality of the existing form.

Many thanks
Absolutely, and glad you posted back, because I forgot to include the javascript that you'll need for that function to work.  Here you go:


<html>

<head>
<script language=JavaScript>
  function openWindow(url,width,height) {
    popupWin = window.open(url, 'remote', 'scrollbars=0, resizable=0,width='+width+',height='+height);
    return false;
  }
</script>
</head>


<form id="form1" name="form1" method="post" action="">
    <p>Encrypted Password:
      <input name="encrypted" maxlength="128" size="50" type="text" id="encrypted" />
&nbsp;&nbsp;&nbsp;<input name="dec" type="submit" id="dec" value="Submit" onClick="return openWindow('http://www.someurl.com',500,300);"/>
</form>

</html>

Open in new window

"not change functionality"
your code will stop submission and not work in case of popup blockers
kosmas: is the popup unrelated to the form result?
I suggest you return the following from the form submission

<script type="text/javascript">
window.onload=function() {
  var w = window.open("somepage.html","_blank","width=400,height=400");
  if (!w) alert('Sorry, you have blocked popups')
}
</script>

Alternatively ajax the form result and show the popup in that part of the script
Avatar of kosmas

ASKER

To make this clear for everyone, here is the full code of the page.  I'd like when someone presses the 'submit' button, the script to continue as it does now, but also pop up an additional window with a URL that I will provide.  

You can use the code below and post it with your recommended changes.

Many thanks everyone!

  <form id="form1" name="form1" method="post" action="">
    <p>Encrypted Password:
      <input name="encrypted" maxlength="128" size="50" type="text" id="encrypted" />
&nbsp;&nbsp;&nbsp;<input name="dec" type="submit" id="dec" value="Submit" />
    <br/>      
    <br/>
    Password:
    <input name="decrypted" type="text" size="50" id="decrypted" value="
<?php
    $Regex = "/^[A-z0-9]+$/";

    if(isset($_POST['encrypted'])) {
        if(!strlen($_POST['encrypted'])) {
            echo "Input was empty";
            return;
        }
        if(preg_match_all($Regex, $_POST['encrypted'], $Matches)) {
            $command = "./decrypt.pl ".$_POST['encrypted'];
            htmlspecialchars(system($command));
        } else {
            echo "Invalid input";
            return;
        }
    }
?>
"/>
<br>

<form id="form1" name="form1" method="post" action="">
    <p>Encrypted Password:
      <input name="encrypted" maxlength="128" size="50" type="text" id="encrypted" />
&nbsp;&nbsp;&nbsp;<input name="dec" type="submit" id="dec" value="Submit" />
    <br/>      
    <br/>
    Password:
    <input name="decrypted" type="text" size="50" id="decrypted" value="
<?php
$ok = false;
    $Regex = "/^[A-z0-9]+$/";

    if(isset($_POST['encrypted'])) {
        if(!strlen($_POST['encrypted'])) {
            echo "Input was empty";
            return;
        }
        if(preg_match_all($Regex, $_POST['encrypted'], $Matches)) {
            $command = "./decrypt.pl ".$_POST['encrypted'];
            htmlspecialchars(system($command));
            $ok=true;
        } else {
            echo "Invalid input";
            return;
        }
    }
if ($ok) {?>
<script type="text/javascript">
window.onload=function() {
  var w = window.open("somepage.html","_blank","width=400,height=400");
  if (!w) alert('Sorry, you have blocked popups')
}
</script>
<?php
}
?>
"/>
<br>

Open in new window

Avatar of kosmas

ASKER

mplungjan,

Unfortunately the script did not work. When clicking on the submit button, I get the decrypted password, and the following as shown in the attached image: User generated image
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial