Reputation: 5353
I have a template file (page-order.php) and a page corresponding to it. I've tried 3 things:
In each case when I submit the form I always get redirected to index.php. What am I doing wrong? I tried to change method also. And I don't use nice urls right now, instead ?page_id.
This is $_SERVER array displayed in index.php after redirection
Array
(
[SERVER_SOFTWARE] => Apache/2.4.9 (Ubuntu)
[REQUEST_URI] => /354
[REDIRECT_STATUS] => 200
[HTTP_HOST] => tree
[HTTP_CONNECTION] => keep-alive
[CONTENT_LENGTH] => 41
[HTTP_CACHE_CONTROL] => max-age=0
[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
[HTTP_ORIGIN] => http://tree
[HTTP_USER_AGENT] => Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36
[CONTENT_TYPE] => application/x-www-form-urlencoded
[HTTP_REFERER] => http://tree/354/
[HTTP_ACCEPT_ENCODING] => gzip,deflate,sdch
[HTTP_ACCEPT_LANGUAGE] => ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4,fr;q=0.2,zh-CN;q=0.2,zh;q=0.2,it;q=0.2
[HTTP_COOKIE] => wp_psr=79c2feaa8e0f6c89cf4cc2cc26661dbc; __atuvc=1%7C19; mc4wp_email=fdsf%40sdf.ru; wp-settings-1=editor%3Dhtml%26ed_size%3D533%26hidetb%3D1%26libraryContent%3Dbrowse%26urlbutton%3Dnone%26align%3Dleft%26dfw_width%3D822%26mfold%3Do; wp-settings-time-1=1400661809; wordpress_test_cookie=WP+Cookie+check; wordpress_logged_in_5880cb28233a06f6781467ba3412f6ff=admin%7C1400855925%7C0524345be09cf1aa7cafa34b27f8cdcc
[PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[SERVER_SIGNATURE] =>
Apache/2.4.9 (Ubuntu) Server at tree Port 80
[SERVER_NAME] => tree
[SERVER_ADDR] => 127.0.0.1
[SERVER_PORT] => 80
[REMOTE_ADDR] => 127.0.0.1
[DOCUMENT_ROOT] => /var/www/tree
[REQUEST_SCHEME] => http
[CONTEXT_PREFIX] =>
[CONTEXT_DOCUMENT_ROOT] => /var/www/tree
[SERVER_ADMIN] => [no address given]
[SCRIPT_FILENAME] => /var/www/tree/index.php
[REMOTE_PORT] => 40802
[REDIRECT_URL] => /354
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.1
[REQUEST_METHOD] => POST
[QUERY_STRING] =>
[SCRIPT_NAME] => /index.php
[PHP_SELF] => /index.php
[REQUEST_TIME_FLOAT] => 1400697038.09
[REQUEST_TIME] => 1400697038
)
Upvotes: 0
Views: 202
Reputation: 5353
I absolutely have no idea why it happens but when I changed
<p><input type="text" name="name" placeholder="Your name" /></p>
to
<p><input type="text" name="username" placeholder="Your name" /></p>
it works fine with form action=""
Upvotes: 0
Reputation: 4350
You are correct that you are hitting index.php but the reason is a little more complex. Basically the index file acts as a front controller for a Wordpress install. Whenever you navigate to any page, post, archive, whatever, on Wordpress you are hitting the index.php file first. That file then goes through a process of checking the rewrite rules and serves you a pretty URL with the correct slug using mod_rewrite. So technically you are never actually hitting any other file than index.php - all of the templates are loaded into that file.
Since you are not using permalinks, your URL for the form action needs to just be the page ID in the query string. You were almost there, but try this instead:
<form action="/?page_id=354" method="post">
If you ever decide to switch to pretty URLs that form action will still work correctly. However, I would recommend against hard-coding the ID into the form action. Usually an empty form action will suffice when you want to use the current page as a handler, but since you are saying that wasn't working you could always just use the current page ID in the form action like this:
<?php global $post; ?>
<form action="/?page_id=<?php echo $post->ID ?>" method="post">
Again, using an empty action should do the same thing, so it might be wise to figure out why that isn't working in your case. Try checking the PHP error logs, or maybe run a validator on your HTML.
Hope this helps!
Upvotes: 1