简体   繁体   中英

PHP: Form prevent multiple tabs.

I'm currently using javascript to prevent link to be opened on multiple browser tabs every time user click on it. Let say my link destination is google, it will create a new tab if not exist but refresh if same destination exist. And it works good so far.

Here is the code.

Link

<div id="container">
<a href="https://www.google.com" target="_blank">Google</a>
</div> 

JavaScript

<script>
    document.getElementById("container").onclick = function(test){
        if (test.target.tagName === "A")
            window.open(test.target.href, test.target.href);

        return false;
    }
</script>

My question is, How can I use same JavaScript on my form with similar effect? I mean if user submit my form. It will create a new tab when submitted if it not exist. But resubmit on same tab if the tab is exist. I tried to change JavaScript but can't make it work as normal link.

Here is my form

<form name='form1'  action='/newform.php' method='post' target='_blank'>
<input type='hidden' name='test' value=''>
<input type='image' src='../data/images/myimages.gif' width='100px' height='30px' alt=''>
</form>

How about using a session variable?
Also use ID's for javascript.

<?php 
  //Check if session has not been started.
  if (session_status() == PHP_SESSION_NONE)
  {
    session_start();
  }


//If it has not been set, set no default.
if (!ISSET($_SESSION['hasSubmit'])
{
    $_SESSION['hasSubmit'] = "no";
}

if ($_SESSION['hasSubmit'] == "no")
{
   //Execute code for no
  echo "
  <form name='form1'  id='firstSubmit' action='/newform.php' method='post' target='_blank'>
  <input type='hidden' name='test' value=''>
  <input type='image' src='../data/images/myimages.gif' width='100px' height='30px' alt=''>
  </form>";
}
else if($_SESSION['hasSubmit'] == "yes")
{
   //Execute code for yes
   //Edit your form properties here to your likes.
  echo "
  <form name='form1'  action='#' method='post' target='_newblank'>
  <input type='hidden' name='test' value=''>
  <input type='image' src='../data/images/myimages.gif' width='100px' height='30px' alt=''>
  </form>";
}
?>

in newform.php set:

$_SESSION['hasSubmit'] = 'yes'

now the javascript:

$('firstSubmit').click(function(){
location.reload;
});

this should do the trick

You might try something like this:

<script type='text/javascript'>
    function formSubmit() {
        window.open("about:blank","newWindow");
    }
</script>
<form name='form1'  action='/newform.php' method='post' onSubmit='formSubmit()' target='newWindow'>
  <input type='hidden' name='test' value=''>
  <input type='image' src='../data/images/myimages.gif' width='100px' height='30px' alt=''>
</form>

Basically, this will open new window when the form is submitted for the first time, and every other time form will submit to the already open window.

Keep in mind that window.open can be blocked by popup blockers and often is.

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