How to pass empty parameter through $_POST - php

In one of the cakePHP framework's view, I take the parameters given by a user and make an action call. Here is how it looks like:
echo $this->Html->link(__('Save as PDF'),array('action'=>'view_as_pdf',$_POST['data']['Event']['employee'],$_POST['data']['Event']['project'],$_POST['data']['Event']['from'],$_POST['data']['Event']['to'],'ext' => 'pdf'));
The problem appears when *$_POST['data']['Event']['employee']* or *$_POST['data']['Event']['project']* project is not provided.
That makes a proper url like:
pdf.com/action/16/77/2014-01-01/2014-01-15
Look like:
pdf.com/action/16/2014-01-01/2014-01-15
What I would like it to look is something like:
pdf.com/action/16/null/2014-01-01/2014-01-15

Replace the items in your array passed into the link method with ternary operator and check the values. Essentially, you need to set a default value if the POSTed value is not set/empty/what-have-you.
You could do something like this:
empty($_POST['data']['Event']['project']) ? 'null' : $_POST['data']['Event']['project']
You need to pass a string of null in order for it to be passed as 'null'. Likely, the underlying code for that link method ignores empty parameters.
Doing it this way will give you the pdf.com/action/16/null/2014-01-01/2014-01-15 url you are looking to achieve.

Related

php Passing a variable in a URL with value not equal to

I am following this PHP tutorial for getting hold of some basic PHP stuff ->
http://html.net/tutorials/php/lesson10.php.
Here it says that if we enter an URL like this:-> http://html.net/page.php?name=Joe, it will pass value "Joe" to the variable "name" of the page.php script.
This makes me wonder if we can pass multiple names (except that of Joe) to page.php script via something like http://html.net/page.php?name!=Joe ? Is this legal syntax or is there another way of achieving this.
No You can't pass like this. But if you want to comapre the value. You can pass extra query string param which indecates whether to match value or doesn't match.
For example:
http://html.net/page.php?name=Joe&match=0
Which indecates you want result with name = "Joe" (if match = 0)
and
http://html.net/page.php?name=Joe&match=1
Which indecates you want result with name != "Joe" (if match = 1)
No. To achieve that you could send the exclusions on another variable name called "exclusions" and then have some sort of filtering on the serverside.
Like this:
http://html.net/page.php?name=Maria,Peter&exclusions=Joe

Laravel pagination: Append query parameter without value

I have a pagination-instance where I want to append all query parameter from the request to the next_page_url attribute.
I have query parameter with a value like &name=chris but I also have single parameter without a value like &xyz.
However, when I append all query parameters to the pagination instance, like so:
$query->simplePaginate(50)->appends($request->all());
only parameters with a value are getting appended.
How can I append all parameters to the next_page_url?
Update
I want to append query parameters to get the next chunk of requested data.
If I don't, it always gives back "next_page_url":"http://vue.dev/contacts?page=2". What I want is "next_page_url":"http://vue.dev/contacts?name&page=2"
Take URL http://vue.dev/contacts?page=2&name for example. Although perfectly valid, it's still quite ambiguous. Do we mean to include name? Do we mean to exclude name?
So I'd suggest you to use this URL instead http://vue.dev/contacts?page=2&select=name. If you decide to select more stuff you can just do http://vue.dev/contacts?page=2&select=name,age,gender.
Later in your code just use explode to use the value as an array:
$attributes = explode(',', $request->select);
Useful reading: http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api
Even though Fahmis solution is possible as well, I end up using the approach from this so-question. This has the advantage that php reads the parameter as an array automatically. My url end up looking like this:
http://vue.dev/contacts?page=2&select[]=xyz&select[]=abc

Get parameters from request according to the name of variables

I want something like this:
I have send a request to server which contains my_par=test.
I can get it like this $my_par= $_REQUEST["my_par"]. this is the normal way.
But I want if my variable name is match to the request name then it take the value of that parameter automatically.
so I want a function like set_variable_value(array($my_par,$my_par2),$_REQUEST), then it should set the value for variables like this:
$my_par = $_REQUEST["my_par"];
$my_par2 = $_REQUEST["my_par2"];
is it possible to achieve such a thing in PHP?
OR in a simple way
if I send a request like this my_par1=test&my_par2=test2, I want to access them just by add $ at the beginning of their name like this:
$my_par1 and $my_par2
Extract the $_POST array, like this:
extract($_POST);
This will give you variables named after $_POST's keys, with corresponding values.
I have no idea why you'd want to do this, but this can be achieved by using parse_str():
$query = http_build_query($_REQUEST);
parse_str($query);
So, if the request was like below:
test.php?foo=test1&bar=test2&baz=test3
... you can simply access the variables using their query parameter names, like so:
echo $foo;
would output:
test1
With the above code, you may accidentally override already defined variables. If you use the second parameter of parse_str(), you can store the variables in an array, instead:
parse_str($query, $params);
That way, you don't set/override variables in the current scope.

How to get query string values in YII without PATH format?

I am having following url,
www.example.com/index.php?r=recommend/create&id=184?
title=Ruins%20of%20Mahabalipuram
&url=http://localhost/index.php/mediadetail/index/184
I need to get title and url which are query string parameters.
Has anyone worked on getting values in query string in Yii?
There is also the getParam() method in CHttpRequest.
Yii::app()->request->getParam('title')
I've found it a valuable shortcut, since it checks both $_POST and $_GET and gives priority to $_GET, so you can use it to override post variables in the address URL. It also performs null checks and you can provide a default value in the second parameter.
The drawbacks are that you can't use it for arrays and maybe it's a little bit verbose (compared to $_GET['title']).
Look the function parse_str, it would worked and if not, look parse_url but it's not necessary for what you want to do.
They'll automatically be available in your action as $_GET variables. Yii handles parsing them for you as part of the CHttpRequest object

assign a new value to a $_GET variable

We always retrieve the value in a $_GET['var'], but is it possible to assign a value as well? I have a php page where at one point, through ajax, I want to stay on the same page but change the query string or do an assignment like this:
$_GET['myvar'] = 'newvalue';
Is this possible?
Yes you can override the $_GET. however that is only for that request.
with ajax you do a new request and in the ajax call you can just use diffrent values for the data.
Yes, you can assign to the $_GET array, but it won't change the query string in the URL.
It's probably not the wisest thing to do though, as it will be overwritten in the next request
$_GET is just like a regular array. The only difference is that the keys of this array will be automatically populated with values that come with the request using HTTP GET. They are called superglobals because they are automatically and globally available anywhere in your script, other wise they behave just like regular arrays.
So if you have a request like mypage.php?key=value, PHP automatically does something equal to this for you:
$_GET['key'] = 'value';
And just like any regular array, you can overwrite it with a different value. However I really do not see a use case for that unless you are doing some testing or some really weird thingy..

Categories