Opening a new tab on PHP
Solution 1
I think you could do it this way, put the target blank by default.
<form id="frm" action="process.php" method="post" target="_blank">
</form>
then when submitting on the form submit() and modify & navigate away if you need to. you could just use javascript before submitting, change the action or target attributes
http://jsfiddle.net/fedmich/9kpMU
$('form#frm').submit(function(){
var open_new_tab = false;
var v = $('#shortcut').val();
if(v.match('/') ){
open_new_tab = true;
}
if ( open_new_tab ) {
alert('opening to new tab');
$('form#frm').attr('target', '_blank');
$('form#frm').attr('action', 'http://www.youtube.com');
}
else{
alert('opening to self_page');
$('form#frm').attr('target', '_self');
$('form#frm').attr('action', 'http://www.yahoo.com');
}
});
Solution 2
Submit your form via AJAX, send the new URL in the response from PHP, and use JavaScript to open your new window, if your PHP script specifies that it should.
mrjimoy_05
I am a Objective-C, Swift, Java, ABAP, VB.Net, Visual C#, PHP developer utilizing PostgreSQL, MongoDB, SQL Server, MySQL, DB2, Oracle :)
Updated on November 17, 2020Comments
-
mrjimoy_05 over 3 years
I have a simple form:
<form id="frm" action="process.php" method="post"> <input type="text" id="shortcut" name="shortcut" /> </form>
In
process.php
:$gomenu = $_POST['shortcut']; if(strpos($gomenu, "/") === 0) { //if $gomenu contains `/`, then it should open in new tab header('Location: newTab.php'); //--> How to open this into a new tab? } else { header('Location: somePage.php'); //--> Redirect to somePage.php on the same tab }
When the
shortcut
contains value of/
, then it should be redirect to a new tab, else the same tab. How to do that? I know it's impossible to do that by usingheader();
, but I have no idea how to do that. Any ideas? Thanks.PS: The form is intended to fill by a menu code, then it has to redirect the page based on the menu code filled on
shortcut
(as in SAP). But if it contains a specific prefix, a different way should be implemented.
UPDATE
Above the form, I've added this script:
<script> $("#frm").ajaxForm({ url: 'process.php', type: 'post' }); </script>
And on the
process.php
:$gomenu = $_POST['shortcut']; if(strpos($gomenu, "/") === 0) { print "<script> window.open('newTab.php', '_newtab'); </script>"; } else { header('Location: somePage.php'); }
But then it opened in a new pop-up window. How to open it on the newtab?
ANSWER (@fedmich's answer)
$(document).ready(function () { $('frm#frm').submit(function(){ var open_new_tab = false; var v = $('#shortcut').val(); if(v.match('/') ){ open_new_tab = true; } if ( open_new_tab ) { $('frm#frm').attr('target', '_blank'); } else { $('frm#frm').attr('target', '_self'); } }); });