php CodeIgniter 的 URI 和表单操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18706291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter's URI and form action
提问by hugerth
let's say we are on
假设我们在
somesite.com/mail
it means we are using controller named 'mail'. This controller has function:
这意味着我们正在使用名为“邮件”的控制器。该控制器具有以下功能:
public function index(){
$this->load->view('header');
$this->load->view('mail/contact_form');
$this->load->view('footer');
}
This function is loaded when we type in adress bar just
当我们在地址栏输入时加载这个函数
somesite.com/mail
and press enter (no additional arguments). And let's see first line of the contact_form view:
然后按回车(没有额外的参数)。让我们看看 contact_form 视图的第一行:
<form role="form" method="post" action="sendmail">
And that's my problem. When I type adress with backslash on the end like this:
这就是我的问题。当我像这样在末尾输入带有反斜杠的地址时:
mysite.com/mail/
and use my contact formula, everything works good and my Mail controller loads function sendmail and the URL is now:
并使用我的联系公式,一切正常,我的邮件控制器加载函数 sendmail 并且 URL 现在是:
mysite.com/mail/sendmail/
But when I forget about backslash (mysite.com/mail), it looks for controller named "sendmail" but I don't want it. My url is then:
但是当我忘记反斜杠 (mysite.com/mail) 时,它会查找名为“sendmail”的控制器,但我不想要它。我的网址是:
mysite.com/sendmail
but of course I don't have any controllers named sendmail. My question is - how to change action of my formula or what should I do to make this working good? Is the only answer to remember about backslash or what?
但当然我没有任何名为 sendmail 的控制器。我的问题是 - 如何改变我的公式的动作或者我应该怎么做才能使这个工作正常?关于反斜杠或什么,唯一要记住的答案是什么?
回答by M Khalid Junaid
As simple example as to give you is to use CI's site_url()
给你的简单例子是使用 CI site_url()
site_url("controller/function")
and it will take care of other url things
site_url("controller/function")
它会处理其他 url 的事情
<form role="form" method="post" action="<?php echo site_url('mail/sendmail');?>">
Go through url_helper
回答by Kyslik
please use base_url('mail/sendmail')
请用 base_url('mail/sendmail')
as follows
如下
<form role="form" method="post" action="<?=base_url('mail/sendmail')?>">
<form role="form" method="post" action="<?=base_url('mail/sendmail')?>">
回答by sand
<?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>
回答by sand
<?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>
回答by Keshav Kalra
Don't forget to change to make in lower case or it wont allow you to submit special characters like ó
不要忘记更改为小写,否则它不会允许您提交特殊字符,如 ó
$config['charset'] = 'utf-8';
回答by Prabrisha
First call url helper.
Then give action in form like action="<?php echo base_url();?>index.php/mail/sendmail"
首先调用 url helper。然后以类似的形式给出动作action="<?php echo base_url();?>index.php/mail/sendmail"
then write the code in controller function
然后在控制器函数中编写代码