MVC pattern and $_POST variables - php

I am building a php/mysql based framework and cms , and I got stucked into passing variables using post method , from a form located in one controller, to another controller. More exactly , i built a form for changing languages. this form is located in localhost/index/index, and when I select a language, it goes to http://localhost/application/change_language, where change_language is a public function in application class. The thing is that $_POST variables from that form, don't get through , to change_language function. I var_dump-ed the entire $_POST tree, in this function, and all I got is array(0) { }. What I am doing wrong, or why isn't this working? Sorry for my english . Cheers

Sounds like you could use sessions to carry your data over. I ran into this problem with CodeIgniter and post data. I created a session with the post data, worked like a champ.

Could be a variety of things that are going wrong, it will be best to post some code here so we can have a look at what is wrong instead of blindly guessing what might be wrong.
Although as first look, it sounds like you did not properly post the form values to the function change_language. Please check that the form is properly formed. You might want to have a look at this.

In the MVC's perspective, form should be inside View not the Controller. So i would suggest you to put the form inside a View and then specify the action attribute of the form to a Controller which will process the form request something like this:
<form name="myform" method="post" action="index.php/your_controller">
.... more stuff here
</from>
Now you code for the your_controller Controller to process the form request:
class your_controller extends whateverparentcontroller
{
print '<pre>';
print_r($_POST);
}

Related

Codeigniter page not found with trailing slash in url

I am using codeiginiter where I have a form like so:
<form action="announcements/submit_announcement" method="post" id="announcement_form">
Where announcements is my controller.
If I am at www.mysite.com/home and I submit the form it works properly. However, if I am at www.mysite.com/home/ it will append the text above the end, resulting in www.mysite.com/home/announcements/submit_announcement.
It will then get a page not found.
What is going on here? Shouldn't codeiginiter have this in mind and not have this happen? Disappointed in this..
Does anybody have a fix for this?
Just use codeigniter form_open() it makes things so much easier.
In application/autoload in helpers include form to load automatically or load in your controller
$this->load->helper('form');
then in your view
echo form_open('announcements/submit_announcement','id="announcement_form"');
Note that I passed your id in there as a string but you can also include a variable for an array, like if you are using bootstrap and want to add a class.
more in the manual:
https://codeigniter.com/user_guide/helpers/form_helper.html#form_open
note that reading the manual might save you more "disappointment" :-)
Use following instead what you using now
<form action="<?php echo site_url('announcements/submit_announcement');?>" method="post" id="announcement_form">
and now test

How to get data from runAction

I have this code in my view page:
<?php if(isset($journal)&&($keyword=="%")){
return Yii::$app->controller->runAction('journalslist', ['publisherID'=>$journal['publisher_id'], 'partial'=>1]);
}
How can I get 'publisherID' and 'partial' in actionJournalList() in the controller?!!! var_dump($_POST) shows empty array and Yii::$app->getRequest()->getQueryParams() just have $_GET data? How can I config runAction to POST data?
Any help would be appreciated!
As far as I know there shouldn't really be a reason to ever call runAction explicitly as it does not match up with the MVC design.
Explain what you're trying to achieve overall and you'll find a better answer. For now i'd say, look at the current controller/action that is rendering that view and have it check if(isset($journal)&&($keyword=="%")){ and route the information from the existing action to your view instead of trying to call a new action.
Alternatively run a redirect.
POST method is only for form. If you redirect to an action, This means GET. There is no way for redirecting with POST. $_POST will be filled only when you submit a form.

Codeigniter PHP - loading a view at an anchor point

