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 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载