CodeIgniter URI and Form Action

let's say that we are on

somesite.com/mail 

this means that we are using a controller named "mail". This controller has the function:

 public function index(){ $this->load->view('header'); $this->load->view('mail/contact_form'); $this->load->view('footer'); } 

This function loads when we enter the address bar only

 somesite.com/mail 

and press enter (no additional arguments). And look at the first line of the form contact_form:

 <form role="form" method="post" action="sendmail"> 

And that is my problem. When I type the address with a backslash at the end, for example:

 mysite.com/mail/ 

and use my contact formula, everything works fine, and my Mail controller loads the sendmail function, and now the URL:

 mysite.com/mail/sendmail/ 

But when I forget about the backslash (mysite.com/mail), it searches for a controller named "sendmail", but I don't want that. Then my url:

 mysite.com/sendmail 

but of course I don't have controllers named sendmail. My question is: how do I change the action of my formula or what should I do to make this work well? Is this the only answer you need to remember about the backslash or what?

+6
source share
7 answers

As a simple example, to give you should use CI site_url()

site_url("controller/function") and it will take care of other url actions.

 <form role="form" method="post" action="<?php echo site_url('mail/sendmail');?>"> 

Go through url_helper

+10
source

use base_url('mail/sendmail')

in the following way

<form role="form" method="post" action="<?=base_url('mail/sendmail')?>">

+1
source
 <?php include(conexao.php) if($_post) { $usuario = $_POST['usuario']; $senha = md5 ($post['senha']; $sql = 'select * from usuarios where usuario = ' .$usuario. " and senha = " .$senha."'"; $query = mysql_query($sql); $num_rows = mysql_num_rows($query); if ($num_rows >=1_ { session_start(); $_session['logado'] = true; $_session['usuario'] = $usuario; header('Location: logado.php'); else $erro= 1 } } ?> <html> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale = 1.0"> </head> <body> <section> <div class="col-md-8 defaultbox-container"> <h1>Acesso</h1> <?php if($erro){ ?> <div class="alert alert-danger alert-dismissable"> <strong>Erro!</strong> Usuario ou senha incorretos. </div> <?php } ?> <form role="form" method="post" action=""> <div class="form-group"> <label for="">Usuario:</label> <input type="text" class="form-control" name="usuario" placeholder="Usuario"> </div> <div class="form-group"> <label for="">Senha:</label> <input type="password" class="form-control" name="senha" placeholder="Senha"> </div> <button type="submit" >Acessar</button> </form> </div> </section> </body> </html> 
+1
source
 <?php include('conexao.php'); session_start(); //VERIFICA SE O USUARIO ESTA LOGADO if($_SESSION['logado'] == 0){ header('Location: login.php'); } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Login</title> </head> <body> <section class="container"> <div class="col-md-8 defaultbox-container"> <h1>BEM VINDO <?php echo $_SESSION['usuario'];?> <small>Painel de controle</small> <a href="logout.php" class="btn btn-danger pull-right">Logout</a></h1> <div> </div> </div> </section> <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> </body> </html> 
0
source

Be sure to change to make lowercase, or it will not allow you to send special characters such as

 $config['charset'] = 'utf-8'; 
0
source

Why, when I use this "> result in the address bar of my web browser, index.php is not displayed, and the result looks like this: http: // localhost / my_ci_project / mail / sendmail ?

help me

0
source

The first url call. Then give an action in the form of type action="<?php echo base_url();?>index.php/mail/sendmail"

then write the code in the controller function

-one
source

Source: https://habr.com/ru/post/1501360/


All Articles