Welcome Guest, Not a member yet? Register   Sign In
Form validation error
#1

[eluser]felixk[/eluser]
I have two forms in one page, and when one of the forms have an error, that error is displayed in both forms.
Any ideas how to solve this?
If i shouldn't code in CodeIgniter i would just change name="submit" ...

This is the code for my to my controllers
Code:
function update()
    {
        $data['title'] = "GDI/Kundvagn - Webbshop | Hästtillbehör, båtlås, transportband";
        $data['query_cat'] = $this->categoryModel->getAllCategories();
        $data['myProduct'] = $this->productsModel->getProduct($this->uri->segment(3));
        $data['error'] = '';
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('antal', 'Antal', 'trim|required|integer');
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('buy_view', $data);
        }
        else
        {
            $id = $this->uri->segment(3);
            $_SESSION['cart'][$id] = $_POST['antal'];
            $this->load->view('buy_view', $data);
        }
    }


    function buy ()
    {
        $data['title'] = "GDI/Kundvagn - Webbshop | Hästtillbehör, båtlås, transportband";
        $data['query_cat'] = $this->categoryModel->getAllCategories();
        $data['myProduct'] = $this->productsModel->getProduct($this->uri->segment(3));
        $data['error'] = '';
        //Kundvagnen
        if(!isset($_SESSION['cart']))
        {
          $_SESSION['cart'] = array();
        }
        $id = $this->uri->segment(3);
        //Om buy-sidan har ett ID:
        if(strlen($id) > 0)
        {
            //Om ID är en siffra
            if(ctype_digit($id))
            {
                //Kolla om produkten finns
                if(count($this->productsModel->getProduct($id)) > 0)
                {
                    if(!array_key_exists($id,$_SESSION['cart']))
                    {
                        $_SESSION['cart'][$id]=0;
                    }
                    $_SESSION['cart'][$id]++;
                }
                // Om produkten inte finns, felmeddelande...
                else { $data['error'] = "Felmeddelande: Felaktigt id för produkten."; }
                
            } //Om ID är en siffra --> SLUT
            else { $data['error'] = "Felmeddelande: Felaktigt id för produkten."; }
        } //Om det finns något ID --> SLUT
        
        //Contact form:
        $this->load->library('email');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'lang:Namn', 'required');
        $this->form_validation->set_rules('email', 'lang:E-mail', 'trim|required|valid_email');
        $this->form_validation->set_rules('city', 'lang:Gatuadress', 'required');
        $this->form_validation->set_rules('code', 'lang:Postadress', 'trim|required|min_length[5]|max_length[5]|alpha_numeric');
        $this->form_validation->set_rules('phone', 'Telefonnummer', 'trim|required|alpha_numeric');
        $this->form_validation->set_rules('url', 'Hemsida', '');
        $this->form_validation->set_rules('business', 'Företag/Organisation', '');
        $this->form_validation->set_rules('comment', 'lang:Meddelande', '');
            
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('buy_view', $data);
        }
        else
        {
            $data['hej'] = "hej";
            $this->load->view('buy_view' ,$data);
        }

And here's parts of the viewer:

Code:
<form action="<?php echo base_url(); ?>gdi/update/<?php echo $row['prod_id']; ?>" method="post" name="products">
                  <td>&lt;?php echo $row['prod_number']; ?&gt;</td>
                  <td>&lt;?php echo $row['prod_name']; ?&gt;</td>
                  <td>&lt;input type="text" name="antal" value="&lt;?php echo $antal; ?&gt;" size="3"/&gt;&lt;/td>
                  <td>&lt;?php echo $row['prod_price']; ?&gt;</td>
                  <td>&lt;input type="submit" value="Ändra" /&gt;&lt;/td>
                  &lt;/form&gt;


      &lt;?php echo validation_errors(); ?&gt;
          &lt;?php echo form_open('gdi/buy'); ?&gt;
            <p>&lt;input type="text" tabindex="1" size="22" value="" id="name" name="name"/&gt;
            <label for="name"><small>För- och efternamn (behövd)</small></label></p>
            
            <p>&lt;input type="text" tabindex="2" size="22" value="" id="email" name="email"/&gt;
            <label for="email"><small>E-mail  (behövd)</small></label></p>
            
            <p>&lt;input type="text" tabindex="3" size="22" value="" id="city" name="city"/&gt;
            <label for="city"><small>Gatuadress (behövd)</small></label></p>
            
            <p>&lt;input type="text" tabindex="4" size="22" value="" id="code" name="code"/&gt;
            <label for="code"><small>Postadress (behövd)</small></label></p>
            
            <p>&lt;input type="text" tabindex="5" size="22" value="" id="phone" name="phone"/&gt;
            <label for="phone"><small>Telefon (behövd)</small></label></p>
            
            <p>&lt;input type="text" tabindex="6" size="22" value="" id="url" name="url"/&gt;
            <label for="url"><small>Hemsida</small></label></p>
            
            <p>&lt;input type="text" tabindex="7" size="22" value="" id="business" name="business"/&gt;
            <label for="business"><small>Företag/Organisation</small></label></p>
            
            <p>&lt;textarea tabindex="8" rows="10" cols="50" id="comment" name="comment"&gt;Valfritt meddelande&lt;/textarea&gt;&lt;/p>
            
            <p>&lt;input type="submit" value="Skicka" tabindex="8" id="submit" name="submit"/&gt;
            </p>
        &lt;/form&gt;
#2

[eluser]felixk[/eluser]
Anyone?
#3

[eluser]David Johansson[/eluser]
Just give the submitbuttons some names, like submit1 and sumbit2.
Then change the line:
&lt;?php echo validation_errors(); ?&gt;

to:
&lt;?php if ($this->input->post(submit1)) echo validation_errors(); ?&gt;
if you want to display the errors from the first validation.
#4

[eluser]felixk[/eluser]
Thanks David!
#5

[eluser]felixk[/eluser]
Code:
&lt;?php if ($this->input->post(submit1)) echo validation_errors(); ?&gt;
That didn't work for some reason, but i changed to:
Code:
&lt;?php if(isset($_POST['submit1'])) echo validation_errors(); ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB