Try to search your question here, if you can't find : Ask Any Question Now ?

Form won't send the email address twice via POST

HomeCategory: stackoverflowForm won't send the email address twice via POST
Avatarrupesh asked 2 weeks ago

I made a coupon generator system and for that you must inform your email address. Now i’m trying to make a small algorithm to check if the email address already received a coupon before. I have a formulary that asks the user for his email, it has a “POST” method and the action points to my controller. Now if i inform a completely new email (an email that has never received a coupon before) it will normally store that email in the “emails” table in my database and generate the coupon.

Now if i go back to the formulary and inform the very same email address i can’t even compare it to the emails on my database to check if it already exist there because it won’t get inside the first if (check my code to understand better)

I can’t understand why it works if the email is new and it doesn’t work if the email already exists.

I’ve tried to use print_r() on the input field to see how PHP is reading it and it only (and exactely like this) returns ’email@email.com’ without quotes. I’ve tried without the first if, in fact i added it later because i noticed that MySQL was giving me an error related to that email field saying it could not be Null

Maybe it’s just a dumb thing and i can’t see it because i’m under some pressure and can’t think properly

Controller:

<?php

class Cupom extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Popup_model');
        $this->load->model('Cupom_model');
        $this->load->model('Email_model');
    }


    function index()
    {
        if ($this->input->post('email_cupom')) {
           //CHECK IF IT IS ALREADY REGISTERED ON EMAILS' TABLE

if ($this->Email_model->get_email_by_email($this->input->post('email_cupom'))) {
  //IF IT EXIST, SAVE IT IN THE VARIABLE BELOW (TURNING IT INTO AN ARRAY

$email = $this->Email_model->get_email_by_email($this->input->post('email_cupom'));

                //CHECK IF THAT EMAIL HAS RECEIVED A COUPON BEFORE
                if ($email['recebeu_cupom'] == 0) {
                    $params_email = array(                         
                        'recebeu_cupom'  => 1,
                    );

                    $this->Email_model->update_email($email['id'], $params_email);

                    //THIS HERE JUST SAVES ON THE DATABASE IF THE USER INFORMED AN EMAIL
                    $ultimo_converted = $this->Popup_model->get_popup_by_popupid(1);

 //GO IN DATABASE AND CHECK HOW MANY PEOPLE INFORMED THEIR EMAIL ON THAT POPUP

$novo_converted = intval($ultimo_converted['converted']) + 1;
                    $params = array(
                        'converted'  => $novo_converted ,
                    );
                    $this->Popup_model->update_popup('1', $params);


                    //GERA O CUPOM PROMETIDO POR ESSE POPUP
                    $params = array(
                        'codigo'  => rand(10000, 99999),
                        'descricao' => 'Cupom de primeira compra',
                            'unico' => '1',
                            'tipo'  => 'P',
                            'valor' => 20,
                            'status'=> '1'
                    );

                    //GRAVA O CUPOM NO BANCO DE DADOS
                    $cupom_id = $this->Cupom_model->add_cupom($params);

                    //BUSCA O CUPOM NO BANCO DE DADOS PELO ID
                    $cupom = $this->Cupom_model->get_cupom($cupom_id);
                    $data['cupom'] = $cupom['codigo'];
                    $data['_view'] = 'page/cupom';
                    $this->load->view('layouts/main', $data);
                } else {
                    redirect(current_url().'?cupom=1');
                }
            } else {
                $params_email = array(
                        'email'  => $this->input->post('email_cupom'),
                        'origem'  => 'Popup de Cupom',
                        'recebeu_cupom'  => 1,
                    );

                $this->Email_model->add_email($params_email);

                //PEGA POPUP PELO ID, CADA POPUP TEM ID DIFERENTE.
                $ultimo_converted = $this->Popup_model->get_popup_by_popupid(1);

                //BUSCA O NÚMERO DE CONVERSÃO DAQUELE POPUP E CONVERTE EM UM NÚMERO INTEIRO
                $novo_converted = intval($ultimo_converted['converted']) + 1;

                //GRAVA +1 CONVERTED PARA AQUELE POPUP
                $params = array(
                    'converted'  => $novo_converted ,
                    );
                $this->Popup_model->update_popup('1', $params);


                //GENERATES THE COUPON
                $params = array(
                    'codigo'  => rand(10000, 99999),
                    'descricao' => 'Cupom de primeira compra',
                    'unico' => '1',
                    'tipo' => 'P',
                    'valor' => 20,
                    'status'=> '1'
                );

                //STORES THE COUPON ON DATABASE
                $cupom_id = $this->Cupom_model->add_cupom($params);

                //SENDS THE COUPON TO THE VIEW
                $cupom = $this->Cupom_model->get_cupom($cupom_id);
                $data['cupom'] = $cupom['codigo'];
                $data['_view'] = 'page/cupom';
                $this->load->view('layouts/main', $data);
            }
        } else {
            echo "<script>alert('não tem post')</script>";
        }
    }

}


View (just the form):

 <form action="cupom" method="POST">

   <div class="row" style="height: 100px; ">
     <div class="col-md-6=8 col-md-offset-3">

    <label>Insira seu E-mail: </label>
<input type="email" name="email_cupom" placeholder="seuemail@email.com.br" style="..." required><br><br>

        </div>
    </div>

<button type="button" class="..." data-dismiss="modal">Não Quero Pagar Menos</button>
<button type="submit" class="..." style="...">RECEBER CUPOM</button>

<div class="...">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Não Quero Pagar Menos</button><br><br>

<button type="submit" class="..." style="...">RECEBER CUPOM</button>
    </div>
 </form>



Model (just the function i’m using, in case you need it):


class Email_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function get_email_by_email($email)
    {
        return $this->db->get_where('emails', array('email' => $email))->row_array();
    }
}





If the email has already received a coupon i'll send a get value to the page and print a message if that get value exists
1 Answers
Best Answer
AvatarJyoti answered 2 weeks ago
Your Answer

4 + 13 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com