CodeIgniter Forums
Two form on Same Page - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Two form on Same Page (/thread-10439.html)



Two form on Same Page - El Forum - 07-31-2008

[eluser]Yash[/eluser]
Hi,

I've two forms on my page.One is for login and other is search form.

When I try to search something on this page it show me errors for login.I mean it calls different form.

Else

when there is only search form it works perfectly.

What I'm missing?


Two form on Same Page - El Forum - 07-31-2008

[eluser]nmweb[/eluser]
You validate both forms. Check which form is submitted before you validate.


Two form on Same Page - El Forum - 07-31-2008

[eluser]Yash[/eluser]
see if I press a submit button to call a controller function it calls correct function [when it is only form]
but when I try with two forms it calls other forms function


Two form on Same Page - El Forum - 07-31-2008

[eluser]Michael Wales[/eluser]
Place a unique identifier within each of the forms (I like to use the submit button).

Code:
<?php echo form_open('process'); ?>
<input type="text" name="q" />
<input type="submit" name="btnSearch" value="Search" />
<?php echo form_close(); ?>

<?php echo form_open('process'); ?>
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" name="btnLogin" value="Login" />
<?php echo form_close(); ?>

Code:
function process() {
  $this->load->validation();
  // This is one approach - personally, I would use the if...then to determine the form
  // that was submitted and then call a private method to handle that POST request
  if ($this->input->post('btnSearch')) {
    $rules = array('q' => 'trim');
    $fields = array('q' => 'Search terms');
  } elseif ($this->input->post('btnLogin')) {
    $rules = array('username' => 'required|trim|callback__check_login',
                   'password' => 'required');
    $fields = array('username' => 'username',
                    'password' => 'password');
  }
  $this->validation->set_rules($rules);
  $this->validation->set_fields($fields);

  if ($this->validation->run()) {
    // Do some stuff, based on the type of form that was submitted
  }
}



Two form on Same Page - El Forum - 07-31-2008

[eluser]Yash[/eluser]
Can't we have two separate forms. Like you are calling process function I'm calling two different functions from two different forms.

but it always calling one on either pressing submit buttons.


Two form on Same Page - El Forum - 07-31-2008

[eluser]Michael Wales[/eluser]
Paste your code.

You are saying you have two forms, both with different ACTION parameters, and they are ending up at the same controller?

Let's see the View - might need to see your Controller if it's odd.


Two form on Same Page - El Forum - 07-31-2008

[eluser]Hannes Nevalainen[/eluser]
Check your html-source, jou may have nested your form or something..


Two form on Same Page - El Forum - 07-31-2008

[eluser]Yash[/eluser]
Controller Code

Code:
function do_search()
    {
        
        
        $rules['blog_search'] = "required";
        $this->validation->set_rules($rules);

        $fields['blog_search'] = "Search";
        $this->validation->set_fields($fields);
        
        if ($this->validation->run())
        {        
            $keyword=$this->input->post('blog_search');
            $place=$this->input->post('search_area');
            
            
            //redirect(,'refresh',,1);
            
            $data['title']="Information Page";
            $data['header']="Information";
            $data['url']='/blog/search/'.$place.'/'.$keyword;
            $data['time']="2";
            $data['message']="<p>Thank You!</p>
                              <p>Stand by while we direct you to your search results</p>
                              <p>".anchor('/blog/search/'.$place.'/'.$keyword,"Click here")."if you are not redirected automatically</p>";
            $this->load->view('info',$data);
            
            
        }
        else
        {
        $data['title']="MyBlog";
        $data['allposts']="ss";//$this->load->view('searchform',$data,TRUE);
        $data['SearchForm']=$this->load->view('searchform',$data,TRUE);
        $data['pages']=$this->Homemodel->PageLinks();
        $data['Categories']=$this->Categoriesmodel->CreateSimpleLiCategories();
        $data['TagClouds']=$this->Homemodel->TagClouds();
        $data['RecentPosts']=$this->Homemodel->RecentPosts(10);
        $data['RecentComments']=$this->Commentsmodel->RecentComments(10);
        $data['Archives']=$this->Homemodel->ArchivesList();    
        
        $this->load->view('main',$data);
          
        }
        }



