Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] $_POST is getting only the last field
#1

(This post was last modified: 03-11-2015, 08:07 PM by fcordeiro.)

I'm starting now with Codeiginiter and for days I'm trying to fix this code. I have a lot of fields in my form: name, email, phone, message, etc, but $_POST only get the last field, Message.

View HOME.PHP

PHP Code:
<?php echo form_open('/home/cadastrar'); ?>
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="compname">
                                Nome
                            </label>
                            <div class="input-group">
                                <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
                                <input type="text" class="form-control" id="name" placeholder="Nome completo" required="required" /></div>
                        </div>
                        <div class="form-group">
                            <label for="mail">
                                Email Address
                            </label>
                            <div class="input-group">
                                <span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span></span>
                                <input type="email" class="form-control" id="email" placeholder="Digite seu email" required="required" /></div>
                        </div>
                        <div class="form-group">
                            <label for="mobile">
                                Telefone Celular
                            </label>
                            <div class="input-group">
                                <span class="input-group-addon"><span class="glyphicon glyphicon-earphone"></span></span>
                                <input type="text" class="form-control" id="phone" placeholder="(XX) XXXXX-XXXX" required="required" /></div>
                        </div>
                        <div class="form-group">
                            <label for="zip">
                               CEP
                           </label>
                           <div class="input-group">
                                <span class="input-group-addon"><span class="glyphicon glyphicon-home"></span></span>
                           <input type="text" class="form-control" id="cep" /></div>
                        </div>
                        <div class="form-group">
                            <label for="addr">
                                Endereço
                            </label>
                            <input type="text" class="form-control" id="addr" required="required" />
                        </div>
                        <div class="form-group">
                            <label for="complement">
                               Número / Complemento
                           </label>
                           <input type="text" class="form-control" id="numr" required="required" />
                        </div>
                        <div class="form-group">
                            <label for="neighborhood">
                               Bairro
                           </label>
                           <input type="text" class="form-control" id="bairro" />
                        </div>
                        <div class="form-group">
                            <label for="city">
                               Cidade
                           </label>
                           <input type="text" class="form-control" id="cidade" />
                        </div>
                        <div class="form-group">
                            <label for="state">
                               Estado
                           </label>
                           <input type="text" class="form-control" id="estado" />
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="messag">
                                Message</label>
                            <textarea name="message" id="message" class="form-control" rows="9" cols="25"
                                placeholder="Message"></textarea>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <button type="submit" class="btn btn-skin pull-right" id="submit">
                            Efetuar Matrícula
                        </button>
                    </div>
                </div>
                <?php echo form_close(); ?>

Controller HOME.PHP

PHP Code:
public function cadastrar() {
        
$this->output->enable_profiler(TRUE);

        
$this->load->library('form_validation');
        
$this->form_validation->set_rules('name''Nome Completo''trim|required|min_length[6]|max_length[50]|xss_clean');
        
$this->form_validation->set_rules('email''Email''trim|required|valid_email');
        
$this->form_validation->set_rules('phone''Telefone Celular''trim|required');
        
$this->form_validation->set_rules('cep''CEP''trim|required|min_length[8]|max_length[9]|xss_clean');
        
$this->form_validation->set_rules('addr''Endereço''trim|required|min_length[6]|max_length[50]|xss_clean');
        
$this->form_validation->set_rules('numr''Número / Complemento''trim|required|min_length[1]|max_length[50]|xss_clean');
        
$this->form_validation->set_rules('bairro''Bairro''trim|required|min_length[3]|max_length[50]|xss_clean');
        
$this->form_validation->set_rules('cidade''Cidade''trim|required|min_length[3]|max_length[50]|xss_clean');
        
$this->form_validation->set_rules('estado''Estado''trim|required|min_length[2]|max_length[2]|xss_clean');
        
        
$data['title'] = "Curso";
        
        if (
$this->form_validation->run() === FALSE) {
            
$data['msg'] = " Desculpe, não foi possível concluir a matrícula.";
            
$this->load->view('templates/header'$data);
            
$this->load->view('pages/home');
            
$this->load->view('templates/footer');    
        } else {
            
$data['msg'] = " Matrícula efetuada! Clique no botão abaixo para efetuar o pagamento.";
            
$this->load->view('templates/header'$data);
            
$this->load->view('templates/registered');
            
$this->load->view('templates/footer');
        }
    } 

So, form_validation always return FALSE because I'm only receiving data from the textarea "message".

Form submited:
[Image: Sele_o_043.jpg]

Data received ($_POST):
[Image: Sele_o_044.jpg]

Thank you for your help.
Reply
#2

Did you notice that your 'message' field is the only one with a 'name' attribute?
PHP uses that for POST parameters, and not the 'id' attribute.
Add the 'name' attribute to your other fields and they should be accessible to your code.
Reply
#3

(03-11-2015, 07:46 PM)ciadmin Wrote: Did you notice that your 'message' field is the only one with a 'name' attribute?
PHP uses that for POST parameters, and not the 'id' attribute.
Add the 'name' attribute to your other fields and they should be accessible to your code.

Thank you! Just after post the message I saw it. CTRL + C / CTRL + V, always replicating our own mistakes Dodgy This post can be closed.
Reply
#4

(03-11-2015, 08:06 PM)fcordeiro Wrote:
(03-11-2015, 07:46 PM)ciadmin Wrote: Did you notice that your 'message' field is the only one with a 'name' attribute?
PHP uses that for POST parameters, and not the 'id' attribute.
Add the 'name' attribute to your other fields and they should be accessible to your code.

Thank you! Just after post the message I saw it. CTRL + C / CTRL + V, always replicating our own mistakes Dodgy This post can be closed.

I will leave the post here, in case other users have the same problem. It is not the first time I have seen it Undecided
Reply
#5

(03-11-2015, 07:46 PM)ciadmin Wrote: Did you notice that your 'message' field is the only one with a 'name' attribute?
PHP uses that for POST parameters, and not the 'id' attribute.
Add the 'name' attribute to your other fields and they should be accessible to your code.

I just wanted to add a note that this is part of the standard for HTML forms, so it happens in the browser before PHP gets involved. I think most of us, as web developers, run into this at some point (hopefully early) in our careers, regardless of what language we use on the server.

With the increased use of AJAX libraries to submit forms, I occasionally run into a library that builds a POST and uses ID values if the names aren't present, but I would generally recommend avoiding depending on behavior like that, as it just becomes confusing and/or difficult to troubleshoot down the road.
Reply
#6

Great post! This blog helps roadrunner users for Roadrunner email not working error. This post gives solutions for this error. Thanks for sharing this great post.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB