I am new to CodeIgniter 3, I am trying to create a static page from this tutorial https://codeigniter.com/userguide3/tutorial/static_pages.html
http://localhost/index.php/pages/view It is work.
but my actual is like this C:\xampp\htdocs\application\views\pages
? (and not work)
why the folder is not /views/pages ?
I am getting confuse.
Also need help ! i am new to codeIgniter 3, I want to create a simple form, but they not work in the views
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
welcome.php
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
both added the controllers
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome');
}
}
class Form extends CI_Controller {
public function index()
{
$this->load->view('form');
}
}
I tried these, but they not work.
You have not mentioned form action correctly. The form action should be look like this
action="<?php echo base_url(); ?>form"
and in the route.php add route for your action.
Example:
$route['form'] = 'form/index';
Then you can have $_POST values in your Form controller.
Related
Can anyone help me make this work? I am very very new to PHP and I was just trying to create a single file thing which asks for a name and returns it. Can anyone help see where I am going wrong? I am testing it on wtools.io
Thanks in advance for any help :)
<form method="POST" action="formFunction()" name="form1">
Name: <input type="text" name="name">
<input name="s1" value="Submit LoL !" type="submit">
</form>
<?php
function formFunction() {
$name = $_POST['name'];
echo $name;
}```
You cannot call a PHP function as the action of your form. Instead, you should create a second file/script, something like post.php, and use that as the action. For example:
<form action="post.php" method="post" name="form1">
Then, in your post.php:
<?php
if (!$_POST) {
print "This should only be called on form submit.";
exit();
}
// you should actually test to see if name is supplied before this next line
$name = $_POST['name'];
echo $name;
?>
<!-- do some other stuff... -->
A similar question for your review Calling a particular PHP function on form submit
I'm new to opencart. I have to write a custom Log-in form for users. Then i design a small code for log-in form in opencart like below. path is (MyTheme/temlate/auth/Sign.tpl)
<form action="<?php echo $Sub; ?>" method="GET" enctype="multipart/form-data">
Name:<Input type="text" name="txtUser">
<br>
Password:<input type="password" name="txtPassword"><br>
<input type="submit">
and controller is like (Path is controller/auth/Sign.php)
<?php
class ControllerAuthSign extends Controller{
public function index() {
$data['Sub']=$this->url->link('auth/result','','SSL');
if(file_exists(DIR_TEMPLATE . $this->config->get('config_template'). '/template/auth/sign.tpl')){
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/auth/sign.tpl',$data));
}
else{
$this->response->setOutput($this->load->view('default/template/account/login.tpl'));
}
}
}
?>
when a user submit the form have to navigate to Result page (Path is /auth/result.tpl)
<?php
echo "Welcome : Mr./Mrs. ".$User;
?>
<br><p>Your are Loged-In</p>
and the controller for Result is.. (Path is /auth/result.php)
<?php
class ControllerAuthResult extends Controller{
public function index() {
$data['User']=$_REQUEST['txtUser'];
$data['Password']=$_REQUEST['txtPassword'];
if(isset($data)){
$this->response->redirect($this->url->link('auth/sign', '', 'SSL'))
}
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/auth/result.tp',$data));
}
}
?>
but the problem is when i click on submit , page navigate to
http://localhost/opencart/index.php?txtUser=Narayana&txtPassword=narayana
and displayed index page. Can any one help how to navigate to result page...?
Thanks in Advance.
Use this
<form action="<?php echo $Sub; ?>" method="POST" enctype="multipart/form-data">
Name:<Input type="text" name="txtUser">
<br>
Password:<input type="password" name="txtPassword"><br>
<input type="submit">
I've been debugging this piece of code for hours now without success. All I'm trying to do is get a view to load. Instead of it loading, I get a white screen and no error messages anywhere.
Here's the code from the controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('welcome_message');
}
//echo "we made it to the function";
public function login_form()
{
$this->load->view('login_form');
}
public function login_submit()
{
print_r( $_POST );
$this->load->model('Usermodel', 'users');
$match = $this->users->authenticate_user( $_POST['email'], $_POST['password'] );
if( $match )
{
echo "User exists in database!";
}
else
{
echo "Email or password is wrong, bretheren!";
}
}
}
The important part is the "function login_form". Here is the code for that view:
<form action="<?php base_url() ?>welcome/login_submit" method="post">
<label>
email:
<input id="email" name="email" />
</label>
<label>
password:
<input type="password" id="password" name="password" />
</label>
<input type="submit" value="Log me in!" />
</form>
This is the link I'm using in my browser to get to the page:
localhost/intranet/index.php/welcome/login_form
The code all looks fine to me and I just can't seem to figure out where the program is breaking. Does anybody have any ideas?
EDIT: I took out the shorthand but I have the same problem.
1) You're missing an echo...
<?php echo base_url() ?>
2) To use base_url(), you also need to load the URL Helper someplace.
https://www.codeigniter.com/user_guide/helpers/url_helper.html
3) I strongly recommend reading the entire documentation, including the simple demos & tutorials, before starting a CodeIgniter project...
https://www.codeigniter.com/user_guide/
4) Although you can use PHP short-tags (as per your server config), they're not recommended.
https://www.codeigniter.com/user_guide/general/styleguide.html#short-open-tags
You have to either enable the URL Helper in your configuration, or in the controller if you want to use the base_url() function:
$this->load->helper('URL');
<form action="<?php base_url() ?>welcome/login_submit" method="post">
should be
<form action="<?php echo base_url() ?>welcome/login_submit" method="post">
OR Shorthand if you configure it properly:
<form action="<?=base_url() ?>welcome/login_submit" method="post">
Noiticed the post about not being able to use shorthand with CodeIgniter and yes you can, I've just completed a project where we used shorthand. It's just how you configure your php.ini
<?= is just short for <?php echo
you are using codeigniter so why not use it's all functionality. you can create form in codeigniter like this
So you no need to use base_url or so and it is the right method to use in codeigniter.
<?php echo form_open("welcome/login_submit",array('method'=>'post')); ?>
<label>
email:
<?php echo form_input(array('name'=>'email','id'=>'email')); ?>
</label>
<label>
password:
<?php echo form_password(array('name'=>'password','id'=>'password')); ?>
</label>
<?php
echo form_button(array('type'=>'submit','value'=>'Log me in!'));
echo form_close(); ?>
my view:
<form action="<?=base_url()?>index.php/frontend/main_con/temp">
<input type="text" name="temp">
<input type="submit"/>
</form>
Controller:
function temp(){
echo $_GET['temp'];
}
i cant able to reach this function and i got an error
An Error Was Encountered
The URI you submitted has disallowed characters.
So, how to pass form data in controller using GET method?
thanx in advance.
View:
<form action="<?=site_url('controller_name/function_name);?>" method="get">
<input type="text" name="temp">
<input type="submit"/>
</form>
Controller
class controller_name extends CI_Controller{
function function_name(){
echo $this->input->get('temp');
}
}
parse_str($_SERVER['QUERY_STRING'],$_GET);
ONLY worked for me after I added the following line to applications/config/config.php:
$config['uri_protocol'] = "PATH_INFO";
To solve the error go to this line. I personally think, that this is a mistake by design, because black-listing symbols from URI would be much better then white-listing.
As for GET variables .. you would have to use <form method="get" action="/what/ever/">.
Can't you use $this->input->get('temp');
<?php
class SignupController extends Zend_Controller_Action
{
function indexAction()
{
if ($this->_request->isPost())
{
echo "Your email address is: " . $this->_request->getPost('email');
}
$this->render("signup");
}
}
This is my controller
this is my view
<html>
<body>
<form action="/signup" method="post">
Email: <input name="email" type="text" />
<input name="btnSubmit" value="Click me" type="submit" />
</form>
</body>
</html>
when I tried to run the view
I am getting result
blank/signup
why this happens
please rectify that error, and tell me the reason for that error
If you are using signup view then replace your render code from this :-
$this->_helper->viewRenderer('signup');
Try it with else:
if ($this->_request->isPost()) {
echo "Your email address is: " . $this->_request->getPost('email');
} else {
$this->render("signup");
}