I can't get the posted values in symfony 2 - php

I'm using Symfony2 and Doctrine 2.
I created a form in Symfony2. But I am not able to get the posted values in the controller. The syntax I use to get the values is:
$block = $request->request->get('txtblock');

You should try to use a var_dump() on $request->request->all(). That way, you can debug what has been send. Most likely, your form/input field has a different name.

You can try to print the content of $_POST. It's not the way Symfony is designed to be, but if $_POST is empty too, I guess the problem is on your form submission.

Related

Is it possible to send an array in a php header (like you can with a form)?

I am trying to make a dynamically sized form for a web-page I am creating! I have had no issue passing the information needed to the 'action' page through a form (including two arrays), by setting the name of all dynamically created forms to be name[i].
To get the data from the array in the 'action' file, I use the code below, and it works fine:
$_POST['name'][$i]
However, I wish to return the information to the form if there is an error with any of it, and the way I am doing this is with headers.
header("Location: ../originalPage.php?error=error&someValue=".$someValue."&someArray[]=".$someArray);
exit();
Is there anything I need to change for this to return something other than Array()?
Clearly the header is using the $_GET method rather than the form's $_POST method, but why can I only send the array one way?!
Any help would be appreciated!
The issue you have is that you try to concatenate your array to a string, but that does not happen in the way you would prefer. You could convert your array into JSON, like this:
../originalPage.php?error=error&someValue=".$someValue."&someArray[]=".json_encode($someArray));
Read more about json_encode by clicking on the link.

How to debug form symfony field values disappearing after handleRequest()?

Question is similar to this: How to debug a Symfony2 form?
The problem is - on posting data - the data is in $_POST array. Form has the fields and they even have some values.
After handleRequest - those values become nulls.
The form is big, its a lot of work now to minimize the code. I think other developers know where else to look for to see the problem.
I have tried looking in debug bar, but I don't see anything useful - just errors, one of them that field is not filled which is not true because it is in $_POST array.
Stepping inside handleRequest also sounds unproductive with many of those loops going on there and unclear code inside.
I have a problem with Symfony 3.4.
One of ways - check if you have not modified $request wrong way.
I needed to unset some data from request object
if ($policyRef->getUser() !== null && $policyRef->getUser()->getHowDidYouHearAboutUs() !== null) {
// this happens when existing account is signing up in checkout with oauth
$postData = $request->request->all();
unset($postData['checkout']['account_setup']['signup']['howDidYouHearAboutUs']);
unset($postData['undefined']); // todo maybe in frontend unset?
$request->request->set('checkout', $postData['checkout']);
}
By stepping inside handleRequest with xdebug, found out that there is error that form contains extra field 'checkout'.
It was because at first I have done this:
$request->request->set('checkout', $postData);
Probalby also in debug bar should have shown this, but I did not pay attention because there were few errors and I had focused on other fields.

Selecting only Non-file Parameters from Laravel Request

For one of my Laravel Web app I want to log all the Request Parameters(Post as well as Get) in database in Json Format for that I am using $request->all() Method, which results in an exception when user tries to upload any file.
that's why I want a way to select only Serializable Parameters from the request.(for get as well as for post Requests) or a way to select all the request parameters except files.
Request::except([]) will not work for me since in Except method we will have to provide the file parameter names.
In my project, i used this except for many fields like below,
$input = $request->except('first_name', 'middle_name', 'last_name', 'address',...);
It is work fine for me.
I stored all the remain values into $input and store values from that input variable.
Please try this one.
In your case please take this debug code for test once, might be you like it to use in your current work
$allRequestParams = array_map(function($input) {
return !is_array($input) ? $input : false;
}, $request->all());
echo '<pre>';
print_r($allRequestParams);
echo '<pre/>';
die;
Since any of the answer didn't work for me I did lots of reading and some digging about laravel but still I could not find the specific solutions I was looking for, so I did a small hack, instead of using Laravel's Request Object and pulling parameters from there I simply used PHP's built in $_REQUEST parameter.
Eg.
$non_file_parameters = $_REQUEST;
$_REQUEST will have both Get as well as Post Parameters except file Parameters coz in Core PHP for files we have $_FILES super global variable.
Thanks guys for your efforts...

how to parse dynamic _POST variable in php

I'm stuck with a php/mySQL thing..
I have a dynamically created form and I want to parse the $_POST variables it generates. To be specific,I have a query in SQL which generates the fields in my form. Then, I need to process these variables in the php file, where the action of the form goes.
However, I cannot parse the dynamically created $_POST variables. Below is my code:
$sql="just-a-query";
$result = mysql_query($sql);
while ($data = mysql_fetch_array($result)) {
${''.$data['parameterName']}=$_POST[$data['parameterName']];
}
For example, if I have 3 variables that got through the form the values:
house=1
tree=3
car=2
I would like to save them via php like this:
$house=$_POST['house'];
$tree=$_POST['tree'];
$car=$_POST['car'];
However I can't get through it. It returns Undefined index error. Any thoughts?
If you want to find if a variable is defined before using it, it's as simple as using isset():
if( isset($_POST[$data['parameterName']]) ) {
${''.$data['parameterName']}=$_POST[$data['parameterName']];
}
If on the other hand, it's supposed to be defined (you see the form element), but then it's not getting defined in the postback. First check to make sure that your form submission type is post, then check to make sure you are using the name attribute in the form elements.
thank you for your time. My problem was that I was parsing wrong parameters from the HTML.
Yes, I'm an idiot and yes, var_dump() helped me to figure my error.
Thanks again!
btw, my code was working perfectly. Ha!

In CakePHP, why would $this->params['form'] be empty if I've just posted a form?

Using CakePHP 1.3, I post a form which correctly fills in $this->data. According to the docs, it seems like $this->params['form'] should be populated with some information as well, but it's simply an empty array. Is there a particular reason for that?
The form is built using the Form Helper, as follows...
Some relevant code:
$default_form_create_options = array(
'inputDefaults' => array(
'label'=>false,
'div'=>false
)
);
echo $form->create('Preappform', $default_form_create_options);
// --- snip, a bunch of form elements created via $form->input()
echo $form->end(array('label'=>'Send This Form ยป', 'class'=>'submit-button', 'escape'=>false));
I know that the form data is available in $this->data, so maybe this is just a documentation/curiosity issue. If so... my bad.
Just for giggles try $this->params['data']. I don't know why but for some reason it shows form data in there for me.
The documentation has conflicting data as you can see here http://book.cakephp.org/view/972/data. I am guessing that if you use the FormHelper it will show up in $this->data and if you don't use the FormHelper it will show up in $this->params['form'].
Notice if you use FormHelper the name of the element will be data['Model']['element_name'] and if you just create the form manually you can name it 'element_name'. By doing the later I believe it throws it into the params['form'] instead of the $this->data.
I too faced the same while working with CakePHP 1.3. Later I solved the issue by using the $this->params['data']. But I have some questions, I'm a newbie to cakephp and using the manual as my reference, it seems that the manual is not at all updated, while searching about this issue, I found out that it was working well in earlier versions and after 1.2, it is not at all there in Cakephp. Is there any CakePHP experts to clarify this stuff ?
Not necessarily related to Cake, but the answer to the problem when I had it: if you're including a file upload in your POST, double-check that the file you're uploading isn't larger than the limit specified in your php.ini file.

Categories