What is PHP equivalent for <jsp:forward> ???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amirssid
Forum Newbie
Posts: 3
Joined: Fri Feb 26, 2010 11:19 am

What is PHP equivalent for <jsp:forward> ???

Post by amirssid »

Hi Experts,

I am new to PHP. I just want to know, how can we advance from one page to another (without a form submit) from within PHP, like <jsp:forward> does in JSP? If it is possible in PHP, please also tell how we can send parameters to the new page, like <jsp:forward> send with <jsp:param> tags? the jsp example which I want to convert in PHP is as follow:

<jsp:forward page="user_db_login.jsp" flush="true">
<jsp:param name="username" value="<%= strName %>" />
<jsp:param name="password" value="<%= strPwd %>" />
<jsp:param name="DatabaseString" value="<%= strDbstr %>" />
</jsp:include>

An early response will be greatly appreciated.

Thanks in advance.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: What is PHP equivalent for <jsp:forward> ???

Post by AbraCadaver »

Code: Select all

header('Location: http://www.example.com/user_db_login.php');
And if you want to pass vars then you can append them to the URL as a query string.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
amirssid
Forum Newbie
Posts: 3
Joined: Fri Feb 26, 2010 11:19 am

Re: What is PHP equivalent for <jsp:forward> ???

Post by amirssid »

Thank you very much. But here is another question. If we append parameter values to the URL as query string, it will only work with pages which accept parameters with GET method.... what about if the target page is expecting parameters by POST method?

Thanks again.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: What is PHP equivalent for <jsp:forward> ???

Post by AbraCadaver »

The location header sends a header to the client browser telling it to redirect there. There is no header to redirect a browser and send post data. You can send post data from the server, but this does not redirect the browser. I would use: http_post_fields() or if you want to send a string of pre-encoded data: http_post_data().

To actually get the browser to post to a page you would have to use client side javascript.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: What is PHP equivalent for <jsp:forward> ???

Post by JNettles »

Which I believe JSP does. Languages with custom controls like these (also in ASPX pages) replace those custom tags of yours with client-side Javascript to generate the effect that they need. Look at the page source of a JSP or ASPX page and you'll see Javascript pages included in your HTML output.

You can do the same thing with PHP. Just develop a template parser that takes custom HTML tags and replace it with prebaked code and Javascript. Prado does this to great effect.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: What is PHP equivalent for <jsp:forward> ???

Post by AbraCadaver »

I was looking through some old code for this. Quick and dirty, needs cleanup:

Code: Select all

function http_post_redirect($url='', $data=array(), $doc=false) {
 
    $data = json_encode($data);
 
    if($doc) {
        echo "<html><head></head><body>";
    }
    echo "
    <script type='text/javascript'>
        var data = eval('(' + '$data' + ')');
        var jsForm = document.createElement('form');
        
        jsForm.method = 'post';
        jsForm.action = '$url';
        
        for (var name in data) {
            var jsInput = document.createElement('hidden');
            jsInput.setAttribute('name', name);
            jsInput.setAttribute('value', data[name]);
            jsForm.appendChild(jsInput);
        }
        document.body.appendChild(jsForm);
        jsForm.submit();
    </script>";
 
    if($doc) {
        echo "</body></html>";
    }
    exit;
}
Last edited by AbraCadaver on Mon Apr 12, 2010 1:04 pm, edited 1 time in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
amirssid
Forum Newbie
Posts: 3
Joined: Fri Feb 26, 2010 11:19 am

Re: What is PHP equivalent for <jsp:forward> ???

Post by amirssid »

Thank you very very much for the script. Its really nice.
Post Reply