-1

I want to make a login form which should provide 2 ways to submit , one through the login button which should submit the info into the login.php and another through the href (Forgot password) which should redirect the user and submit the info to the forgotpassword.php file. So far I have tried this code but it doesn't work:

<script type="text/javascript">
function  forgotpassword()
{

document.getElementById("ee").action="project_forgotpassword.php";
document.getElementById("ee").submit();

}    
</script>
<form id="ee" method="POST" action="login.php">
<table>
<tr>
<th>Username</th>
<td><input type="text" name="username"></td>
</tr>   
<tr>
<th>Password</th>
<td><input type="password" name="password"></td>
</tr>
<tr>
<th><a href="project_forgotpassword.php" onClick="javascript:  forgotpassword();">Forgot Password</a></th>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="submit" value="Submit">
</td>
</tr>   
</table>
</form>
enu
  • 2,375
  • 2
  • 11
  • 12

3 Answers3

1
  • First issue as I have commented was the typo mistake.
  • Second issue is submit button name. Refer to following post.

JSFiddle

Code

function forgotpassword() {
  var form = document.getElementById("ee");
  form.action = "project_forgotpassword.php";
  console.log(form)
  form.submit();
}
<form id="ee" method="POST" action="login.php">
  <table>
    <tr>
      <th>Username</th>
      <td>
        <input type="text" name="username">
      </td>
    </tr>
    <tr>
      <th>Password</th>
      <td>
        <input type="password" name="password">
      </td>
    </tr>
    <tr>
      <th><a href="project_forgotpassword.php" onClick="javascript:  forgotpassword();">Forgot Password</a>
      </th>
    </tr>
    <tr>
      <td></td>
      <td>
        <input type="submit" name="btnSubmit" value="Submit">
      </td>
    </tr>
  </table>
</form>

Changes

<input type="submit" name="btnSubmit" value="Submit">
Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • Now it submits fine, but the problem is that when I try to refer to the username tag value by $username=$_POST['username']; it says that the username is undefined index. – enu Dec 26 '15 at 10:25
  • My apologies! I dont know PHP, but are you getting values on submit button? – Rajesh Dec 26 '15 at 10:38
  • yes but my point is that, I also want to get the values from href that is what I am trying to do. – enu Dec 26 '15 at 12:41
0

When calling the javascript function you can omit the javascript: from the onclick handler. You might wish to use that syntax when invoking the function from a standard hyperlink, such as <a href='javascript:func()'>link</a> but that is not common.

In the amended forgotpassword function I use setAttribute rather than trying to call the attribute directly - I think it is more reliable that way. Also, unless in the js function, you prevent the default action of the hyperlink it will simply try to go to the value in the href attribute.

<script type="text/javascript">
    /*
    function forgotpassword(){
        document.getElementById("ee").action="project_forgotpassword.php";
        document.getElementById("ee").submit();
    }
    */

    /* I would tend to use this approach to setting an attribute */
    function forgotpassword(){
        var form=document.getElementById("ee");
        form.setAttribute('action','project_forgotpassword.php');
        form.submit();
    }
</script>
<form id="ee" method="POST" action="login.php">
    <table>
        <tr>
            <th>Username</th>
            <td><input type="text" name="username"></td>
        </tr>   
        <tr>
            <th>Password</th>
            <td><input type="password" name="password"></td>
        </tr>
        <tr>
            <th><a href="#" onclick="forgotpassword()">Forgot Password</a></th>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="submit" name="subform" value="Submit">
            </td>
        </tr>   
    </table>
</form>

One final thing - change the name of the submit button ~ it shouldn't be called submit - especially when trying to invoke form submission using javascript.

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
0

Try this,

$('#forgetpassword').click(function(){

      $("form[name='form_name']").attr("action", "project_forgotpassword.php");      

      $("form[name='form_name']").submit();
   });

Change the forget Password link as below,

<a id="forgetpassword">Forgot Password</a>
user3040610
  • 750
  • 4
  • 15