Page 1 of 4

I had a revelation today.

Posted: Tue Jun 27, 2006 8:34 pm
by daedalus__
EDIT: I do not have a good title for this topic and if you have a suggestion, post it. Better yet, if you are an admin, change it.

Forever and ever, since I have been coding websites, I have had one problem that drives me insanely crazy.

The URLs of my website look like this:

http://www.camicus.net/?q=home
http://www.camicus.net/?q=weblog&a=search
http://www.camicus.net/?q=weblog&a=details&id=1

Stop! You are thinking, don't. Just listen.

Forever and ever, whenever I would be writing admin pages, I would POST the form. Now, you can't post to something sexy like a querystring.

You have to post to form_handler.php and then redirect back to a sexy querystring.

I hate that! I hate, hate, hate that! I hate it.

I figured it out today. It was beautiful. PHP Buddha came to me and told me how to right this horrible, horrible wrong.

I have a page, index.php

index.php inits a class named PageBuilder

every form posts to index.php?q=weblog&a=addentry or something other such thing

PageBuilder checks for POST data to index php and then checks the querystring and decideds what functions it needs to call.

Code: Select all

// Gangster pseudo-code

$pagebuilder->buildpage

// buildpage:

function buildpage
{
    if ($_POST)
    {
        // handle the form data
    }
    // call functions to output page
}
Now, I don't know if anyone has ever cared about this, much less this vehemently but I solved it! I frickin' figured it out and today, I love myself.

I hope this helps someone as stupid as me.

I'll never use header() or meta redirects again.

Posted: Tue Jun 27, 2006 8:41 pm
by daedalus__
Also, you may have a better way and I would like to know it!

Re: I had a revelation today.

Posted: Tue Jun 27, 2006 8:58 pm
by timvw
Daedalus- wrote: Forever and ever, whenever I would be writing admin pages, I would POST the form. Now, you can't post to something sexy like a querystring.
Why not?
Daedalus- wrote: You have to post to form_handler.php and then redirect back to a sexy querystring.
Usually i simply use action="#"... This way, users post to the URL they're currently visiting...
And each of my pages would then:

Code: Select all

if (isset($_POST)) {
  require('formhandler.php');
}

// do regular stuff

Posted: Tue Jun 27, 2006 9:09 pm
by daedalus__

Code: Select all

<form action="?q=projects&a=add" method="post">
    <input type="text" name="blah" />
</form>
Will not post anything. You have to give it a page name. Something to do with HTTP or some other such thing.

I suppose that the reason I have had to do this is because of the way I structure and write my websites. When I have a page with a form in it, if there is POST data it handles it, if not, it posts the form. Rather than having a user see hey/this/is/where/my/admin/pages.are, I like having them as a query string.

Posted: Tue Jun 27, 2006 9:16 pm
by phpCCore Brad
There is a function that will format URLs and forms to post to dynamic pages... I am trying to think what it is.... *goes to check out all functions*

Posted: Tue Jun 27, 2006 9:18 pm
by daedalus__
If I missed, and it exists, then I am going to tattoo RTFM on my forehead. I've asked like 20 people I know about this and they all do the same thing. I suppose we all suck equally though.

P.S. searching on google is like coming up with a topic name for me, i can hardly do it

Posted: Tue Jun 27, 2006 9:21 pm
by phpCCore Brad
I swear I found a function on php's function list that adds hidden elements to forms for that purpose and it adds variables to URLs to, but I can't for the life of my rememeber what it is.

Posted: Tue Jun 27, 2006 9:25 pm
by phpCCore Brad
I found what i was talking about. It uses output buffering though.

output_add_rewrite_var()

Posted: Tue Jun 27, 2006 9:32 pm
by phpCCore Brad
Oh and as was said above posting to a blank anchor will keep it to the right page.... If that was the problem you are seeing.

Example:

Code: Select all

<?PHP
	if (isset($_REQUEST)) {
		print_r($_REQUEST);
	}
	
	?>
		<form method="post" action="#?testvar=test">
			<input type="submit" name="hello" value="test">
		</form>
?>

Posted: Tue Jun 27, 2006 9:46 pm
by shoebappa
Yeah, wow, what browser are you using? I tested this code in firefox and ie...

Code: Select all

<?php

	if (count($_GET) > 0 || count($_POST) > 0) {
		echo "<h1>GET</h1><pre>";
		var_dump($_GET);
		echo "</pre><h1>POST</h1><pre>";
		var_dump($_POST);
		echo "</pre>";
	}

