PHP Submit refers to the wrong file

Asked

Viewed 86 times

1

Good people I have a problem with my code I have 2 photons in which one logs in and the other does the Register, I’m using a one page template, and I’m having some problems. I when I do the Register everything is ok without problems, the problem is when I log in. I’m using 3 files for each thing, one that has html which is where the code of both Forms is, I have 1 to connect the BD which is what I’m using for the 2 as well, and I have another to connect the table and ask for the data I need, each having their own. At the beginning of each form in html I am asking for the proper file information being the login login.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 log in it doesn’t try to use loginpro.php but regpro.php which is the file I use for Register as you can also see in 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’m very hurt because I need this done with some urgency.

  • 1

    Why, its function submitform() is being used for both Forms. And it gives the Submit in the same form always. You must use a function for each form or pass a parameter telling which of the Forms should be submited.

  • 2

    Why do you use javascript if you can just use one <input type="submit"> ? The way it is there, it’s the same... because you’re not adding anything to Submit...

  • @Rafaelwithoeft I tried with this code and it was working but the template my client asked me to use does not allow sending without java.

  • @Earendul I’m using a submitform() for each I forgot to copy the JS code to register it.

  • Then edit your question and put the code you forgot, ;)

  • Try to change your function to receive parameters, so I believe you are always overwriting the same function and so should generate this problem.

  • @Earendul done ,I’m new in web programming so it’s normal if I make a few mistakes but I appreciate the help

  • @Can you give me an idea of how I can do it please.

  • I don’t know if it’s gonna work, but.... try changing your input to type="Submit" and put the event in the form so onsubmit="submitform(this)"; End result would look something like this: <script>&#xA; function formsubmit(form) {&#xA; form.submit();&#xA; }&#xA;</script>&#xA;<form action="#" method="post" onsubmit="formsubmit(this)">&#xA; <input type="submit" value="Enviar">&#xA;</form>

  • @Rafaelwithoeft Thank you already this everything to work thanks to you very much.

Show 5 more comments

2 answers

3

Problem

Since you indicated that you are using a "single page" template, it 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 who prevails.

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

Solution

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

  1. Functions with specific names

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

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

    and

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

    It is difficult to realize what is normal nowadays given the abuse in the way pages are programmed just because it is cool, but in fact, the normal form of a form to be submitted is through HTML itself:

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

    A <input/> of the kind submit is an element with an attribute created to tell the browser to send form data to the server when clicked.

1

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

Anyway, a simple adjustment to the Submit function can help:

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

And on the presses, 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           ^

Browser other questions tagged

You are not signed in. Login or sign up in order to post.