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
Related
Hello I am very new to WordPress on my requirement I have created a couple of php files in wordpress theme. Where detailsform.php consists
<form method="post" name="details" action="customerdetails.php">
where after clicking submit button the form has to redirect to customerdetails.php in php it is working fine but in wordpress it is giving 404 error(page not found) I kept all new php files in the existing theme folder.
Please suggest me it is killing my time.
As wordpress has its very own specific way to handle ajax calls, wordpress has it too for post http request by form submitting. In the codex on wordpress there is more details about that.
In a brief explanation, using your functions.php file on your wordpress theme:
The first thing you need to do is set in your action attribute from your form tag, point the following url:
<form action="http://www.example.com/wp-admin/admin-post.php" method="post">
<input type="hidden" name="action" value="my_handler_function">
</form>
Once pointed, you need to add a hidden input with the name attribute action and a value attribute specifying the name of your action. Then, you need to build your handler function on your functions.php file. This is where your going to write the code you need for treat the data you will received by the global $_POST or $_REQUEST variable.
function my_handler_function() {
var_dump($_REQUEST);
die();
//request handlers should die() when they complete their task
}
Next step is to bound this function to your form by using the action hook admin_post_nopriv_ or admin_post_. The difference between these, is where your form is placed. If your form is for a custom functionality for the admin of wordpress then is private and you use admin_post hook action. If this form is part of your public content then use the admin_post_nopriv_ like in the following example:
add_action( 'admin_post_nopriv_my_handler_function', 'my_handler_function' );
As it is show in the code example above, you need to call the add_action function. In the first parameter, It is needed to pass the action hook provide by wordpress combined with the action value specify in the hidden input named action in the form, like admin_post_nopriv_$action_value. In the second parameter, you need to placed the function name you build on you functions.php file. Both are mandatory.
For matters of conventions, generally, the name of the function handler is set it as same as the value of the action input to avoid misunderstandings and gain more readability.
Onced everything is put it all together, all you have to do is test your code.
Happy coding!!
PD: If you want to clarify about this procedure of wordpress, please take a look in the wp-admin/admin-post.php file, but don't even dare to modify it.
// Put your file customerdetails.php in current theme and use following path in action:-
<form method="post" name="details" action="<?php echo get_template_directory_uri() ?>/customerdetails.php">
404 error means the action is wrong for form.
<form method="post" name="details" action="customerdetails.php">
^ ^
Correct the action to exact path of customerdetails.php
Issue must be with path (form redirection URL on submission), try with full url.
Can you share the URL at which you have put your form ??
edit your php file with template.
Ex. http://www.wpbeginner.com/wp-themes/how-to-create-a-custom-page-in-wordpress/
<?php
/*
Template Name: Customer Details Page
*/
get_header();
?>
// your php file as it is.
<?php get_footer(); ?>
now create new page with "customerdetails" name in wp admin & select "Customer Details Page" in right side column & save.
now your form action path will be as below
<form method="post" name="details" action="<?php echo get_site_url(); ?>/customerdetails">
Now your form is getting submitted & customerdetails page also receive post data.
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
I am very new to PHP , but i want to show a html page and handle it with and .php file in codeigniter, How can i do it.
Example could be like this :
http://www.w3schools.com/php/showphp.asp?filename=demo_form_post
I tried to do the same but in controller if i try to run the html file it works fine but as i click on submit and try to fetch values it give my errors.
i do:
1. Made 2 file in view namely: Test.html and Test_Values.php
in Test.html i keep : the code as mentioned in w3 link
and in Test_Value.php I wrote the logic.
2.In controller i calls : $this->load->view("Test.html");
it shows me my html page but as i click on the submit button it says 404 page not found.
Please help me. What am i doing wrong??
Firstly you will have to put your logic in a function in your controller and not a separate .php file. e.g:
class Yourcontrollername extends CI_Controller{
...
function form_processor(){...}
}
Then You are supposed to give the address and the name of the function which is going to process the form you sumbitted in the action attribute of your form! e.g:
<form action="yourControllerName/form_processor" method="post">
Finally, you really need to have to study the Codeigniter Manual
I am a beginner in CodeIgniter framework and I have problem with form action.
I don't have any problems when I do it on this way:
<?php
echo form_open("controller_admin/method1");
?>
<?php
echo form_close();
?>
but when i wrote this:
<form name="form" method="post" action="<?php site_url('controller_admin/method1') ?>">
</form>
Form doesn't want to call controller's method method1, and I don't see why?
Because I'm a beginner I would like to know both ways, but currently only the first one is working.
In order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php
$autoload['helper'] = array('url');
Or, manually:load it in function construct of controller
$this->load->helper('url');
Once it's loaded, be sure to keep in mind that base_url() doesn't implicitly print or echo out anything, rather it returns the value to be printed:
echo base_url();
Remember also that the value returned is the site's base url as provided in the config file.
I use of CodeIgniter. I have a form that saved in formregister.php.
I've written in first of the form formregister.php:
echo form_open('main/sabtm');
The 'main' is a controller and 'sabtm' is a function in controller.
Also, I set $this->load->helper('form'); in controller.
Then, when I click on button in the form, the url in browser changed to localhost/emdad/index.php/formregister, where in source code of my page has set <form action="localhost/emdad/index.php/main/sabtm" method="post" accept-charset="utf-8">
what is problem?
This sounds a strange problem. If I have form action "localhost/emdad/index.php/main/sabtm" in my source code and by submitting the form it changes to "localhost/emdad/index.php/formregister". I think following points can be checked for such an issue:
Please check if you have controller "formregister.php" and somewhere in form/web page you are changing the form action via some client side script like javascript.
Please check if you have properly closed the form (form_close()).
Please check the method sabtm() if it has some validations and it redirects to respective page upon success/failure of the validation.
Alternatively please post full code of controller & view.
the file App.php of the folder Config change
public $indexPage = 'index.php';
to
public $indexPage = '';