sarath sarath - 4 months ago 12
HTML Question

Form is not submitting/Working

This form Submission is not working, Can anyone help me...


I am using bootstrap css and trying to submit a simple form, there is a small issue but I can't detect the problem, please help


<div class="container">
<div class="thumbs askus container col-md-6 col-md-offset-4">
<form id="logfrm" action="" method="post" class="form-horizontal">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Username</label>
<div class="col-sm-4">
<input type="text" name="unm" class="form-control" id="inputUser" placeholder="Username">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Password</label>
<div class="col-sm-4">
<input type="password" name="pwd" class="form-control" id="inputPassword" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-4">
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-4">
<button class="btn btn-default" >Sign in</button>
</div>
</div>
</form>
</div>
</div>


PHP Code for form handling

if (isset($_POST['unm'])) {
if ($_POST['unm']=="admin" && $_POST['pwd']=="xxxxxx") {
echo '<script type="text/javascript">alert("Hi Admin");</script>';
header('Location: home.php');
}
else{
echo '<script type="text/javascript">alert("Unknown Username/Bad password");</script>';
}


}

Answer

You just missed the action url so that the form submission does not working ... I just change the form action tag from action="" to action="action.php" where action.php is the file containing your php codes to handle the html form.Use the below Code this will solve your problem.

home.php

    <?php
    if(isset($_POST['submit_button']))
    {

if ($_POST['unm']=="admin" && $_POST['pwd']=="xxxxxx") {
    echo '<script type="text/javascript">alert("Ok");</script>';
    header('Location:home.php');
}
else{
    echo '<script type="text/javascript">alert("Unknown Username/Bad password"); if(confirm("Unknown Username/Bad password")){document.location.reload(true);}</script>';

}
}

else
{

?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body><br/><br/><br/><br/>
    <div class="container">
    <div class="thumbs askus container col-md-6 col-md-offset-4">
        <form id="logfrm"  method="post" class="form-horizontal" action="home.php">
          <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">Username</label>
            <div class="col-sm-4">
              <input type="text" name="unm" class="form-control" id="inputUser" placeholder="Username">
            </div>
          </div>
          <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">Password</label>
            <div class="col-sm-4">
              <input type="password" name="pwd" class="form-control" id="inputPassword" placeholder="Password">
            </div>
          </div>
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-4">
              <div class="checkbox">
                <label>
                  <input type="checkbox"> Remember me
                </label>
              </div>
            </div>
          </div>
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-4">
              <button type="submit" class="btn btn-default" name="submit_button" >Sign in</button>
            </div>
          </div>
        </form>
    </div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
  <?php } ?>
</html>