Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter's URI and form action

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:

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

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/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?

like image 675
hugerth Avatar asked Sep 09 '13 20:09

hugerth


3 Answers

As simple example as to give you is to use CI's site_url()

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

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

Go through url_helper

like image 160
M Khalid Junaid Avatar answered Oct 20 '22 01:10

M Khalid Junaid


please use base_url('mail/sendmail')

as follows

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

like image 1
Kyslik Avatar answered Oct 20 '22 02:10

Kyslik


<?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>
like image 1
sand Avatar answered Oct 20 '22 02:10

sand