Home » Php » html – form in php not redirecting to page after submitting

html – form in php not redirecting to page after submitting

Posted by: admin February 25, 2020 Leave a comment

Questions:

i have a form in php which i created like a calculator, i have used angular js to display the user entered value on the display, which i designed like below:

<?php

if(isset($_POST['entering'])) {
    header("Location: https://www.google.com");
    exit();
} else {
    $msg="Something Went Wrong. Please try again.";
}
?>

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

  <div class='container'>
    <div class='ct-inner'>

      <!-- Project HTML Below -->
      <div ng-app="" class='calculator'>
        <div class='inner'>

          <div class='innerIn'>
            <div style="height:49px;" class='screen active'>
              <div class='output'> {{name}}</div>

              <div class='screen-layer'></div>
            </div>


            <form method="post" action="">
              <div class='controls'>


                <div style="margin-top:5%;" class="form__group">
                  <input ng-model="name" type="password" class="form__input" id="name" placeholder="Password" required="" maxlength="6" />

                </div>
                <div style="margin-left:35%; margin-top:1%;" class='control-pad operator-pad1'>

                  <input type="button" class='button med oper' name="entering" value="ENTER">
                </div>

              </div>
            </form>
          </div>
          <!-- Inner in End -->
        </div>
      </div>
    </div>
  </div>

this is my whole code, when i am trying to submit the form entering values its not going to different page after submission, can anyone please tell me what could be wrong here, thanks in advance

How to&Answers:

From the Docs:

Since the role of forms in client-side AngularJS applications is different than in classical roundtrip apps, it is desirable for the browser not to translate the form submission into a full page reload that sends the data to the server. Instead some javascript logic should be triggered to handle the form submission in an application-specific way.

For this reason, AngularJS prevents the default action (form submission to the server) unless the <form> element has an action attribute specified.

You can use one of the following two ways to specify what javascript method should be called when a form is submitted:

  • ngSubmit directive on the form element
  • ngClick directive on the first button or input field of type submit (input[type=submit])

For more information, see

Answer:

Your form is not submitting because you don’t have a submit button in it, you have to add button with type=”submit” if you want to submit the form

<button type="submit" value="Submit">Submit</button>

If you want to submit the form to another php script, don’t forget to set the form action attribute

Edit:

Add my button and var_dump the content of $_POST variable on the top of the page, it is probably empty after submit.

Answer:

Use <input type="submit" instead.

<?php
if(isset($_POST['entering'])) {
    header("Location: https://www.google.com");
    exit();
}
?>
<html>
    <head><title>the title</title></head>
    <body>
        <form method="POST">
            <input type="submit" name="entering" value="Go to google.com">
        </form>
    </body>
</html>

Answer:

I have edited the your code this seems to work now is redirected to Google.com.

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

    header("Location: https://www.google.com");
    exit();
} else {
    $msg="Something Went Wrong. Please try again.";
}
?>

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

  <div class='container'>
    <div class='ct-inner'>

      <!-- Project HTML Below -->
      <div ng-app="" class='calculator'>
        <div class='inner'>

          <div class='innerIn'>
            <div style="height:49px;" class='screen active'>
              <div class='output'> {{name}}</div>

              <div class='screen-layer'></div>
            </div>


            <form action="" method="post">
              <div class='controls'>


                <div style="margin-top:5%;" class="form__group">
                  <input ng-model="name" type="password" class="form__input" id="password" name = "password" placeholder="Password" required="" maxlength="6" />

                </div>
                <div style="margin-left:35%; margin-top:1%;" class='control-pad operator-pad1'>

                  <input type="submit" class='button med oper' name="entering" value="ENTER">
                </div>

              </div>
            </form>
          </div>
          <!-- Inner in End -->
        </div>
      </div>
    </div>
  </div>