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 = '';
Related
I have a page in view that has two parts actually which are accessed through # tags, like login#signin and login#signup. When the page loads for the first time it shows login form without having #signin without a problem.
So signin is not causing a problem as it loads at folder/login. But when I try to put folder/login#signup to load directly signup part it gives an error that there is no view login#signup.php. How to cope with this situation?
$this->load->view('workers/login#signup'); is not working.
When I don't put #signup it loads login form that is weird.
I'll expand more on my initial comments for the cause of this error, and how to fix things.
The cause of the issue
As mentioned throughout the comments, you cannot a view using an anchor point. For example, this does not work:
view('workers/login#signup'); // The #signup should not be here.
The documentation states:
Loading a View
To load a particular view file you will use the following method:
$this->load->view('name');
Where name is the name of your view file.
The name is the file is "name", not "name#signup".
Further down,
The .php file extension does not need to be specified unless you use something other than .php.
This implies, that when you use view('name'), CodeIgniter will, by default, load the file name.php. If you include a #signup in it, then CodeIgniter will not be able to find name#signup.php because that file does not exist.
Correct way to handle things
You mentioned you're using the form validation, so we need to ensure no value is lost during the transition process.
Here's a simplified explanation for how to handle it:
function login() {
// Data to be passed to the view (you may or may not already have this)
// More info: https://codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view
$data = array();
// Validation has failed...
$this->form_validation->run() == FALSE ) {
// Set variable to redirect to #signup upon page load
$data['redirect_to_signup'] = true;
}
// Load view with $data which contains values to be passed to the view
$this->load->view('workers/login', $data);
}
In your workers/login view file, we just need to check if the redirect_to_signup value exists. If it does exist, then we can use some simple JavaScript to scroll down the #signup form:
<?php if (isset($redirect_to_signup) && $redirect_to_signup === true): ?>
<script>
var top = document.getElementById('signup').offsetTop;
window.scrollTo(0, top);
</script>
<?php endif; ?>
Because your validation object is still valid, you can use the built-in CodeIgniter functions to preload your form elements with the set_value() helper functions. For example:
<input type="text" name="email" value="<?php echo set_value('email'); ?>">
That hopefully explains how to achieve what you're after:
Validate user submitted form; and
If there are errors, reload the form with validation messages; and
Scroll down to the #signup form on the page.
One alternative is using redirect('login#signup'), but I would not recommend this method. You would need to save your form values and validation errors to the session to show them on the next page. You also run into the issue that the user might click the refresh button and all values would be lost then.
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 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 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
This is simple I guess but I am new to codeigniter, so please help. I have a form page that takes data and when submitted stores that data in database. I then tried two ways to redirect back to main page:
first: $this->load->view('home');
second: redirect('/login/form/', 'refresh');
They successfully redirect or load the home page, but url is still /property/new_property which is the view for data input. also when I click refresh on the home page with url as stated above, data is resubmitted so multiple records in database. How can I redirect to home and make the url property/home?
Thanks
$this->session->set_flashdata('message', 'New Post has been added');redirect('somefunction', 'refresh')
set_flashdata after save successful will help you to destroy your submitted data and redirect will change your url.
$this->load->view() will only load the view and the url will not change .
When you use redirect() it will redirect to a different page and your url will change.
And for your solution You can select the data which you are going to insert in your model before inserting and if it exists then don't run the insert query simply redirect to a different page.
You need url helper to use
redirect('/home')
This is the correct way to redirect at CI.
http://ellislab.com/codeigniter/user_guide/helpers/url_helper.html
the second param. is not 100% required
To load that helper you can use $this->load->helper('url');
or to write it inside your autoload.php at config folder (this way it will be loaded to all page).
For example here is one simple submition page at my site.
public function save_tips(){
if($this->input->post('save_tips')){
//Form validation and contact with model
$res = $this->model->save_tips();
if(count($res['errors'])==0){ //no errors means success
redirect('/my_tips');
} else{
$errors = $res['errors'];
}
}
$data = array();
if(isset($errors)) {
$data["errors"] = $errors;
}
$this->load->view('save_tips',$data);
}
i have faced same issue with codeigniter 3.1.7 , i have just simply validate the submit button value with name , example as below
in view side
<input type="submit" value="save" name = "submit">
in controller side
if(isset($_POST['submit'])){}