How to keep Html file and its php file in Codeigniter? - php

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

Related

Trying to load a portion of page with # tag through conroller

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.

How to embed codeigniter script into external php script

l have a index file of my website in
localhost-->mywebsite-->index.php (this is my external script)
and i have build a registration form with codeigniter.
I can use the registration form using url http://localhost/codeigniter/index.php/form.
But how can i attach that registration form into my website index file.
using include_once"../codeigniter/index.php/form"; in my external php script(index.php of my website) is not working.
However if i make that 'form' the default controller file and use the following code from external php script
include_once "codeigniter/index.php"; it works well. but what if i need other controller files but not just 'form'?
Update:
I can also link to that file from external php script as:
<a href="codeigniter/index.php/form>Register</a>.
But not include. Whenever i include it displays no such file.
So if i understood you correctly you want to use codeigniter's form in some other php website, actually idea to do such thing is wrong (as you can't normally control your form action link etc better to build same for in other website directly), but answer for your question is following trick
Place this code in your other website where you want to have codeigniter form included
$output = shell_exec('php ABSOLUTE_PATH/codeigniter/index.php form index');
// where "ABSOLUTE_PATH" is absolute path to your codeigniter project
// "form" is your controller name
// "index" action name
echo $output; // will echo form html generated by codeigniter
In localhost-->mywebsite-->index.php, you could try this:
<?php include_once('/codeigniter/index.php/form'); ?>
If not you can move your index.php code into a codeigniter view and set it up that way.
try using below code...
<?php $data = file_get_contents("http://localhost/codeigniter/index.php/form/");
$html_encoded = htmlentities($data);
echo $html_encoded;
?>
In the above code we get all contents of the url in $data variable and then use htmlentities on that for html encoding...

form_open helper function not working

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 = '';

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);

how to submit a form and show the result in that page

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

Categories