Do query strings need to be manually sent to POST requests - php

This is a CakePHP / General PHP question.
In my application I use a query string like /login?continue=/admin/posts
This query string is used to redirect users to the URL in the query, but it doesn't work so it seems as though the app can't see the string...
This has got me wondering as basically when you arrive at the page with the string it's a GET request where as when you login, it becomes a POST or XML request (if using AJAX). Do I need to add the query string manually to the form for the POST to see it?
Either in the form action or a hidden input? Or am I barking up the wrong tree?
I'm currently grabbing the query like so:
if(isset($this->params['url']['continue']))
{
$pathtoredirect = $this->params['url']['continue'];
}
else
{
$pathtoredirect = $this->Auth->redirect();
}
But that's within the POST request so perhaps the query is lost... and adding it to a hidden input would not solve the problem with the current code so I would either change the code to look at the hidden field or pass the query with the action on the form?
e.g. <form action="/login?continue=/admin/posts" method="post">
Am I correct in thinking this? And would anyone be able to offer solutions or pros and cons of the two methods I mention?
In short I'm asking how to add the query string to my form action value
It currently looks like:
php echo $this->Form->create('User',
array(
'id' => 'loginform',
'type' => 'post',
'url' => array
(
'admin'=>false,
'controller' => 'users',
'action' => 'login'
)
)
);
So how would I add the query string to the form?
Thanks

When receiving a POST request you can receive both POST and GET variables through the superglobals $_POST and $_GET.
You can either send your paramater in $_GET by including it in the form's action attribute or send it in $_POST by creating an <input type="hidden"> tag within the form

A slash (/) is a reserved character. Encode it with %2F.
http://blooberry.com/indexdot/html/topics/urlencoding.htm#whatwhy

The solution was to do this:
<?php echo $this->Form->create('User',
array('id' => 'loginform', 'type' => 'post',
'url' => array('admin'=>false,'controller'=>'users','action'=>'login','url'=>$this->params['url']['continue']))); ?>
as I have a route already setup to handle the additional URL parameter:
Router::connect('/auth/login', array('controller'=>'users','action'=>'login'));
Router::connect('/auth/login?continue=:url',
array('controller'=>'users','action'=>'login'),
array(
'url' => '[A-Za-z0-9/\._-]+',
'pass' => array('url')
));
If anyone sees any issues with the way I do this though, please feel free to comment.

Related

Laminas toRoute as POST?

I work with Laminas and in a ControllerAction function I want to redirect to another url as a post by using $this->redirect()->toRoute('newSite', $noURLParams, $options);
When I use the "query"-Option ($options = [$query => ['postPara1' => 'blabla', 'postPara2' => 'blubblub']]), I will get the "post"-Parameter as GET-Parameter in the URL:
hhttp://localhost/new-site?postPara1=blabla&postPara2=blubblub
So everyone could see the content of the parameters.
Is there a way to get the toRoute() to make a "POST" out of it?
Or do I have to use another function for this?
Thank you for your interest and help. After you said that it doesn't work, I changed the sequence of what, when should be called and now I can do it without an additional redirect.

Onchange in Symfony2 form

This is my input type
->add(‘year’, ChoiceType::CLASS, array(‘choices’ => $array, ‘attr’ => array(‘onchange’ => ‘this.form.submit()’)));
Onchange page is reloading and data is submitted. Then in controller I can access value like this:
$_POST[‘year’].
The thing is I would like to get $_POST in symfony’s way:
$form[‘year’]->getData();
I don’t know why only $_POST[‘year’] works and no result with $form[‘year’]->getData().
You can use for POST request :
$request->request->get('year');
For GET request:
$request->query->get('year');
For FILE queries:
$request->files.
You can get a single item from the form data like;
$year = $form->get('year')->getData();
In this example 'year' is the name given to the field you are asking for (as per your form builder)

laravel 4 full documentation Form::open

when I search in Laravel 4 API, ex:
Form::open(array $options = array())
I can't find full list of available options? where to find it?
http://laravel.com/api/4.1/Illuminate/Html/FormBuilder.html#method_open
thanks,
See below link for specific options namely method,action,files,url and route
http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html#63
Their usage is explained here
http://laravel.com/docs/html#opening-a-form
And you can add any other attributes you use in HTML as options, too.
The options are:
method: POST, GET, PUT, PATCH, DELETE. The last three methods are spoofed with a hidden field.
action: sets action='...' attribute. If there is also a url, route option, they will be translated into the appropriate URL. Otherwise, the action should point to a Controller action route. If not present, the action defaults to current URL.
accept-charset: is forced to UTF-8
files: can be true if file upload is present, appends enctype = 'multipart/form-data' to the form.
toghether with any option you wish to add (like "id", "enctype" or similar).
You should probably check out at laravel official documentation
http://laravel.com/docs/html
Take a look at the source code: https://github.com/laravel/framework/blob/master/src/Illuminate/Html/FormBuilder.php#L95
Some complicated form:
Form::open(['method' => 'put', 'action' => 'awesomeController#putForm', 'id' => 'my-id', 'class' => 'some more css classes', 'files' => 'true', 'data-url' => 'This could be read in JavaScript']);
method, action and files are specific to Laravel, the other values are just attributes and values.
The list of options is the same list of form without use php:
http://www.w3schools.com/tags/tag_form.asp
The difference is that the attributes will be passed in array format:
array('action' => 'user.update', 'enctype' => 'multipart/form-data', 'method' => 'PUT')
The default method is POST, if not especified.
#edit
Seems that i didn't let clear.
Laravel only include some atributes to help, how url(converted to action), route(converted to action), file(converted to enctype='multipart/form-data'), but yet same thing.
file to enctype conversion:
if (isset($options['files']) && $options['files'])
{
$options['enctype'] = 'multipart/form-data';
}

