Any idea why submitted form data loads the search.php page instead of submitting form?
URL: http://domain/contact-us/
On form submission it redirects to: http://domain/?s=&wpforms%5Bfields%5D%5B0%5D%5Bfirst%5D=iuiou&wpforms%5Bfields%5D%5B0%5D%5Blast%5D=uoiu&wpforms%5Bfields%5D%5B1%5D=amigoow%40yahoo.com&wpforms%5Bfields%5D%5B6%5D=General+Feedback&wpforms%5Bfields%5D%5B3%5D=7987&wpforms%5Bfields%5D%5B2%5D=ou&wpforms%5Bhp%5D=&wpforms%5Bid%5D=87632&wpforms%5Bnonce%5D=df749058ef&_wp_http_referer=%2Fcontact-us%2F&wpforms%5Bsubmit%5D=wpforms-submit
I have changed the plugin from ContactForm7 to WPForms but it turns out it's something not related to plugin.
Well your form opening tag looks like this:
<form method="get" action="http://newdev.propakistani.pk" role="search">
So its doing exactly as per this tag. You need to amend action to the URL of the php file that processes the form.
Related
First page has form using method POST with action to the offer page.
<form id="value-form" method="POST" action="/offer/">
When user submits form on the first page, I want on the offer page class="add-info" to be added to the <form so then it fires up the jQuery script
<form id="formOffer" class="add-info">
Jquery
if (formOffer.hasClass('add-info')) {
launchrocket();
}
I can't figure out class tag can be added via PHP to the new form on the process page.
I want to know different between <form action="#" method="post"> and <form action="name of file" method="post">
I am always using # but don't know disadvantages.
Can you explain why I should use # or file name?
Thanks
form action = file name
It is used to send a request on the other page(i.e your file name) containing your form fields(inputs) with methods like GET and POST.
example my HTML page is having a form then and my PHP page is having all the backend code. Whatever I need to do with form inputs. I will give the file name of my PHP page in action. the action attribute of the form is used to send the form request to the destination we want to with methods like the POST and GET. If you do not want to send a request to another Page and want it to your default page. You can leave action ='' attribute of the form empty as I did.
An action of # indicates that the form stays on the same page, simply suffixing the URL with a #. A similar use occurs in anchors. Link for example, will stay on the same page.
Thus, the form is submitted to the same page, which then processes the data etc
The content of action allows you to know where you will put the code that will process the request.
If you put the name of a file it, then his file will process the request.
For example: you have your form on the index.php page and you want to put the PHP code of the form in a process.php file. You will put process.php in action (action="process.php").
If you do not put anything it is like sending the content of the request to the same file (index.php).
Hello I am very new to WordPress on my requirement I have created a couple of php files in wordpress theme. Where detailsform.php consists
<form method="post" name="details" action="customerdetails.php">
where after clicking submit button the form has to redirect to customerdetails.php in php it is working fine but in wordpress it is giving 404 error(page not found) I kept all new php files in the existing theme folder.
Please suggest me it is killing my time.
As wordpress has its very own specific way to handle ajax calls, wordpress has it too for post http request by form submitting. In the codex on wordpress there is more details about that.
In a brief explanation, using your functions.php file on your wordpress theme:
The first thing you need to do is set in your action attribute from your form tag, point the following url:
<form action="http://www.example.com/wp-admin/admin-post.php" method="post">
<input type="hidden" name="action" value="my_handler_function">
</form>
Once pointed, you need to add a hidden input with the name attribute action and a value attribute specifying the name of your action. Then, you need to build your handler function on your functions.php file. This is where your going to write the code you need for treat the data you will received by the global $_POST or $_REQUEST variable.
function my_handler_function() {
var_dump($_REQUEST);
die();
//request handlers should die() when they complete their task
}
Next step is to bound this function to your form by using the action hook admin_post_nopriv_ or admin_post_. The difference between these, is where your form is placed. If your form is for a custom functionality for the admin of wordpress then is private and you use admin_post hook action. If this form is part of your public content then use the admin_post_nopriv_ like in the following example:
add_action( 'admin_post_nopriv_my_handler_function', 'my_handler_function' );
As it is show in the code example above, you need to call the add_action function. In the first parameter, It is needed to pass the action hook provide by wordpress combined with the action value specify in the hidden input named action in the form, like admin_post_nopriv_$action_value. In the second parameter, you need to placed the function name you build on you functions.php file. Both are mandatory.
For matters of conventions, generally, the name of the function handler is set it as same as the value of the action input to avoid misunderstandings and gain more readability.
Onced everything is put it all together, all you have to do is test your code.
Happy coding!!
PD: If you want to clarify about this procedure of wordpress, please take a look in the wp-admin/admin-post.php file, but don't even dare to modify it.
// Put your file customerdetails.php in current theme and use following path in action:-
<form method="post" name="details" action="<?php echo get_template_directory_uri() ?>/customerdetails.php">
404 error means the action is wrong for form.
<form method="post" name="details" action="customerdetails.php">
^ ^
Correct the action to exact path of customerdetails.php
Issue must be with path (form redirection URL on submission), try with full url.
Can you share the URL at which you have put your form ??
edit your php file with template.
Ex. http://www.wpbeginner.com/wp-themes/how-to-create-a-custom-page-in-wordpress/
<?php
/*
Template Name: Customer Details Page
*/
get_header();
?>
// your php file as it is.
<?php get_footer(); ?>
now create new page with "customerdetails" name in wp admin & select "Customer Details Page" in right side column & save.
now your form action path will be as below
<form method="post" name="details" action="<?php echo get_site_url(); ?>/customerdetails">
Now your form is getting submitted & customerdetails page also receive post data.
I am making an e-commerce website where I have lots of products. If a user goes to any product items page and submits any form there then they should come on the same page.
So, how to come on the same page?
On the formular target page set:
header('Location: http://www.example.com/same_page');
Leave action attribute of form blank. Like so:
<form action="" method="post">
Or
<form action="#" method="post">
On your opening form tag add
action="submit.php"
then once it goes to that page when the submit button is hit add this
to the bottom of that php page:
header("Location: success.html OR success.php");
If you want to submit various forms on same page and then go back to the page where the form is submitted, you must also send the form URL of the page where it was sent, preferably in a hidden element. And after processing form redirect to URL stored in hidden.
You can use this :
header('Location: filename.php);
If you get any $_POST errors put it in a condition: if(isset[$_POST])
Thank You All. I Got My Answer
$_SERVER['REQUEST_URI']; will give the current URL with query strings.
Like my page is 'products.php?Product=20'
echo $_SERVER['REQUEST_URI']; =>/products.php?Product=20
So, we can directly use this in header location.
I have form and some fields and I want send these fields to the next page via done.php using action="#main_body".
What are the differences between these two forms?
<form id="formElem" name="formElem" action="/ifs/form/index.php" method="post">
<form id="formElem " class="ifs" method="post" action="#main_body">
The complete action of the form is the URL of the page containing the form at the time of loading the form + the hashtag. So submitting the form will load the same page, but with a ahashtag (anchor) of #man_body. This is a side effect of action attributes being realtive if not definitly given as absolute.
Please be aware, that it is browser-dependant and header-dependant wether the page will actually reload or just scroll.
in the first case you send the values of your inputs to a specific page called done.php. In the second way you're calling the same page in which you have your form (plus an hashtag)
In the second link you are calling the same page with a hashtag of "main_body". it will work something like a 'TOP' link provided in lengthy pages which scrolled back to top of the page.
a difference is here the page will scroll(or reload) to "main_body" when you submit the form.