i am looking in run a
<form action=bob.php?action=$test>
how would i phrase my php file so that i can do say a large php of all my actions
$test1
$test2
etc...
i am looking in run a
<form action=bob.php?action=$test>
how would i phrase my php file so that i can do say a large php of all my actions
$test1
$test2
etc...
I'm not sure I understand your post so let me echo back what I think you're saying and let me know if I'm off...
You're looking to control the action URL variable in your form and wish to have 1 PHP page handle all the actions. But you would like to know the best way to have PHP handle this.
If I got that correctly, there's about 3 ways to do this. Of which I've done all 3. And I can share with you the 3 and which one works the best for me.
yea one action processor and loads of forms to complete the actions to
Thanks
Lets start out and assume you have something like this at the top of your page:
$action = $_GET['action'];
Now we have $action equal to the action you wish to perform.
You could use a collection of if statements to test $action and then do something. Disadvantage: pain in the butt to read and update if you have a lot of these. For like 1 test, it works great. For 3 or more, I'd use one of the other methods.
Use switch. I had been using this for a year and it worked. But if you have a lot of tests to perform, the switch code gets ugly pretty fast (not as fast as the #1 approach, but almost as bad). The cool part about switch is you can use the default statement. Kinda like this:
switch($action)
{
case 'read':
doread();
break;
default:
dodefault();
break;
} // end switch($action)
So the switch provides a hint of security if someone starts messing with your action variable. But as you see, for 1 test and a default, we're up to 9 lines of code and its only going to get worse.
I also ran into an article about how you should avoid using if and switch statements for a purpose similar to this. The idea was it gets messy and difficult to support.
$action = $_GET['action'];
$function = 'action_' . $action; // creates a string: action_ and then your action data
// check if this function exists:
if(function_exists($function))
$function($_GET, $_POST); // I like to pass along get and post for form handling
else
action_Default($_GET, $_POST); // function doesn't exist so call the default (similar to the switch default as seen above)
// functions:
function action_Default($get, $post)
{
// show your default page/form
} // end
function action_doread($get, $post)
{
// another function that does something
} // end
The cool part about this approach is if you need to add functionality, you just add another function. You don't have to fiddle with if statements or case statements. Plus, if you need to troubleshoot, you just need to know the name of $action and then look up its function.
I put "action" before the actual $action variable because I want to make sure the user can not tweak the $action variable to point to PHP's collection of functions. You can use whatever you like instead of "action" (I normally use "Do_" which works well for me).
Then its just a matter of plugging in the code you want that function to do.
so
$_get('THISBIT')
would be <form action=bob.php?action=THISBIT method=post>
yea?
??
isnt it easier than this i remember something like this b4 which was simpler to code.?
cant remember how obviously .. HE EH
Thanks
The code you have will work. I don't typically pull the form action from the URL, but that is an option...
what are /is the recomend ways of submiting to a form.
forms to seperate processor pages
forms with included processors
or is there more options which i am not aware of?
what are /is the recomend ways of submiting to a form.
my opinion is that use one template and include in to it if actions associates with "master"-template... ex.
//index.php
//
include($template); // template ="registration_form.php"
//registration_form.php
if(!isset($action)) {
include("form.php");
} else {
// add,modify,delete
include("action_".$action.".php");
}
no you can process the page by using URL
index.php?template=registration_form.php
and after form you submit this to:
index.php?template=registration_form.php .... and send action parameter via form..
Originally posted by keithjasper
what are /is the recomend ways of submiting to a form.
I use a template system to display the pages mixed with the #3 option seen in my previous posts. With this in mind, when I have an action that displays a form, I'll have the function responsible for displaying the form set the form's action with a variable to be used with the template. In your case, it simply would be a variable of your choice and a matter of echoing it out.
Instead of using this:
<form action="myscript.php?foo=blah&bar=blahblah">
you can use this:
<form action="myscript.php">
<input type="hidden" name="foo" value="blah">
<input type="hidden" name="bar" value="blahblah">
Browsers do seem to properly append form variables to the action URL variables, but I'm not sure if that is required by the specification. Using hidden fields seems to be the more correct way to do it.
Originally posted by swr
Instead of using this:
<form action="myscript.php?foo=blah&bar=blahblah">
you can use this:
<form action="myscript.php">
<input type="hidden" name="foo" value="blah">
<input type="hidden" name="bar" value="blahblah">
...
Actually, doing action="myscript.php?Action=myaction" will not work if the form method is "get". And for "get" forms, you do need to use hidden variables. The post method doesn't seem to mind if you have data up on the action="" url.