I am trying to get a HTML/PHP form to submit properly. Some details:
base url = http://localhost/directory
page = page/add
complete address = http://localhost/directory/page/add
Using htaccess to rewrite urls so http://localhost/directory/page/add is actually http://localhost/directory/index.php?q=page/add
My HTML POST action is “page/add” so that the front controller knows which function to fire to sanitize and submit the data (it acts as a ‘form id’).
The page loads fine at http://localhost/directory/page/add but when I click on the submit button, the URL gets mangled to page/page/add. And every time I press “submit” I get another “page” added to the url. So 5 clicks will get “page/page/page/page/page/page/add”
I can’t seem to find why I am getting that “extra” “page”.
The actual PHP error (page/page/add doesn’t exist in $routes since it isn’t a valid route):
Notice: Undefined index: page/page/add in C:\xampp\htdocs\script\includes\common.inc on line 92
Here is the function at line 92:
function route_path($path = NULL) {
$routes = get_routes(); //Returns array: approved "urls => function callbacks"
if($path === NULL) {
$path = get_path(); //Returns $_GET['q'] with trim and strip_tags
}
$function = $routes[$path]; <<<<<----This is LINE 92
if(isset($function)) {
$form_name = str_replace('/', '_', $path); // page/add = function page_add()
}
if(function_exists($function)) {
call_user_func($function, $form_name);
}
else {
//TODO: Redirect to Login screen.
}
}
The basic HTML is:
<form action="page/add" method="post" />
//Form elements
<input type="submit" value="Submit" />
</form>
Thanks for the help.
UPDATE: What I did was add the <base> tag to my HTML templates. This allows me to keep the action as page/add (since it is also a route in my simple router/dispatcher).
By using a relative path, you’re telling the form to submit at the existing path plus your action. So if you are at http://example.com/page/add, the form uses
http://example.com/page/as a base and adds the actionpage/addresulting in a POST tohttp://example.com/page/page/add.You can still use a relative path, just change the action accordingly: