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
Related
I am working on a project where i need to access clean URL's always i.e no parameters can be passed in the URL for getting values on another page.
Can anyone please suggest an alternative other than 'Form Submit' method.
Thanks In Advance.
Use $_SESSION for this purpose.
Using $_SESSION you can use variable in multiple pages wherever you want.
// assigning variable
$_SESSION['name']="variable";
//retrieving
echo $_SESSION['name'];
write session_start() at the top of page wherever you want $_SESSION variable
For Clean URL's i prefer you may use HTTP POST Method. Hope that helps.
<form name="frmApp" method="POST" action="/action.php">
</form>
Else, you can use AJAX with jQuery to submit the values to another page.
$.ajax({url:"action.php",success:function(result){
$("div").html(result);
}});
Check out w3schools to get started with AJAX : http://www.w3schools.com/jquery/jquery_ajax.asp
No support for SESSION since i don't like writing php code inside my web page.
Sessions can help, or ajax call.
for using clean url you can use post method in form
<form name='' method='POST' action=''>
You can try mapping resources to the url.
Suppose you want to get mailing address of a customer then you can write.
http://[baseurl]/[customerId]/mailingaddress.
Read more here
I am fairly new to codeigniter, or you can say new to MVC framewrork, i just want to load view 'landing', when user clicks on HTML form submit button, currently when i am using i.e what do i have to type in the action attribute to make it goto 'landing' view. ihave something like this
<form class="navbar-form pull-right" action="<?php $this->load->view('landing')" ?> style="margin-right:20px; margin-top:4px;">
it either says object not found or you dont have permission
You have to submit form on a controller method which will load the new form
<form action="<?php echo site_url('controllername/methodname');?>" method = "POST">
Now controller method
function methodname(){
$this->load->view('landing');
}
You are doing it wrong. The way explained by Raheel Shan is correct. You can't include a template in the form action attribute.
$this->load->view
includes (loads) the template file, it does not provide the url to the template. Think of it like php include , include_one or required and required_once statements.
Kindly read the codeigniter documentation as it is very easy.
http://ellislab.com/codeigniter/user-guide/
Thank you
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 working on a project where i need to access clean URL's always i.e no parameters can be passed in the URL for getting values on another page.
Can anyone please suggest an alternative other than 'Form Submit' method.
Thanks In Advance.
Use $_SESSION for this purpose.
Using $_SESSION you can use variable in multiple pages wherever you want.
// assigning variable
$_SESSION['name']="variable";
//retrieving
echo $_SESSION['name'];
write session_start() at the top of page wherever you want $_SESSION variable
For Clean URL's i prefer you may use HTTP POST Method. Hope that helps.
<form name="frmApp" method="POST" action="/action.php">
</form>
Else, you can use AJAX with jQuery to submit the values to another page.
$.ajax({url:"action.php",success:function(result){
$("div").html(result);
}});
Check out w3schools to get started with AJAX : http://www.w3schools.com/jquery/jquery_ajax.asp
No support for SESSION since i don't like writing php code inside my web page.
Sessions can help, or ajax call.
for using clean url you can use post method in form
<form name='' method='POST' action=''>
You can try mapping resources to the url.
Suppose you want to get mailing address of a customer then you can write.
http://[baseurl]/[customerId]/mailingaddress.
Read more here
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);
}