I have a form at the bottom of a long page, if a user fills out the form but it doesn't validate the page is reloaded in the typical codeigniter fashion:
$this->load->view('template',$data);
however because the form is way down at the bottom of the page I need the page to load down there like you do with HTML anchors. Does anyone know how to do this in codeigniter?
I can't use the codeigniter
redirect();
function because it loses the object and the validation errors are gone. Other frameworks I've used like Yii you can call the redirect function like:
$this->redirect();
which solves the problem because you keep the object. I've tried using:
$this->index()
within the controller which works fine as a redirect but the validation errors are in another method which is where the current page is loaded from:
$this->item($labs)
but when I use this it get stuck in a loop
Any ideas? I've seen this question a lot on the net but no clear answers. I'm researching using codeigniter "flash data" but think it's a bit overkill.
cheers.
I can't personally vouch for this, but according to this thread if you append the anchor to the form's action, it will work.
CodeIgniter helper:
<?php echo form_open('controller/function#anchor'); ?>
Or vanilla HTML:
<form method='post' action='controller/function#anchor'>
If you were open to using Javascript, you could easily detect a $validation_failed variable and appropriately scroll. Or, even better, use AJAX.
Another option is to put the form near the top of the page?
Ok, as far as I understood your problem, it isn't much related to the back end(codeigniter). You want the form at the bottom of the page to be 'what-users-sees-on-page-load' (since you mention anchors).
Now, what you can do is, you can set delimiters for your validation error messages using:
echo validation_errors('<div id="bottom_form_error">', '</div>');
Using jQuery ScrollTo, do:
$( function() { $('#bottom_form_error').ScrollTo(); } );
And, the user will be scrolled to the errors at the bottom of the page. Don't forget to include jQuery too.
Anchor hash fragment click is different - it is scrolling at ∞ speed.
I hope that is what you wanted.
P.S. I am ignoring what you said below this line:
Does anyone know how to do this in codeigniter?
as I felt it is not really relevant to the question.

Codeigniter: Load View file with #id added on to url to scroll down page

I am using the form validation class in codeigniter. All working fine but when it reloads the form with the errors the page is at the top again. (because it's refreshed). Is there a way I can add an #id onto the end of the url so it will load further down the page where the form is?
I tried redirect('form/form#form, 'refresh') which worked but didnt carry through the errors.
The problem is if you redirect the page then the POST array is cleared. Therefore set_value() can't repopulate your form.
There are a couple of ways you can go about this
Overwrite the session library. Inside this library, stick your post array into a session flash_data on redirect. Then inside your set_value functions ( there are 2 of them ), if you can't find anything in the post array, look for your flashdata.
Don't use the set_value function. Instead, inside your action controller put your form data into flashdata, and inside your form, use $this->session->flashdata('form_field_1'), instead of set_value('form_field_1');
As far as the redirection goes, you're doing it right.
Sorry for being lazy and not posting code. If this isn't clear enough let me know and I can post code.
You're just doing a simple form validation right? If so, you should be able to use set_value to repopulate your form after an error.
http://codeigniter.com/user_guide/libraries/form_validation.html#repopulatingform
If you're using CI's form validation, I'm assuming your form goes back to the controller. The controller should then determine where to go.
if ($this->form_validation->run() == FALSE) {
$this->load->view('form#anchor');
} else {
$this->load->view('formsuccess');
}
You should be able to dynamically set the #anchor tag in your controller based on what errors you get during validation.

Problems with cakephp

I have a form with multiple fields. Some file and input.
I was working on it for some time.
Everything was working as it should be until a few hours back suddenly the form is not submitting to the right.
I have no idea what went wrong.
Action to submit the form is the same as the view it generated.
After i submit the form browser does not show anything default template address stays the same as the form submits to same view. But i do have redirect statement if the data is saved correctly..
As the form is submitted browser goes blank, not even the default template is shown ... and to add to my pain no errors ... Things are looking worse as they are..
So would really appreciate any pointers..
Thank You.
A quick thing to check for is if there are any whitespace characters at the end of your models or controllers (actually, any .php file) after the '?>' That can cause the behavior you describe.
<?php
class YourController extends AppController {
/*** your code here ***/
}
?>(whitespace chars here)
Something that I do that helps with this problem is to remove the '?>' on my models and controllers. The php interpreter will consider the EOF as the closing tag.
<?php
class YourController extends AppController {
/*** your code here ***/
}
// END
#webbiedave has good advice too, cake has great debugging, although you may need to add the following to your layout template depending on which version of cake you are using...
<?php echo $this->element('sql_dump'); ?>
I put it right at the end of my default template
Please post the code for the controller method that handles the form view.
Also post the <form action=... code.
If it isn't too long, also post the whole of the form html instead of (2.)
With the code, we can help you debug the problem.
To ensure that you can see any errors generated by PHP, open app/config/core.php in your editor and search for debug. Set the debug level to 2 - Configure::write('debug',2);

Categories