mrjimoy_05
mrjimoy_05

Reputation: 3568

Opening a new tab on PHP

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

Upvotes: 2

Views: 21308

Answers (3)

fedmich
fedmich

Reputation: 5381

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

Upvotes: 3

Mohit Mehta
Mohit Mehta

Reputation: 1285

<?php

$gomenu = $_POST['shortcut'];

if(strpos($gomenu, "/") === 0) 
{
    echo "<script type='text/javascript'>window.open('newTab.php', '_blank')</script>";
} 
else 
{
    echo "<script type='text/javascript'>window.open('newTab.php', '_parent')</script>";
}

?>

Upvotes: 0

Brad
Brad

Reputation: 163272

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.

Upvotes: 3

Related Questions