Form method Post not working - php

My problem seams to be simple, but I am struggling to get this to work.
Here's my html:
<form class="form" role="form" id="form" method="post" action="<?php echo site_url("register"); ?>" onsubmit="" >
<div class="col-md-6 col-sm-6 col-xs-12 fm">
<input type="text" class="form-control" id="email" name="email"
value="<?php if(isset($validation_errors['post_data']['email'])) echo $validation_errors['post_data']['email']; ?>"
placeholder="Email" >
</div>
<div class="text-center">
<input type="submit" id="btn" name="btn" class="btn" value="Quero ganhar um Cartão">
</div>
</form>
I made the code a bit simple, so you could read it fast.
When I submit the form to the register page, I try to see if it is a post or not like this :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller {
function Register() {
parent::__construct();
$this->load->model('register_model');
}
public function index() {
if($_POST) { echo 'post'; } else { echo 'not a post'; }
}
}
And the controller keeps printing 'not a post'.
It seams like the form only redirect to the URL and do nothing more.
Edit :
I did echo $_SERVER['REQUEST_METHOD']; in my controller and it prints " GET ", so, I guess something is wrong with the method ? I Already tried method="POST", method="post" and it keeps printing " GET ". Don't know what else to do.

add url helper in controller, then only you can access site_url() in your view page
$this->load->helper('url');
remove unnecessary onsubmit="" in your form tag

i did manage to get this going.
My redirect link didn't have "www" on it, and despite the redirect, it needed the "www" to do the POST.
Thanks to everyone that tried to help !

Related

PHP form not submitted to its caller method

I have installed a complete PHP system that is based on CodeIgniter. Its first view is a form with action set to
"<?php echo current_url(); ?>"
and this view is shown from a file that defines a class with some methods, a method for each subsequent view. This setup method exists in the directory: setup/controllers/Setup.php, while the views shown are in the directory: setup/views.
The code that shows the first view is:
if ($this->input->post('licence_agreed')) {
$this->session->set_tempdata('setup_step', 'requirements', $this->setup_timeout);
redirect('requirements');
}
if ( ! file_exists(VIEWPATH .'/license.php')) {
show_404();
} else {
$this->load->view('header', $data);
$this->load->view('license', $data);
$this->load->view('footer', $data);
}
The license form is defined as:
<form accept-charset="utf-8" method="POST" action="<?php echo current_url(); ?>" />
<input type="hidden" name="licence_agreed" value="1" />
<div class="terms"><?php echo lang('text_license_terms'); ?></div>
<div class="buttons">
<p class="text-right"><?php echo lang('text_license_agreed'); ?></p>
<div class="pull-right">
<button type="submit" class="btn btn-success"><?php echo lang('button_continue'); ?></button>
</div>
</div>
When I load the first view and post the form, it reloads the same view again. I have tried to debug the method above and it seems that it only executes the same if condition each time with the same result, regardless of the value of licence_agreed parameter.
I can't figure out what is the problem. It works fine on the server. But locally it reloads the same very first view all the time with no action taken.

redirect url to requested page after login codeigniter

I have an issue while login . when i click on a link and if i am not loggedin than it will redirect me to login page and after success login it should redirect me requested url but it redirect me to profile page.
here is my view file:
<!--Left cols-->
<?php error_reporting(0);?>
<div class="col-lg-8 col-sm-8 col-md-8">
<div class="reg-form">
<h2>Member's Login</h2>
<title>Login</title>
<form method="post" action="<?php echo base_url();?>index.php/register_step1/login">
<div class="row">
<label for="name">Email</label>
<span><i class="icon-name"></i><input type="email" placeholder="example#gmail.com" required id="name" name="email"/></span>
</div>
<div class="row">
<label for="pass">Your Password</label>
<span><i class="icon-pass"></i><input type="password" placeholder="*************" id="pass" required name="password"/></span>
</div>
<!--input type="hidden" name="redirurl" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" /-->
<div class="row">
Forgot Password click here / Register new user
</div>
<div class="row">
<input type="submit" value="Login" name="submit"/>
</div>
</form>
</div>
</div>
<?php $this->load->view("home_template/rightbar");?>
And this is my controller :
public function login(){
$this->load->model("User_model");
$this->load->library('user_agent');
if(isset($_POST["submit"])){
//$url = $this->input->post("redirurl");
$data = $this->User_model->login();
if($data){
foreach($data as $dta){
$id = $dta->id;
$email = $dta->email;
}
$session_array = array("id"=>$id,"email"=>$email);
$this->session->set_userdata("logged_by_login",$session_array);
redirect("profile");
}
}
else{
}
$data["main_content"] = "login_view";
$this->load->view("home_template/template",$data);
}
for page redirection we used redirect() statement in codeigniter.
redirect() sends the user to the specified web page using a redirect
header statement.
redirect() statement resides in the URL helper so we need to load url helper:
$this->load->helper('url');
profile page redirection we write bellow statement and pass first parameter as "Profile" and second last parameter is "refresh/location".
redirect('profile','refresh');
The redirect function loads a local URI specified in the first
parameter of the function call and built using the options specified
in your config file.
The second parameter allows the developer to use different HTTP
commands to perform the redirect "location" or "refresh".
According to the Code Igniter documentation: "Location is faster, but
on Windows servers it can sometimes be a problem." so better to use "refresh" as second parameter
You can use redirect like this. Might be controller not get correct path,
$this->load->helper('url');
redirect(base_url('profile'));
Let me know if it not works.
change your code to
un comment your code
" /-->
change to
<!input type="hidden" name="redirurl" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" />
and in your controler change code as shown below
if($data){
foreach($data as $dta){
$id = $dta->id;
$email = $dta->email;
}
$session_array = array("id"=>$id,"email"=>$email);
$this->session->set_userdata("logged_by_login",$session_array);
redirect("profile");
if(!empty($this->input->post('redirurl'))){
redirect($this->input->post('redirurl'),'refresh');
}else{
redirect('profile','refresh');
}
}

