Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
98
rated 0 times [  105] [ 7]  / answers: 1 / hits: 21151  / 12 Years ago, sat, november 17, 2012, 12:00:00

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 using header();, 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');
}
});
});

More From » php

 Answers
10

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');
}
});

[#81947] Thursday, November 15, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aubriea

Total Points: 641
Total Questions: 118
Total Answers: 101

Location: French Southern and Antarctic Lands
Member since Fri, Jan 6, 2023
1 Year ago
;