Please upgrade here. These earlier versions are no longer being updated and have security issues.

Multiple forms on one page getting same id

edited August 2012 in Vanilla 2.0 - 2.8

I'm sorry if this has been answered before. I searched but couldn't find anything.

I am working for a custom application internally for our company. In our application, I have a page that has two forms on it. In my controller, I create two forms like so:

$this->Form = new Gdn_Form();
$this->Form2 = new Gdn_Form();

In the view, I use them both and they work correctly. However, when I check the page in developer tools in Chrome, both forms have the same id - 'id="Form_Form"'. This is causing me issues because I am using javascript to do some client side form validation (along with server side validation if javascript is disabled for some reason). Also, in the controller, I get the POST'd values from "$this->Form->FormValues();" which returns the values for whichever form is returned (which seems weird to me also).

My questions:
1) Why do the forms have the same id? How do I change that so they are different?
2) Is there a way to use javascript client-side to validate the two forms? Admittedly, I am not very good at javascript. I'm just learning it. Currently, I use "$('#Form_Form').submit(function() {.....}" to validate.

Any help would be appreciated. Unfortunately, the page is not public but I can past in more code if needed.

Thanks!

Comments

  • Why really depend on how it is being used but it is a good idea to set your own prefix.

        $this->Form->InputPrefix='MyPrefix';
        echo $this->Form->Open();
        ....
        echo $this->Form->Close();
    

    and

        $Sender->Form->InputPrefix='MyPrefix';
        if($Sender->Form->IsPostBack() != False){
                ....
        }
    

    alternatively use

    echo $this->Form->Open();
    if($this->Form->GetValue('Task')=='MyTask')
        echo $this->Form->Errors();
    $this->Form->AddHidden('Task','MyTask');
    echo $this->Form->Hidden('Task',array('value'=>'MyTask'));
    

    and

        if($Sender->Form->IsPostBack() != False){
               $FormValues = $Sender->Form->FormValues();
               switch($FormValues['Task']){
                     case 'MyTask':
                           ....
                     break;
                     ....
               }
        }
    

    grep is your friend.

    422UnderDogunirambler
  • x00, thanks for the input. I am currently using your alternative method above in my PHP code and it works. However, that didn't help with the javascript code because both forms had the same id. I did get a work around working by using the containing divs though.

    Your first method does take care of the problem though. It lets me change the id of each form so I don't have two forms with the same id.

    Thanks!

Sign In or Register to comment.