Domas
Domas

Reputation: 1133

How to pass empty parameter through $_POST

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

Upvotes: 0

Views: 663

Answers (1)

phpisuber01
phpisuber01

Reputation: 7715

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.

Upvotes: 1

Related Questions