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.
Related
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).
I am trying to create a template commenting system using Dreamweaver for a site. I have the form setup to submit the webpage and corresponding text to a mysql db. the webpage value is a hidden form field.
The form submits to the db okay but I want to create a repeating view for the comments. How do I reference the hidden form field so I can use it where "WHERE webpage=""" is called?
UPDATE: By repeating view I mean:
<?php do { ?>
<p><?php echo $row_InsertRecords['text']; ?></p>
<?php } while ($row_InsertRecords = mysql_fetch_assoc($InsertRecords)); ?>
My problem is I need to make partial edits to the PHP for that template so I can retrieve comments specific to the child page but Dreamweaver won't let me. It either propogates ALL of the PHP or none of it.
For a dynamic commenting system that uses the templates in Dreamweaver, you can use the following code in your dwt file:
$fname=basename($_SERVER['PHP_SELF']);
$query_ViewRecords = "SELECT * FROM commentsDB WHERE id='".$fname."'";
id can match a hidden value in your comment form defined as such:
<input type="hidden" name="IDField" value=<?php echo "\"$fname\""; ?>/>
The code above was part of code generated by Dreamweaver's DB functions that were later edited by me to add the WHERE clause. This way you can generate HTML that matched each child page when it is created by the template. Make sure codeOutsideHTMLIsLocked parameter is set to true for these changes to propagate to the child pages.
I use of CodeIgniter. I have a form that saved in formregister.php.
I've written in first of the form formregister.php:
echo form_open('main/sabtm');
The 'main' is a controller and 'sabtm' is a function in controller.
Also, I set $this->load->helper('form'); in controller.
Then, when I click on button in the form, the url in browser changed to localhost/emdad/index.php/formregister, where in source code of my page has set <form action="localhost/emdad/index.php/main/sabtm" method="post" accept-charset="utf-8">
what is problem?
This sounds a strange problem. If I have form action "localhost/emdad/index.php/main/sabtm" in my source code and by submitting the form it changes to "localhost/emdad/index.php/formregister". I think following points can be checked for such an issue:
Please check if you have controller "formregister.php" and somewhere in form/web page you are changing the form action via some client side script like javascript.
Please check if you have properly closed the form (form_close()).
Please check the method sabtm() if it has some validations and it redirects to respective page upon success/failure of the validation.
Alternatively please post full code of controller & view.
the file App.php of the folder Config change
public $indexPage = 'index.php';
to
public $indexPage = '';
I have a module which had instaled a CMS page with some default URL and some text with form. On this form I have fields and submit. I validate my fields using ajax(send POST in controller of my module). If validation is OK, I redirect to the same page with "succes message".
The problem is, that the default URL of this CMS page in which I make redirect after succes submit can be changed in BO - that's why I cannot just $this->_redirect('default_URL') in my controller, because this url can be changed.
What should I do?
EDIT : Solution: use $this->_redirectReferer() in my controller after success validation
Try to put "back URL" to your form as a hidden field or you can use $this->_redirectReferer() to redirect back to CMS page.
See http://docs.magentocommerce.com/Mage_Core/Mage_Core_Controller_Varien_Action.html#method_redirectReferer
If you want to redirect to some certain CMS page (not the one where form is placed) you can do this:
Add dropdown to system configuration to be able to select "Success page" (much better than hardcode cms page ID)
Redirect to this page in your controller
Code:
$pageId = Mage::getStoreConfig('mymodule/config/success_page');
$page = Mage::getModel('cms/page')->load($pageId);
$this->_redirect($page->getUrlKey());
You can get a store config by using the getStoreConfig method, something like
Mage::getStoreConfig('helloworld_options/messages/hello_message');
See Alan's blog, about detailed instructions how to create custom config values (which you might already have) and to access them.
Use this code for redirect to referer
<?php Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getRequest()->getRequestUri());?>
For example in custom login form:
<form method="post" action="<?php echo Mage::helper('customer')->getLoginPostUrl() ?>">
<?php Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getRequest()->getRequestUri());?>
...
...
Case: I have a form in the page which enables users to add certain values in a simple text file. Now I want to redirect to the same page after values have been added successfully.
The read/write code all works great, I even put redirection code but this would display warning that header information is already sent.
Headers I tried:
header("Location: some_url_here_in_same_site");
wp_redirect("some_url_here_in_same_site");
My Form code:
if(isset($_POST['txtName'])}
// form validation code here
if(successful)
wp_redirect("some_url_here_in_same_site");
}
PROBLEM:
how can we do redirection in wordpress after form submission in our plugin?
ob_start wonb't work, so don't suggest me this either
Ok I managed to do it using Hooks. I did redirection using the code below. Note that the form action is set to admin-post.php
<form action="admin-post.php" name="frmHardware" id="frmHardware" method="post">
<!-- form elements -->
<!-- Essential field for hook -->
<input type="hidden" name="action" value="save_hw" />
</form>
Then in my plugin's main file, I added the following:
add_action('admin_init', 'RAGLD_dashboard_hardware_init' ); // action hook add/remove hardware
Where, the function definition is as follows:
Also note that the first argument is derived from admin_post which is a reserved word is combined with the action hidden field in the form above.
function RAGLD_dashboard_hardware_init() {
// checking for form submission when new hardware is added
add_action( 'admin_post_save_hw', 'RAGLD_process_hw_form' );
}
after the evaluation that form has been submitted in above add_action, function RAGLD_process_hw_form will be called which is meant to validate form entries and take actions/redirections accordingly.
function RAGLD_process_hw_form() {
if (form_is_validated) {
wp_redirect( add_query_arg( array('page' => 'ragld/edit-hardware', 'action'=> 'InvalidData'), admin_url() ));
} else {
//do something else
}
}
This is the solution I could think of for the time being, you can suggest your answers if you feel they are more effective.