function ForgetPassword()
    {
        $data['title']="Forget Password";
        $this->load->library('validation');
        $this->load->helper('security');
        $this->load->library('table');
        
                
        $rules['username'] = "trim|min_length[4]|max_length[20]";
        $rules['email'] = "trim|required|valid_email|max_length[100]";
        $this->validation->set_rules($rules);

        $fields['username'] = 'Username';
        $fields['email'] = 'Email ID';
        $this->validation->set_fields($fields);
        
        if ($this->validation->run())
        {
                //all details are ok.Now save these.
                //$this->load->model('Usersmodel','',TRUE);
                $this->Usersmodel->ForgetPassword();
                
        }
        else
        {
                  //load it first till user fill all details correctly.
                  
                $data['title']="MyBlog";
                $data['allposts']=$this->load->view('users/forgetpassword',$data,TRUE);
                $data['pages']=$this->Homemodel->PageLinks();
                $data['Categories']=$this->Categoriesmodel->CreateSimpleLiCategories();
                $data['TagClouds']=$this->Homemodel->TagClouds();
                $data['RecentPosts']=$this->Homemodel->RecentPosts(10);
                $data['RecentComments']=$this->Commentsmodel->RecentComments(10);
                $data['Archives']=$this->Homemodel->ArchivesList();    
                
                $this->load->view('main',$data);
                  
        }
    }



Two form on Same Page - El Forum - 07-31-2008

[eluser]Yash[/eluser]
Now My two different views
I've created first normal CI form then switch to this one ..even it's not working

Code:
&lt;form action="&lt;?=site_url();?&gt;blog/do_search" method="post" name="search" id="search"&gt;
  
  <p>
    &lt;input type="text" name="blog_search" id="blog_search" /&gt;
  </p>
  <p>
    <select name="search_area" id="search_area">
      <option value="post" selected="selected">In Posts</option>
      <option value="page">In Pages</option>
      <option value="pag">In Tags</option>
    </select>
    &lt;input type="submit" name="search" id="search" value="Search" /&gt;
  </p>
&lt;/form&gt;

Another view forgetpassword

Code:
&lt;?php

echo '<div id="error" >'.$this->validation->error_string.'</div>';
                  
    $attributes = array('class' => 'forgetpassword', 'id' => 'forgetpassword');
    
    echo form_open('users/ForgetPassword', $attributes);
    echo form_hidden('form', true);
    
    
    $username = array(
                  'name'        => 'username',
                  'id'          => 'username',
                  'value'       => $this->validation->username,
                  'size'        => '50',
                  'class'       => 'input',
                );
                
    $email = array(
                  'name'        => 'email',
                  'id'          => 'email',
                  'value'       => $this->validation->email,
                  'size'        => '50',
                  'class'       => 'input',
                );
                                        
    
    
        $tmpl = array (
                    'table_open'          => '<table border="0" cellpadding="4" cellspacing="4"><caption>Forget Password</caption>',

                    'heading_row_start'   => '<tr>',
                    'heading_row_end'     => '</tr>',
                    'heading_cell_start'  => '<th>',
                    'heading_cell_end'    => '</th>',

                    'row_start'           => '<tr>',
                    'row_end'             => '</tr>',
                    'cell_start'          => '<td>',
                    'cell_end'            => '</td>',

                    'row_alt_start'       => '<tr>',
                    'row_alt_end'         => '</tr>',
                    'cell_alt_start'      => '<td>',
                    'cell_alt_end'        => '</td>',

                    'table_close'         => '<tfoot><tr><td>&nbsp;</td><td>&nbsp;</td></tfoot></table>'
              );

    $this->table->set_template($tmpl);
    
    //$this->table->add_row('Username',form_input($username));
    //$this->table->add_row("",form_submit("forgetpassword", "Get it back!"));
    //$this->table->add_row("","");
    //$this->table->add_row("","<h3>OR</h3>");
    //$this->table->add_row("","");
    $this->table->add_row('email',form_input($email));
    $this->table->add_row("",form_submit("forgetpassword", "Get it back!"));
    

    echo $this->table->generate();
    
    
?&gt;

now whenever I press for search it opens forgetpassword function and not the do_search function

I don't what is my problem

Sorry not to follow any naming convention


Two form on Same Page - El Forum - 07-31-2008

[eluser]Yash[/eluser]
I catch the error but this something more weird .

this is the error
Code:
&lt;form action="&lt;?=site_url();?&gt;blog/do_search" method="post" name="search" id="search"&gt;
  
  <p>
    &lt;input type="text" name="blog_search" id="blog_search" /&gt;
  </p>
  <p>
    <select name="search_area" id="search_area">
      <option value="post" selected="selected">In Posts</option>
      <option value="page">In Pages</option>
      <option value="pag">In Tags</option>
    </select>
    &lt;input type="submit" name="search" id="search" value="Search" /&gt;
  </p>
&lt;/form&gt;



form tag in this form goes away in runtime ....
and I left with

Code:
p>
    &lt;input name="blog_search" id="blog_search" type="text"&gt;
  </p>
  <p>
    <select name="search_area" id="search_area">
      <option value="post" selected="selected">In Posts</option>

      <option value="page">In Pages</option>
      <option value="pag">In Tags</option>
    </select>
    &lt;input name="search" id="search" value="Search" type="submit"&gt;
  </p>

anyways working how to stop this form remove form tag.