douna3367 2018-07-28 11:02
浏览 77
已采纳

JQuery / AJAX和PHP - 使用.load()函数时出现isset()错误

I have a registration page on my website for which I want to implement PHP and AJAX validation. I will show template of my code (my code is much bigger) to try to explain the problem.

registration.php

<!DOCUMENT html>
<html>
 <head>
  <script>
   $(document).ready(function(){
    $("#signupForm").submit(function(event){
     event.preventDefault();
     var firstNameInput = $("#firstName").val();
     var lastNameInput = $("#firstName").val();
     $(".messageForm").load("inculdes/signup.script.php", {
      firstName: firstNameInput,
      lastName: lastNameInput
     });
    });
   });
  </script>
 </head>
 <body>
  <form id="signupForm" action="includes/signup.script.php" method="POST">
   <input id="firstName" type="text" name="firstName">
   <input id="firstName" type="text" name="firstName">
   <button id="submit" name="submit" type="submit">Submit</button>
   <div class="messageForm"></div>
  </form>
 </body>
</html>

signup.script.php

 <?php
  if (isset($_POST['submit'])) {
   include "dbconn.script.php";

   $firstName = $_POST['firstName'];
   $lastName = $_POST['lastName'];

   //Validators for inputs in PHP with various error messages (1-15) and database INSERT and SELECT functions.
   ...

  } else {
     echo "<span>Error 16</span>";
     exit();
    }
  }
 ?>

 <script>
  // JQuery validator for inputs and CSS style changers based on errors
  ...
 </script>

My problem is when I click Submit button, I get error message "Error 16", so my function skip whole PHP script and does not insert data into database. I concluded that the problem is because the variable $_POST['submit'] is not set, because when I change first line of code to if (!isset($_POST[submit''])), it works like charm, but I don't wanna loose ability to prevent users from type signup.script.php in URL field of the browser and run it. How to correct this to work? The tutorial I watched has this code and in that case it works without problem.

P.S. I will expand my code if needed for solution of this problem, because I wanted to spare space so I gave the shorthand version of it.

  • 写回答

3条回答 默认 最新

  • donglun4521 2018-07-28 11:22
    关注

    You simply don't send "submit" with the request. You could add it here:

    $(".messageForm").load("inculdes/signup.script.php", { 
       firstName: firstNameInput, 
       lastName: lastNameInput, 
       submit: 1 
    });
    

    or check for isset($_POST['firstName'])

    Sidenote: If there wasn't an object passed in with load() it would be a GET request. Worth remembering...

    Request Method

    The POST method is used if data is provided as an object; otherwise, GET is assumed.

    The Docs

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决