CakePHP editing form url breaks routing - php

I have the following code to create a form in my app:
<?php echo $this->Form->create('User', array('action'=>'edit')); ?>
and it has a route like:
Router::connect('/settings', array('controller'=>'users','action'=>'edit'));
However the action is wrong...
<form action="/users/edit/6" method="post" accept-charset="utf-8">
If I put the form to defaults with:
<?php echo $this->Form->create(); ?>
then it fixes the routing issue, but why does specifying parameters break the routing???

When you specify in the create() parameters an actual URL - it will map to that url! The routes config doesn't map backwards. For example - if you visit /settings, it will load /edit page with the url still showing /settings. But if you visit /edit, it will show the url and will load the page /edit.
So if you want your form action to map to /settings, don't specify a url or use the url option array('url' => '/settings')

Related

Wordpress form is not submitting

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.

send multiple parameters using html GET method

Im trying to send 2 arguments to my controller method from my view using GET.
I don't know exactly how to add my 2nd argument to the action URL.
Here I am sending only 1 argument.
action="/MVC/teacher/lookspecificMessage/<?= $details['person_receive_id']; ?>" method="GET">
However, I want to add this <?= $details['related_message']; ?>
I've tried using lookspecificMessage/<?= $details['person_receive_id']; ?>&<?= $details['related_message']; ?> but it dosen't work.
If URL rewriting is used, url should follow declared template.
In your case it can be
action="/MVC/teacher/lookspecificMessage/$person_receive_id/$related_message"
or
action="/MVC/teacher/lookspecificMessage/$person_receive_id/lookrelaredMessage/$related_message"
or any other form.
You should check rewriting rules to find correct template for url.
Using URL rewriting, you can have parameters in the main URL structure like that. Although, typically, GET parameters are passed at the end after a question mark like so:
example.com/path/to/page?param=1
If you want more than one parameter, you use the ampersand to separate them:
example.com/path/to/page?param=1&foo=bar
In your case, it looks like your .htaccess file is doing some URL rewriting or your framework is doing some magic, which is automatically extracting the parameter and setting it in the $_GET array.
And to be clear, here is how you could do yours:
<?php
// Get params
$receiveId = (int) $details['person_receive_id'];
$message = (string) $details['related_message'];
// Build URL
$url = "/MVC/teacher/lookspecificMessage/$receiveId?related_message=$message";
// Encode URL (in case message has weird values)
$url = urlencode($url);
?>
<form method="GET" action="<?php echo($url); ?>">
<!-- REST OF FORM HERE -->
</form>

Codeigniter form submission adds action url with website url

I am Trying to Submit a form in Codeigniter in form action my url is "Registration/register" when I submit it, it is adding action with existing url. My english is too bad please see attach image for detail.
Example: My Website Url is example.com/d2t/index.php/Registration
I am adding in form action like action = "Registration/register".
then it adds my form url with my website url: example.com/d2t/index.php/Registration/Registration/register
Use site_url() method
action="<?php echo site_url("Registration/register") ?>"
or you can use the form helper :
echo form_open('Registration/register');
http://www.codeigniter.com/userguide3/helpers/form_helper.html?highlight=form%20helper

form_open helper function not working

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 = '';

Magento redirect to the page from that came

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());?>
...
...

Categories