Form submit in opencart

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">

How do you correctly pass post variables from the view to the controller in CodeIgniter?

I haven't used CodeIgniter in nearly a year and I seem to have forgotten a lot of the fundamentals.
I am trying to retrieve the post variables from a form and pass them into a model which inserts them into mysql.
The controller function my form submits to looks like this:
public function validation() {
$this->load->helper("form");
$this->load->model("contact_form");
$data = array(
"name" => $this->input->post("name"),
... etc. etc. ....
);
if ($this->contact_form->new_form($data)) {
$this->load->view("header");
$this->load->view("submitted");
} else echo "Sorry, there was a problem adding the form to the database.";
}
The form in the view is structured like so:
<? echo form_open("form/validation");?>
<div id="one" style="display: block;">
<h1>A Heading</h1>
<p>Some Text</p>
<p class="bold">Name: <input type="text" name="name" class="single" value="<?php echo set_value('name'); ?>"></p>
<p class="bold">Email: <input type="text" name="email" class="single" value="<?php echo set_value('email'); ?>"></p>
<p class="bold">And then some radio buttons</p>
<p> yes <input type="radio" name="registered" value="yes"> no <input type="radio" name="registered" value="no"></p>
<p class="bold">And a textarea...</p>
<textarea name="description" class="fill" value="<?php echo set_value('description'); ?>"></textarea>
next
</div>
<div id="two" style="display:none;">
<h1>Another Heading...</h1>
<p class="bold">And some more textareas</p>
<textarea name="audience" class="fill"></textarea>
... There are four divs in total with further textarea fields ...
<p class="bold"><input type="submit" name="submit" value="submit" class="center"></p>
back
</div>
<? echo form_close();?>
And finally my model is very simple:
class contact_form extends CI_Model {
public function new_form($data) {
$query = $this->db->insert("contact", $data);
if ($query) {
return true;
} else return false;
}
}
The form processes without any errors, but the data just appears as 0's in MySQL. If at any point I attempt to output the value of $_POST it returns BOOL (false), or with $this->input->post('something'); it returns NULL.
You will notice that no actual validation takes place. Initially I was using $this->form_validation->run() and getting the same results. I thought perhaps I was having trouble with the validation so I stripped it out and now I'm fairly certain my problem is that I'm not passing the $_POST variables correctly.
Can anyone explain why I am failing so hard?
I have now resolved this problem.
For some reason <? echo form_open("form/validation");?> was implementing GET and not POST. Replacing that line with <form method="post" accept-charset="utf-8" action="form/validation"/> resolved the issue.
According to the CodeIgniter documentation, by default, form_open should use POST - I have no idea why in my case it decided to use GET.

can seem to submit form in codeigniter and go to next view?

below is my 'mainview.php' view. from here iam attempting to submit and just open the next view which is called 'carerview.php'.
<form action="<?php echo base_url()?>login" method="post">
<div class="input-prepend">
<span class="add-on"><i class="icon-envelope"></i></span>
<input type="text" id="" name="" placeholder="your#email.com"></br></br>
<div class="input-prepend">
<span class="add-on"><i class="icon-lock"></i></span>
<input type="password" id="" name="" placeholder="Password"></br></br>
<button type="submit" class="btn btn-primary"><i class="icon-user icon-white"></i>Sign in</button>
</div>
</div>
</form>
Iam trying to submit this is giving me issues.The Index page loads which contains the above view. but when i submit . i get requested URL not found on this server
. then if i use the full url action="application/controllers/user/login" i get a forbidden, dont have permission to access it.
my method in my controller class is just to load the next view on submit so i dont think there is an issue there . below is the controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function index()
{
if(!$this->isLoggedIn())
{
$this->load->view('mainview');
}
else
{
//do something
}
}
public function login()
{
$this->load->view('carerview');
}
public function isLoggedIn()
{
return false;
}
}
any help would be appreciated thanks.
if you didn't remove index.php from your URL and didn't set anything to base_url in configuration,try this
<?php echo base_url();?>index.php/user/login
localhost/your_app_folder/index.php/controller/action
Your form action is base_url(), which means is the application index route.
Try using form_open() (in the form_helper), which takes care of building the correct url:
<?php echo form_open('user/login');?>
... your form here
<?php echo form_close();?> // since I didn't see a close form tag in your form
Be careful of any routes that might intercept the request.
Alternatively, you could use site_url():
<form method="POST" action="<?php echo site_url('user/login');?>">

Categories