?>
<html>
<body>
	<form action="?testget1=1&testget2=2" method="post">
		<input type="hidden" name="testpost1" value="1" />
		<input type="hidden" name="testpost2" value="2" />
		<input type="submit" value="Submit" />
	</form>
</body>
</html>
with output:

Code: Select all

GET
array(2) {
  ["testget1"]=>
  string(1) "1"
  ["testget2"]=>
  string(1) "2"
}

POST
array(2) {
  ["testpost1"]=>
  string(1) "1"
  ["testpost2"]=>
  string(1) "2"
}
There's also the handy $PHP_SELF variable

Code: Select all

<html>
<body>
	<form action="<?php echo $PHP_SELF; ?>?testget1=1&testget2=2" method="post">
		<input type="hidden" name="testpost1" value="1" />
		<input type="hidden" name="testpost2" value="2" />
		<input type="submit" value="Submit" />
	</form>
</body>
</html>
but you can definately send get and post data in the same request...

Posted: Tue Jun 27, 2006 9:52 pm
by phpCCore Brad
Maybe we are just not understanding what he meant... It's probably a miscommunication. I guess I just always use one script to handle a lot of things, so it never occurs to me to make another script for handling.

Posted: Tue Jun 27, 2006 9:57 pm
by shoebappa
Some header action

With the $PHP_SELF:

Code: Select all

POST /getpost.php?testget1=1&testget2=2 HTTP/1.1
Host: localhost
*DELETED CRAP*
Referer: http://localhost/getpost.php
Content-Type: application/x-www-form-urlencoded
Content-Length: 23
testpost1=1&testpost2=2
Just ? no anchor:

Code: Select all

POST /getpost.php?testget1=1&testget2=2 HTTP/1.1
Host: localhost
*DELETED CRAP*
Referer: http://localhost/getpost.php
Content-Type: application/x-www-form-urlencoded
Content-Length: 23
testpost1=1&testpost2=2
With anchor:

Code: Select all

POST /getpost.php HTTP/1.1
Host: localhost
*DELETED CRAP*
Referer: http://localhost/getpost.php
Content-Type: application/x-www-form-urlencoded
Content-Length: 23
testpost1=1&testpost2=2
That was in firefox, I knew I didn't like that idea when I first saw it. Hell it doesn't even work in IE either!

I repeat, don't use action="#?getdata"!!! Should just use action="?getdata" or action="<?php echo $PHP_SELF; ?>?getdata"

If someone has a browser that behaves differently I'd like to know about it... Obviously the headers above illustrate that action="<?php echo $PHP_SELF; ?>?getdata" is the way to go because any other method relies on the client adding the current page to the request.

Posted: Tue Jun 27, 2006 10:01 pm
by phpCCore Brad
EH I don't really use the anchor I do ? but I figured there was something someone else knew that I didn't ;)


I am pretty sure he was talking about an Auth. Such as a user is viewing a dynamic link but without logging in they can't view the dynamic link so the login form shows up. Then after you log in the dynamic variables are gone. Personally I use sessions for this, but for all I know that could be a problem. But it works for me...

Posted: Tue Jun 27, 2006 10:11 pm
by shoebappa
Right, just for giggles I ran this:

Code: Select all

<?php

	if ($_GET || $_POST) {
		echo "<h1>GET</h1><pre>";
		var_dump($_GET);
		echo "</pre><h1>POST</h1><pre>";
		var_dump($_POST);
		echo "</pre>";
	}

?>
<html>
<body>
	<form action="#?testget1=1&testget2=2" method="post">
		<input type="hidden" name="testpost1" value="1" />
		<input type="hidden" name="testpost2" value="2" />
		<input type="submit" value="Submit" />
	</form>
	<img height="700" width="1" />
	<a name="?testget1=1&testget2=2" />
</body>
</html>
Which posts and does not send get, and then anchors down to an anchor named "?testget1=1&testget2=2".

I did learn you can use if($_POST) from this thread, I'd been using count($_POST) > 0 which works but sucks. isset() doesn't work for whatever reason.

Posted: Tue Jun 27, 2006 10:16 pm
by Burrito
if the $_GET vars are already set when the pages loads just do this:

Code: Select all

<form method="post">
it will retain the URL var information and just post to itself.