Problem with codeigniter's redirect function - php

This may be a n00b topic but, anyways, I have been having a rather difficult and strange time with this bug. Basically I was working on a controller method for a page that displays a form. Basically, I was debugging the form's submitted data by var dumping the form input using codeigniter's $this->input->post function like so:
var_dump($this->input->post('first_name'));
Every other day this has worked, but today I was finding that when I dumped post variables to the browser it would return false as if they had no values even though they did have values when I submitted the form. Then I tried accessing the variables through PHP's POST superglobal array directly like so:
var_dump($_POST['first_name']);
and that returned empty as well so then I tried dumping the entire post array like so:
var_dump($_POST);
and it was empty as well despite the fact that I filled out the entire form. Nevertheless, the records in my MySQL database were being updated (which means that the form was submitting even though my $_POST variables appeared empty).
Also, I reasoned that normally, if I var dumped variables in the controller function before a redirect function call that it should give me a 'Headers already sent' error but it never did. It just redirected me to the supposed success page instead of dumping my variables.
So for the about 2 hours I thought that my POST data wasn't being sent and re-checked the code for errors and began commenting out statements one by one until I could find the culprit statement in my script.
Finally, I commented out a chunk of code that sets a success message an redirects, like so:
/*
if($record_updated_successfully)
{
$this->session->set_flashdata('success', $this->lang->line('record-updated-successfully'));
}
redirect('admin/success_page');
*/
and only then did the script start dumping out all my previous variable dumps using codeigniter's $this->input->post function as well as the $_POST superglobal array.
So ok, if the script does indeed redirect me despite the variable dumps sending output before headers are sent then I can see why the $_POST variables would appear empty.
So then the real question is why the script would still redirect despite my sending of output before headers are sent? Has anyone ever experienced this?
Any help with this would be appreciated.
EDIT: with respect to loading the view here's a simplified version of my script looks like
with the debugging var dump statements:
function some_controller_method() {
var_dump($this->input->post());
var_dump($_POST);
// some code
if($this->input->post('form_action') == 'update record') {
// code that runs when the form is submitted
/*
* ...
*/
if($record_updated_successfully)
{
$this->session->set_flashdata('success', $this->lang->line('record-updated-successfully'));
}
redirect('admin/success_page');
}
$this->load->view('my-view-file.php');
}

While I can't be sure, I'm going to assume you were outputting things like the var_dump() in your view file. A view is not executed at the time you call it, for example:
$this->load->view('some_view');
echo "hi!";
In a controller will not result in the contents of some view followed by "hi". It will results in "hi" followed by the contents of some view. The view is actually output after everything else in the controller has run.
This is the only thing that comes to mind with the information you've presented. I'd have to see more code to offer a different diagnosis.

I had "the same" problem and I found an unset() function in a loop in my code. Perhaps this will help.

Related

PHP File successfully recieved Form Data, not sent to $_POST[]

I'm trying to access some data I've sent to this page (pincode.php), by using the $_POST[] array. However, it seems the variable isn't set, even though the network tab shows the data got succesfully sent.
The code I use to get the value from the $_POST[] array (in the pincode.php file):
if (isset($_POST['buy'])) {
$id_buy = $_POST['buy'];
echo $id_buy;
}
The problem is the if-statement doesn't fire, so apperently $_POST['buy'] isn't set.
Does anyone know a possible cause?
UPDATE:
As a test, I've created the file: test.php, with the following content:
This creates the following result and I still don't get the echo:
I am getting the same issue here, am I missing something important?

Proceed code after executing function PHP

I have data on my site a user can edit via a form. So If the page is opened the user will see the form filled whith his personal data. and when updated and he pushes the submit button I`ll call a function.
<?php
if (!empty($_POST)) {
company_change($_SESSION['user_name']);
} ?>
<HTML>
(...) <-- form is here
</HTML>
after this function is processed I'll receive the return from the function, but the script is not proceed any further. (the regular HTML part)
So my question is. If I direct to a function from a IF statement, and let update some records. How can i make sure the script is executed further after completion of the function logic.
Do i miss a trigger in te IF statement or at the END of the function i call?
Hope someone can help?
According to official documentation (http://php.net/manual/en/functions.user-defined.php and http://php.net/manual/en/functions.returning-values.php), functions come back after they are called, even if you call them from an IF statement.
If your execution flow is not coming back from the function, there may be some reasons :
Something is happening in the function itself, for example, an unhandled exception been thrown.
The function internally jumps to another script or function where it is not returning from
Paste more of your code to take a better look.
You don`t believe this but I had a "}" to much.
So the answer on this questions is: Use an IDE with auto-syntax correction/checking.
(I now use ShiftEdeit.net but you have much more)

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!

var_dump($_POST); is empty but not var_dump($_POST); die;

i have found some similar topics on this issue, but nothing for my case:
i send some post data via form, but the $_POST array is always empty (not null). but when i add a "die;" or "exit;" after var_dump($_POST); i can see all data sent.
maybe its relevant to know, that this is inside a (shopware) plugin which is called on "onPreDispatch".
thanks for any help
Your (shopware) plugin probably uses output buffering. Which means it will gather all the ecoes and prints until you call ob_flush() which prints all the buffer.
die() function, apart from everything else, also flushes the buffer when called.
So, if you do ob_flush() after your echo you should get the needed result.
the problem was the redirect, which resetted the post although it was after i read the parameters in the request.
i did not know, that shopware saves the desired data (paymentID) in the database. so i switched my plugin back to "onPostDispatch" (this way it will be called after all other actions, one of them saves the data in db). now i can just read the db and get the same data, which was initially in the post array.
i tried to be "the first" who reads the post, but could not work it out. now i am the last who reads it and it works fine.
thanks for all answers! the clue here was "redirect".

CodeIgniter is caching view variable even if I explicitly set it again

I'm using the CodeIgniter PHP framework. I have a simple form that has an $editcode hidden variable that gets sent on form submit. The form submits data to a form processing function which then redisplays the view with the form again, however with the $editcode$ variable changed to a new value.
The problem is that no matter what I do, the value of $editcode remains the same as the original value even after the form is submitted and the view redrawn with a new $editcode variable.
Extract from my view showing how $editcode is included and submitted.
echo form_open('/add', $formattributes);
echo form_hidden('editcode', set_value('editcode', $editcode));
echo form_submit('submit','Submit!','id="submit"');
The add() function code (the $refreshed_editcode is positively different from the original $editcode that got generated).
...
$data['editcode'] = $refreshed_editcode;
$this->load->view('includes/template', $data);
When the view gets redrawn by the add() function, the $editcode value should be $refreshed_editcode, but instead it's still the original value.
I know CodeIgniter does some caching of variables for nested views, however in this case I am explicitly resending new values for the $editcode variable. What gives?
Try getting rid of the call to set_value. Since the form field is hidden, it really shouldn't give you much benefit anyway, and I suspect that is where the "caching" is happening (If I'm not mistaken that function is part of the form_helpers file and it actually is designed to cache to help re-populate data, but it has been a while since I've used it).

Categories