Page 1 of 1
What is PHP equivalent for <jsp:forward> ???
Posted: Fri Feb 26, 2010 11:37 am
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.
Re: What is PHP equivalent for <jsp:forward> ???
Posted: Fri Feb 26, 2010 11:42 am
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.
Re: What is PHP equivalent for <jsp:forward> ???
Posted: Fri Feb 26, 2010 11:50 am
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.
Re: What is PHP equivalent for <jsp:forward> ???
Posted: Fri Feb 26, 2010 12:04 pm
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.
Re: What is PHP equivalent for <jsp:forward> ???
Posted: Fri Feb 26, 2010 12:10 pm
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.
Re: What is PHP equivalent for <jsp:forward> ???
Posted: Fri Feb 26, 2010 1:04 pm
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;
}
Re: What is PHP equivalent for <jsp:forward> ???
Posted: Thu Mar 11, 2010 3:59 pm
by amirssid
Thank you very very much for the script. Its really nice.