简体   繁体   English

为什么我的 AJAX JQUERY 试图将多个变量值传递给 php 服务器端总是返回错误?

[英]Why is my AJAX JQUERY trying to pass multiple variable values to php server side always returning an error?

I am trying to send multiple values from the client side to the server side through JavaScript.我正在尝试通过 JavaScript 从客户端向服务器端发送多个值。 Here is my JavaScript JQUERY function below:这是我的 JavaScript JQUERY function 下面:

// This function addToRentalCart(car) adds the requested
// car to a user's rental cart. It does this through the use
// of an AJAX call to the server with the required data.
// Since the required car data is already on the associated
// html webpage (index.html) all this function does is read it
// from there and send it to the server side process.
// Server side processing will then take care of the rest
// of the operation.

function addToRentalCart (car)
{
    var carPosition = 'carRow' + car;
    var carAvailabilityCol = 'Col10';
    var carAvailableElement = carPosition + carAvailabilityCol;
    var carAvailable = document.getElementById(carAvailableElement);
    
    if (carAvailable.innerHTML === 'N')
    {                                                         //If the car is not available
        alert("Sorry, the car is not available now. Please try other cars.");
    }
    else
    {                                                       //If the car is available
        //var carPictureFileCol = 'Col0';               //Set the column numbers of each desired variable
        var carMakeCol = 'Col1';
        var carModelCol = 'Col2';
        var carYearCol = 'Col3';
        var carPricePerDayCol = 'Col8';
        
        var carMakeElement = carPosition + carMakeCol;
        var carMake = document.getElementById(carMakeElement).innerHTML;   //Get the car make or brand
        
        var carModelElement = carPosition + carModelCol;
        var carModel = document.getElementById(carModelElement).innerHTML;      //Get the car model
        var carPictureFile = carModel + '.jpg';                           //Get the car picture file
        
        var carYearElement = carPosition + carYearCol;
        var carYear = document.getElementById(carYearElement).innerHTML;        //Get the car year
        
        var carPricePerDayElement = carPosition + carPricePerDayCol;
        var carPricePerDay = document.getElementById(carPricePerDayElement).innerHTML;
        carPricePerDay = carPricePerDay.substring(1);    //Get the price per day without the dollar sign
        $.ajax({
                type: "GET",
                url: "rentalCarsCart.php",
                data: {"carPicFile": carPictureFile, "carBrand": carMake, "carMod": carModel, 
                       "carYearMan": carYear, "carPPD": carPricePerDay},
                dataType: "json",
                success: function()
                {
                    alert("You have successfully added this car to your rental cart");
                },
                error: function()
                {
                    alert("error in Ajax call to cart url");
                },
            });
    }
}

All values seem fine even according to the Apache Netbeans IDE output but it bombs out and displays the error alert always.即使根据 Apache Netbeans IDE Z78E6221F6393D1356Z81DB3 总是显示错误警报,所有值似乎都很好。 I have even debugged it inside Chrome developer tools using a breakpoint and step through method and all values are fine.我什至使用断点和逐步方法在 Chrome 开发人员工具中对其进行了调试,所有值都很好。 It is bombing out inside JQUERY.JS itself and not sending the GET request to the PHP backend.它在 JQUERY.JS 内部轰炸,而不是向 PHP 后端发送 GET 请求。 Does anyone have any ideas why?有谁知道为什么? I can't seem to find anything wrong with my code.我似乎找不到我的代码有什么问题。 If you can help me I would be grateful.如果你能帮助我,我将不胜感激。

My server side code is as follows:我的服务器端代码如下:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Rental Car Shopping Cart</title>
        <style>
            h1 {text-align: center;}
            button:hover{cursor: pointer;}
        </style>
    <h1>Car Reservation</h1>
    </head>
    <body>
        <?php
            header("Access-Control-Allow-Origin: *");
            
            $carPicture = $_GET['carPicFile']; // get car picture filename
            $carMake = $_GET['carBrand'];     // get the make of the car
            $carModel = $_GET['carMod'];      // get the car model
            $carYear = $_GET['carYearMan']; // Get the car year of manufacture
            $carPricePerDay = $_GET['carPPD'];    // Get the car price per day
            echo ('<div>');
            echo ($carPricePerDay);
            echo ('</div>');
        ?>
    </body>
</html>

Phil was right in the comment above that you don't need a dataType response from the PHP server side coding, so I removed it.菲尔在上面的评论中是对的,您不需要来自 PHP 服务器端编码的数据类型响应,所以我将其删除。 I also found out the other problem on the PHP backend although not shown in the above PHP code which was very basic originally but got more complicated later:我还发现了 PHP 后端的另一个问题,虽然上面的 PHP 代码中没有显示,这最初是非常基本的,但后来变得更加复杂:

Instead of if (($carPicture!=0) && ($carMake!=0) && ($carModel!=0) && ($carYear!=0) && 
  ($carPricePerDay!=0))
            {
                session_start();         //etc
Try:  
if (isset($carPicture) && isset($carMake) && isset($carModel) && isset($carYear) && isset($carPricePerDay))
            {
                session_start();           //etc

The reason being that when the variables are undefined they are also equal to zero or I am not sure why it decided to work with the bottom version.原因是当变量未定义时,它们也等于零,或者我不确定为什么它决定使用底层版本。 Positive logic seems to be better than negative logic for PHP. PHP 的正逻辑似乎优于负逻辑。 Thanks guys.多谢你们。

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

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