Dynamic Dropdown in yii

I followed the instructions given in the wiki in creating dynamic dropdowns in yii but still the second dropdown doesnt generate,
here is my view file:
echo CHtml::dropDownList('AccountTypeID','', array(1=>'Admin',2=>'Manager',3=>'Business',4=>'Finance',5=>'Customer Support'),array('ajax' => array('type'=>'POST',
'url'=>CController::createUrl('currentController/dynamiccities'), //url to call.
'update'=>'#city_id', //selector to update
)));
echo CHtml::dropDownList('city_id','', array());
and here is my controller:
public function actionDynamiccities(){
$data= RefAccountgroup::model()->findAll('parent_id=:parent_id',
array(':parent_id'=>(int) $_POST['AccountTypeID']));
$data=CHtml::listData($data,'AccountGroupID','AccountGroupName');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}}
Please pay attention on the 'ajax' declaration
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('currentController/dynamiccities'), //url to call.
'update'=>'#city_id', //remember to change it according to this rules #modelname_columnname
'data'=>array('AccountTypeID'=>'js:this.value'), //I think you missed this line
)));
If you have firebug installed on your firefox browser, try to look up at the console tab, and check whether your dropdownlist change successfully sent out parameter AccountTypeID and you can see the responses as well.
First things first, your question would have been improved if you actually included the link to what tutorial you tried to follow.
Second, when answering the question in the comment, "It didn't sent a data" doesn't really clarify anything. At this point, I'm not sure if you even looked in Firebug or any web developer tool to inspect what was being posted and what was being received. My guess is that if you would have looked you would have seen a POST request being made, with no data being sent, and no data being returned.
Your AJAX call doesn't have a data parameter, so $_POST['AccountTypeID'] will not be defined, meaning that $data in your controller will be empty as well.
Update your AJAX to POST the AccountTypeID, e.g.:
echo CHtml::dropDownList('AccountTypeID',
'',
array(1=>'Admin',2=>'Manager',3=>'Business',4=>'Finance',5=>'Customer Support'),
array('ajax' => array('type'=>'POST',
'url'=>CController::createUrl('currentController/dynamiccities'), //url to call.
'update'=>'#city_id', //selector to update
'data'=> '$("#AccountTypeID").serialize()',
)));
echo CHtml::dropDownList('city_id','', array());
You may need to adjust the data parameter to properly parse the correct input to get the AccountTypeID - I'm just guessing in my sample code above.
Then, while testing, make sure to use Firebug or similar to inspect what's getting POST'ed and make sure that you have AccountTypeID being passed.
Is simple. You need to declare a public variable on your model with the same name as the dropdownlist. Ex.
in your model
public mydropdown;
in your view
echo $form->dropDownList($model, 'mydropdown',array('1'=>'hello','2'=>'bye'));
Not sure if this helps, but...
echo CHtml::dropDownList('city_id','', array( 'id' => 'city_id' ));

In CakePHP, Why using HTML->link instead of manually writing an anchor text?

Isn't it much slower concerning development time ?
What are the advantages of of HTML->link ?
Thanks !
It's just a question of whether you want to generate your own URLs and hard-code them, or if you want Cake to do the work for you. For simple urls leading to the homepage of your site using cake may seem slower, but it's actually useful for dynamic urls, for example:
Say you're printing a table of items and you have a link for each item that deletes that item. You can easily create this using:
<?php
echo $this->Html->link(
'Delete',
array('controller' => 'recipes', 'action' => 'delete', $id),
array(),
"Are you sure you wish to delete this recipe?"
);
Notice how using an array specifying the controller and action as a URL allows you to be agnostic of any custom routes. This can have its advantages.
The corresponding way to do it without the HTML helper would be:
Delete
It can also be really useful for constructing URL query strings automatically. For example, you can do this in array format:
<?php
echo $this->Html->link('View image', array(
'controller' => 'images',
'action' => 'view',
1,
'?' => array('height' => 400, 'width' => 500))
);
That then outputs this line of HTML:
View image
It could be a pain to generate that URL manually.
In summary, while it may seem awkward for simple links, the HTML helper definitely has its uses. For further uses, consult the cakePHP book on the HTML helper's link function.

Categories