POST/GET array is empty, after form submit

2019-08-21 09:27发布

问题:

I've tried my code with POST and GET method also, but I can't see any value of these methods. After submit the form the page is refreshing but my .php file write out this: Array ( )

I was looking for the value in Chrome developer tools but there is no 'Form data' inside the network tab, just general, response headers and request headers.

My HTML code:

<!--Modal: Login Form-->
      <form id="loginModal" name="loginModal" class="modal fade modal-full-height" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"
        data-backdrop="static" action="/main.php" method="POST">
          <div class="modal-dialog cascading-modal" role="document">
              <!--Content-->
              <div id="loginModalDiv" class="modal-content">
                  <!--Header-->
                  <div class="modal-header myModalHeader">
                      <h4 class="title"><i class="fa fa-user-lg"></i> Login:</h4>
                  </div>
                  <!--Body-->
                  <div class="modal-body">
                      <div class="md-form form-sm">
                          <i class="fa fa-envelope prefix"></i>
                          <input type="text" id="logF_email" name="email" class="form-control">
                          <label for="logF_email">E-mail address:</label>
                      </div>

                      <div class="md-form form-sm">
                          <i class="fa fa-lock prefix"></i>
                          <input type="password" id="logF_passw" name="password" class="form-control">
                          <label for="logF_passw ">Password:</label>
                      </div>

                      <div class="text-center mt-2">
                          <div id="errorLogin"></div>
                          <button id="btnLogin" type="submit" class="btn btn-default myModalBtn">Belépés <i class="fa fa-sign-in ml-1"></i></button>
                      </div>
                  </div>
                  <!--Footer-->
                  <div id="footerLogin" class="modal-footer">
                      <div class="options text-center text-md-center mt-1">
                          <p>You don't have an account? <a class="loginToSignUp myGreenClass" data-toggle="modal" href="#"> Sign up!</a></p>
                          <p>Forget <a class="forgetPassword myGreenClass" data-toggle="modal" href="#"> password?</a></p>
                      </div>
                      <button type="button" class="btn btn-outline-default waves-effect ml-auto myModalBtn" data-dismiss="modal">Close</button>
                  </div>
              </div>
              <!--/.Content-->
          </div>
      </form>

...and the .php file:

$email =""; $password ="";
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    print_r($_POST);
    $email     = $_POST['email'];
    $password = $_POST['password'];
    print_r($email);
    print_r($password);
    if ($email=="") {echo "<br> 'no data'";}
        else        {echo "Your email: ".$email;}
  }

I'm beginner in PHP, and form-handling, so I think I could miss something. I tried to avoid the trivial mistakes, therefor my code contains:

  • action and method proprty for form
  • name attribute for input fields
  • type="submit" for submit button

if ( $_POST ){
if (!empty($_POST)){

also not working

Any idea what's wrong?

回答1:

Change your form method from GET to POST as your backend is filtering on the POST method.



回答2:

In your form tag method is GET it should be POST. If you do not mention method it will take GET method automatically. So you want get data from POST then you should use POST method in form tag. Remove GET from the form tag.

<form id="loginModal" name="loginModal" class="modal fade modal-full-height" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"
        data-backdrop="static" action="/main.php" method="POST">


回答3:

Change the method of the form to post in place of get

method="GET"

to

method="post"

As in backend you looking for request method as post. In frontend and backend both needs to be same.



回答4:

Okay guys, I've found the error!

Before I sent the value I validated the data with jQuery inside the submit function.

$("#loginModal").submit(function(){
...

The problem is that I've disabled the form before submitting it because I wanted to make some precautionary measure to the user won't be able to do anything until the server not respond.
(e.g.: long waiting time if the connection is slow)

...
$('#loginModalDiv *').prop('disabled',true);
$('#footerLogin').hide();
return true;}

When I cut the last two line out before the return, (so the form stay enabled) the submit function became capable to send the data from the input fields.

Conclusion:
So,... for everybody who don't know (like me before), the POST and GET method not send any data from a disabled part(like a < div >) of a form. Hmmm... good to know! :)



标签: php forms