I have a post html form, and the action is index.php, which is the websites main page. I never actually have index.php in the address bar, since links to "/" go to it, and even if I did, it would be website.com/home(/), since I use rewrite rules. So basically, the user should never see index.php. However, the form submit doesn't work if the action isn't spefically index.php, "/" doesn't work, even though that resolves to index.php. Any ideas on how to get around this?
Don't specify the action. If the post is to the page they're on, the default action of the form is to post to that.
Related
I'm trying to understand how the redirect_to hidden form field works (on Wordpress), as in
<input type="hidden" name="redirect_to" value="http://www.mydomain.com/thank-you" \>
Will it redirect to that value no matter what, or do I need to write some code including wp_redirect() code somewhere?
The reason I'm asking is: I have two forms, one with a redirect_to value to a page on my own site and one with a redirect_to pointing to an external domain. While the first works fine, the latter does not. It always takes me back to my front page. Thus, I'm trying to debug that problem.
Thank you for your help!
Setting up redirect in hidden field does not redirect you anywhere . It just saves the value in that hidden field.
The Form redirects to the url you specify inside action attribute
i.e:
<form action="http://example.com">
If it's left empty or not written then it will redirect you to the same page by default , that's the reason that both of your forms redirect you to the same page.
If you want to redirect your other form to different url , specify it in your form action attribute.
EDIT :
Yes , If you use wp_safe_redirect() then it will redirect you back to your site , in case of providing external host as action attribute.
Ok thanks to the great people on this site I was able to create a php file that directed to a form dependent variable url. What I would like to try and set up is to either mask the directed url so the variable url is not shown or at least have it open to an iframe so the address bar is at least not present.
Some things I have tried:
On the Form page created that has the action thats directed to a my php file I created an iFrame and set the target to the iFrames name------- iFrame shows up on the form, but upon submission full page loads, not in iFrame
Created an iFrame.html doc had the Form page action set to the html doc, then set the iFrame source in the doc to the php file with the action------same thing loads in the same window and no iFrame ever shows
Tried to insert iFrame in the php doc but got parsing errors
Any help would be appreciated even if its just a point in the right direction.
If you want to simply hide the parameters in he URL (for example to not allow users to easily edit them) you can simply change the form to use POST as opposed to GET: <form action="serverside.php" method="post">. On the server side you will also have to change $_GET to $_POST or $_REQUEST (the last one combines the get and post variables with post having higher preference).
As the topic - How do I rewrite a POST request from a form to a user-friendly URL with htaccess?
The scenario:
I have a webpage that uses a search-form. When I submit that form using method="post" it works flawlessly. BUT I don't get any text in the browser address-bar (of course), but that's exactly what I want! And that by using method="POST", NOT method="GET"!
Let's say I search for "banana". The PHP script translates the POST-request and the script shows all receipts with the word banana in it. But the URL then of course shows something like http://www.example.com/search/ (yes I use mod_rewrite for that). I want the URL to look like http://www.example.com/search/banana/.
The original request from the server looks like ....xample.com/index.php?p=search and the post is of course hidden and would otherwise be ....xample.com/index.php?p=search&q=banana.
I'm not new to mod_rewrite rules and conditions but I just can't get it to work...
Thanks in advance!
Quoted from: Apache mod_rewrite question
You can't use POST data for mod_rewrite. This is because the POST data isn't in the HEADER of the http request, it's in the BODY.
My suggestion would be that you perform an action on the posting page that adds the prefix to the URL, which would mean you don't even need to rewrite.
If I understand correctly: you want the browser to POST to http://www.example.com/search/banana/ rather than to http://www.example.com/search/, where banana is one of the form's input-fields. (Is that right?) That's not really a mod_rewrite issue, then, so much as an HTML issue: it happens on the client side. And since HTML doesn't support this, it's actually a JavaScript issue. You'd have to write either an on-submit-handler for your form, or an on-change-handler for the input field. In either case, the handler will have to modify the form's action based on the contents of the input-field.
(Note: The above is also true, to a certain extent, for GET requests. The major difference is that with a GET request, you could circumvent this by HTTP-redirecting from http://www.example.com/search/?q=banana to http://www.example.com/search/banana/; so the browser will initially GET http://www.example.com/search/?q=banana, and then GET http://www.example.com/search/banana/. But according to the HTTP spec, you can't redirect a POST request in the same way.)
You will need javascript.
I answered a similar question before. When someone click on the submit button you will need to change the action attribute at the form tag.
So I changed the structure of my site around some and such and I guess I broke the script some how.
When the user fills out a form correctly, they should be directed to the appropriate page. Instead it just sits there. However, if I test the form directly from form.php it works.
Here is some info on the form.
form.php is the actual form. submit.php does error checking, inserting into database, email message, redirect. script.js does ajax to make the form more user friendly.
Remember, this all works if I go directly to form.php and fill out the form. However, going to register.php which simply has added to it the header and footer and the body for form.php it does not work. What could be wrong?
Thanks for your time.
You propably included the jQuery framework twice, that causes much problems.
I have a <?php print $search_box; ?> in my page.tpl.php page. On pages that exist, the search works, but on 404 pages, it does not.
I saw some bugs/patches threads over at drupal.org for D6.15, but none of them seem to work according to the thread and they weren't really relevant to D5.x
I have a theory that the because the <?php print $search_box; ?> creates a form with an action to itself(a non-existant page), it'll get the 404.
Has anyone run up against this? If so, how did you fix it?
One theory I has was to somehow tap into the form and always make the action="/" (front page) which would always exist.
If this is a good idea, how does one go about tapping into the FormAPI and overwriting the action? Is it a preprocess function?
In a form alter you can use drupal_get_headers() to check if the page being displayed is a 404.
If it is a 404, you can in your form alter set the $form['#action'] attribute in your search form to fx the front page or any other page that you would like to redirect the user to.
I haven't tested this, but it should work.