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条)

报告相同问题?

悬赏问题

  • ¥20 用泊松逆高斯分布拟合已知数据
  • ¥15 web课程,怎么做啊😭没好好听课 根本不知道怎么下手
  • ¥15 做一个关于单片机的比较难的代码,然后搞一个PPT进行解释
  • ¥15 python提取.csv文件中的链接会经常出现爬取失败
  • ¥15 数据结构中的数组地址问题
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题
  • ¥20 oracle RAC 怎么配置啊,配置