简体   繁体   English

我可以在表单提交上调用php文件,并从中调用或回显JS函数吗?

[英]Can I call a php file on form submit, and call or echo a JS function from that?

So I'm basically trying to create an HTML form that will 所以我基本上试图创建一个HTML表单

  1. process my login through php POST method 通过php POST方法处理我的登录
  2. close my login lighbox with javascript, or display an error (right now I'm using JS to change an invisible form value under the login form. 用javascript关闭我的登录lighbox,或者显示错误(现在我正在使用JS来更改登录表单下的不可见表单值。

HTML HTML

      if (isset($_GET['error'])) {
             echo '<p class="error">Error Logging In!</p>';
          }?>
      <br />
       <form action="process_login.php" method="post" name="login_form">                      
           Email: <input class="searchform" type="text" name="email" size="20"/><br />
           Password: <input class="searchform" type="password" 
                         name="password" 
                           id="password" size="20"/><br />
          <input type="button"  class="searchform"
               value="Submit" size="40"  style="height:45px; width:90px"
               onclick="formhash(this.form, this.form.password);" /> 
   <input type="text" id="errorbox" style="height:45px; width:180px" value=""><br>
   </form>

here's the php file: 这是php文件:

    if (isset($_POST['email'], $_POST['p'])) {
      $email = $_POST['email'];
      $password = $_POST['p'];

      if (login($email, $password, $mysqli) == true) {
      // Login success 
    echo "<script>document.getElementById('light').style.display=
      'none';document.getElementById('fade').style.display=    'none'</script>";
    } else {
      // Login failed 
      echo "<script>document.getElementById('errorbox').value='Error'</script>";
  }
  } else {
     // The correct POST variables were not sent to this page. 
     echo 'Invalid Request';
   }




I know I'm not doing this right, I'm just not sure HOW to do it at all... 我知道我做得不对,我只是不确定如何做到这一点......





Well, yes, technically you can do that, but it is a VERY bad practice, and extremely not clean. 嗯,是的,从技术上讲你可以做到这一点,但这是一个非常糟糕的做法,而且非常不干净。

what you'd prefer to do is use Ajax, and do those JS actions in it's success callback: 您更喜欢使用Ajax,并在其成功回调中执行这些JS操作:

Note: you will need to include the jQuery lib in your scripts. 注意:您需要在脚本中包含jQuery lib。

Another none: if you don't have PHP 5.4 on your server, just remove the second callback function, and handle all the scenarios in the success callback 另一个没有:如果你的服务器上没有PHP 5.4,只需删除第二个回调函数,并处理成功回调中的所有场景

!(function($){
   $(function() {
         $('#submitBtn').on('submit', function(){
            $.post('process_login.php', $('form[name="login_form"]').serialize(), function(data){
              //data is a json object which contans the reponse
              data = $.parseJSON(data);
              $("fade").fadeOut();
              $("light").fadeOut();
            },
            function(data){//error callback
              data = $.parseJSON(data);
              if(data.forbidden){
                  $("#errorBox").html("Error!");
              }
              else if(data.error){
                $("#errorBox").html("Invalid request!");
              }
            });
          });

    });
})(window.jQuery);

HTML: HTML:

<form name="login_form">
  <div>
    <input type="text" name="email" placeholder="email">
  </div>
  <div>
    <input type="password" name="p" placeholder="password">
  </div>
  <div>
    <input type="button" id="submitBtn" value="Login">
  </div>
</from>

PHP: PHP:

$response = array();
if (isset($_POST['email'], $_POST['p'])) {
  $email = $_POST['email'];
  $password = $_POST['p'];

      if (login($email, $password, $mysqli) == true) {
          http_response_code(200);//HTTP OK, requires php 5.4
          $response['success'] = true;

    } else {
      // Login failed 
      $response['forbidden'] = true;
      http_response_code(401);//HTTP forbidden
  }
  } else {
     // The correct POST variables were not sent to this page. 
      $response['error'] = true;
      http_response_code(400);//HTTP bad request
   }

echo json_encode($response);

add onsubmit event attribute to the form: <form onsubmit="return validate();"> ... 将onsubmit事件属性添加到表单: <form onsubmit="return validate();"> ...

where validate() is a js function that closes lightbox and returns true if form is ok, or displays errors and returns false if form is bad. 其中validate()是一个关闭lightbox的js函数,如果表单正常则返回true,或者显示错误,如果表单不好则返回false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM