PHP Submit uses the wrong file

1

Well, I have a problem with my code. I have 2 forms in which one is logged in and the other is a register. I am using a one page template, and I am having problems. When I do the register everything is ok without problems, the problem is when I log in. I am using 3 files for each thing, one that has the html which is where the code of both forms is, I have 1 to connect to BD which is what I am using for the 2 also, and I have another one to connect to table and ask for the data I need, each having his own. I at the beginning of each form in the html I am requesting the information of the due file being the login loginpro.php as you can see in the code below.

<script language="javascript">
    function submitform()
    {
       document.forms["as"].submit();
    }
</script>
<form name="as" method="POST" action="loginpro.php">
    Username: <input type="text" name="username" /><br /><br />
    Password: <input type="password" name="password" /><br /><br />
    <input style="font-size: 17px" type=button onClick="submitform()" value="Submit"/>
 </form> 

My problem is that when I try to login I do not try to use loginpro.php but rather regpro.php which is the file I use for the register as well as the code below.

<script language="javascript">
function submitform()
{
   document.forms["register"].submit();
}
</script>
<center>
<form name="register" method="POST" action="regpro.php">
     Username: <input type="text" name="username" /><br /><br />
     Email: <input type="text" name="Email" /><br /><br />
     First Name: <input type="text" name="fname" /><br /><br />
     Last Name: <input type="text" name="lname" /><br /><br />
     Password: <input type="password" name="password" /><br /><br />
     <input style="font-size: 11px" type=button onClick="submitform()" value="Submit"/>
 </form>

If you can help me, I get very agitated because I need this ready with some urgency.

    
asked by anonymous 24.03.2015 / 12:27

2 answers

3

Problem

Since you indicated that you were using a "single page" template, this tells us that you have all the code to be a server at the same time, which raises a problem with your JavaScript functions:

function submitform() {
   document.forms["as"].submit();
}

And then further down:

function submitform() {
   document.forms["register"].submit();
}

It turns out that in JavaScript, if you use the same name for two or more functions, the latter is the one that prevails.

So, whenever you use onClick="submitform()" , you are always calling the same function, the last one that is present on your page.

Solution

You have several ways to resolve this issue, each one more suited to the growth of the work you are doing:

  • Roles with specific names

    Generic names for functions, assume a generic job, so, given that each function is dealing with a specific form, why not use:

    function submitLogin() {
      document.forms["as"].submit();
    }
    

    and

    function submitRegister() {
      document.forms["register"].submit();
    }
    
  • Submit form normally

    It is difficult to realize what is normal these days given the abuse in the way pages are programmed just because it is cool, but in fact, the normal way a form is submitted is through HTML itself: / p>

    <input type="submit" value="Enviar" />
    

    A% of type <input/> is an element with an attribute created for tell the browser that it should send form data to the server when it is clicked.

  • 29.03.2015 / 09:31
    1

    I agree with @Zuul that the ideal is not to use JS unnecessarily, as in this case.

    Anyway, a simple adjustment in the submit function can help:

    // Adicionamos o parametro "meuForm" na funcao
    function submitform( meuForm )
    {
       document.forms[ meuForm ].submit();
    }
    

    And in the forms, you call the function with the right name:

    <form name="as" method="POST" action="loginpro.php">
        Username: <input type="text" name="username" /><br /><br />
        Password: <input type="password" name="password" /><br /><br />
        <input style="font-size: 17px" type=button onClick="submitform( 'as' )" value="Submit"/>
                                   //  Aqui você põe o nome do form      ^
    
    
     <input style="font-size: 11px" type=button onClick="submitform( 'register' )" value="Submit"/>
                                   //  Mesma coisa no segundo           ^
    
        
    29.03.2015 / 13:30