JQuery 和 AJAX .post 在 PHP 中返回未定义的索引


JQuery and AJAX .post return undefined index in PHP

我意识到这个问题已经被回答了很多次,但我似乎找不到有效的解决方案。我整天都坚持在这个问题上,快要失去理智了。一切似乎都很好,数据是正确的,只是没有传递给 PHP。我尝试将.post与jquery和ajax一起使用。

HTML 和 JQuery/AJAX

<!DOCTYPE html>
<html>
<head>
<title> Today's Clients</title>
<link href="../_css/jquery-ui.min.css">
<script src="../_js/jquery.min.js"></script>
<script src="../_js/jquery-ui.min.js"></script>
<script>
$(document).ready(function(){
    $(".clientSubmit").submit(function(event) {
        event.preventDefault();
        var clientInformation = $(this).serialize();
        $.post('IRCpopulatecheckin.php',clientInformation,clientForm);
        function clientForm(data) {
            if (data!='') {
                $('#clientform').load("IRCpopulatecheckin.php");
                alert(clientInformation);
            } else {
                alert("your data returned nothing!!! rewrite the code...");
            }
        } // end clientForm
    }); // end .submit  
}); // end ready
/*
    $(".clientSubmit").submit(function() {
        var clientInformation = $(this).serialize();
    $.ajax({
        type: 'POST',
        url: 'IRCpopulatecheckin.php',
        data: { 'clientInformation':clientInformation },
        cache: false,
        success: function(result){
            $('#clientform').load("IRCpopulatecheckin.php");
            alert(clientInformation);
            } // end result
        }) // end .ajax
    return false;
    }); // end .submit
*/
</script>
<style>
/* css to style and remove everything but text */
    #hiddenInput {
                position    :relative;
                width       :0px;
                height      :8px;
                top         :-40px;
                left        :-230px;260
                }
    input[name="dailyClient"] {
                background-color: white;
                border: none;
                font-weight :bold;
                font-family :sans-serif;
                font-size: 15px;  
                color: black;
                cursor: default;
                line-height: normal;
                padding: 6px;
                text-align: center;
                text-shadow: none;
                white-space: pre;
                }
    input[name="dailyClient"]:hover {
                color: blue;
                }
</style>                
<body>
<div id="clientform"></div>
<?php 
ini_set('display_errors',1);  error_reporting(E_ALL);
if(isset($_POST['DATE'])) {
    $DATE = $_POST['DATE'];
    }else{
        $DATE = date('Y-m-d');
         }
require_once 'IRCconfig.php';
$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database);
    if ($connection->connect_error) die($connection->connect_error);
$query  = "SELECT * FROM CLIENT_CHECKIN WHERE DATE>='$DATE' ORDER BY F_NAME ASC";
    $result = $connection->query($query);
if (!$result) die ("Database access failed: " . $connection->error);
$rows = $result->num_rows;
for ($j = 0 ; $j < $rows ; ++$j)
    {
        $result->data_seek($j);
        $row = $result->fetch_array(MYSQLI_NUM);
        echo <<<_END
        <pre>
            <div id="hiddenInput"><div style="display:none">
            <form class="clientSubmit" name="clientSubmit" action="IRCpopulatecheckin.php" method="POST">
            <input type="hidden" name="DATE"   value="$row[0]">
            <input type="hidden" name="F_NAME" value="$row[1]">
            <input type="hidden" name="M_NAME" value="$row[2]">
            <input type="hidden" name="L_NAME" value="$row[3]">
            </div></div>
            <input type="submit" name="dailyClient" value="$row[1] $row[2] $row[3]"></form>
            </pre>
_END;
    }
?>
</body>
</html> 

相关 PHP 代码

<?php  
//IRCpopulatecheckin.php
ini_set('display_errors',1);  error_reporting(E_ALL);
require_once 'IRCconfig.php';
$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database);
    if ($connection->connect_error) die($connection->connect_error);
//Doesn't work
if(!isset($_POST['DATE'])){
    echo "something is wrong here"; 
}else{
    $DATE=$_POST["DATE"]; 
     }
if(!isset($_POST['F_NAME'])){
    echo "something is wrong here"; 
}else{
    $DATE=$_POST["F_NAME"]; 
     }
if(!isset($_POST['M_NAME'])){
    echo "something is wrong here"; 
}else{
    $M_NAME=$_POST["M_NAME"]; 
     }
if(!isset($_POST['L_NAME'])){
    echo "something is wrong here"; 
}else{
    $L_NAME=$_POST["L_NAME"]; 
     }

/*Doesn't work
$DATE   = isset($_GET['DATE'])   ? $_GET['DATE']   : $_POST['DATE'];
$F_NAME = isset($_GET['F_NAME']) ? $_GET['F_NAME'] : $_POST['F_NAME'];
$M_NAME = isset($_GET['M_NAME']) ? $_GET['M_NAME'] : $_POST['M_NAME'];
$L_NAME = isset($_GET['L_NAME']) ? $_GET['L_NAME'] : $_POST['L_NAME'];
*/

/* Doesn't work
if(isset($_POST['DATE']))
    $DATE = $_POST['DATE'];
if(isset($_POST['F_NAME']))
    $F_NAME = $_POST['F_NAME'];
if(isset($_POST['M_NAME']))
    $M_NAME = $_POST['M_NAME'];
if(isset($_POST['L_NAME']))
    $L_NAME = $_POST['L_NAME'];
*/
$query  = "SELECT * FROM CLIENT_CHECKIN WHERE DATE='$DATE' AND F_NAME='$F_NAME' AND M_NAME='$M_NAME' AND L_NAME='$L_NAME'";   
$result = $connection->query($query);
    if (!$result) die ("Database access failed: " . $connection->error);

试试这个。

$(document).ready(function(){
   $(".clientSubmit").submit(function(e) {
       e.preventDefault();
       var $form = $(this); 
       var clientInfo = $form.serialize();
       console.log(clientInfo);
       $.ajax({
          type: 'POST',
          url: 'IRCpopulatecheckin.php',
          data: clientInfo,
          cache: false,
          success: function(result) {
              // try seeing if you can get this alert to pop up first
              alert(result);
              //$('#clientform').load("IRCpopulatecheckin.php");
          }
       }); // don't forget semi-colon
   });
});