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.
Related
I want to send some data to a php function with javascript. I have already post a question about this earlier today here, though I didnt get a satisfactory answer, maybe didnt explain myself right.
So I have some variables in javascript( I get it through some API). I would like to send this variables to a function in php. I am using codeigniter so I call the function like("somecontroller/somefunction").
I would like the send the data through post to the function. The function calls a view and reloads the whole html. I am looking for something like sending a form. Only I have the variables in javascript. If there isnt any other way I will dynamicly create a form with jquery and send the data with the .click method on the submit button. But is there a more elegant solution??
I know about ajax, but the problem is that I dont want a asynchronous request I want to just submit the data and then for the new html to load.
The easiest way I can think to solve this would be to send the data through the query string, although you'd have to change the target page to accept GET or REQUEST instead of just POST.
To do this just:
window.location.href = 'somecontroller/somefunction?var1='+var1+'&var2='+var2;
Take a look at this link how to pass arguments to your controller methods in CodeIgniter. Then just redirect user to this link.
If you want to redirect through the Form, set action of form to your controller and in php you can access your form elements through $_GET or $_POST
Since you said you're using jQuery:
$.post("somecontroller/somefunction", {
param1: var1,
param2: var2,
...
}, function(result) { ... });
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.
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.
im using a form in php to submit some information into my database
so i used two function to do this
but how to show the result in th same page that has the form
To load the same page you have to assign the variable $_SERVER[PHP_SELF] for the form action field.
<form action='$_SERVER[PHP_SELF]?op=ban' method='post'>
then when the page get load you just check the post variable ,if it contains the appropriate data then print the result with the form.(Normally people using div tag to print the results )
It's as easy as this:
if (isset($_POST['submit']))
{
// do something with your data
}
form();
Forgive me if I am wrong. I think you have copied the code from some where and using it without understanding how forms work.
<form action='index.php?op=ban' method='post'>
The above code says to which page the values should be submitted. As you can see above the values in the form will be submitted to index.php. So the DB operations will(should) happen in index.php and the Thank you message can be shown in index.php.
If you want to show your result in the same page then you will have to submit to the page in which the form resides. But in this case you should have a logic in the page to decide whether the form was submitted or was it loaded first time.
The code snippet in your question does not tell us name of the file the code exists so we wont be able to tell you whether the result will be shown in the same page. Aslo the source code is not complete.
Post a detailed source code and we will be able to help. Hope it helps.
it should be shown on the next request.
because your app should perform an HTTP redirect after POST request.
it can be same page though
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);
}