通过jquery AJAX将javascript数组传递给php

时间:2014-08-29 10:07:37

标签: javascript php jquery arrays ajax

我在将javascript数组传递给php文件时遇到问题。我知道JS数组有正确的用户输入数据,因为我已经通过使用toString()并在我的网页上打印数组来测试它。我的计划是使用AJAX发送JS数组到我的PHP脚本但是我不熟悉使用AJAX,所以我很有可能做错了。我已经看了很多不同的帖子,这些人有同样的问题,但我尝试的一切都没有到目前为止。此时我所知道的是JS在数组中的数据很好,但是当我尝试通过AJAX将它传递给php文件时,php脚本无法接收它。我知道这是因为我一直得到未定义的变量错误。说实话,我不确定我是如何尝试将数组传递给php脚本的问题,或者我是如何尝试请求并将数组值分配给变量的在PHP方面。目前我的代码如下:

我的Javascript:

function createAsset(str, str, str, str, str, str, str, str, str)
    {
        var aID = assetID.value;
        var aName = assetName.value;
        var pPrice = purchasedPrice.value;
        var pDate = purchasedDate.value;
        var supp = supplier.value;
        var cValue = currentValue.value;
        var aOwner = actualOwner.value;
        var wEdate = warrantyExpiryDate.value;
        var dDate = destroyedDate.value;

        //document.write(aID);
        //var dataObject = new Array()
        //dataObject[0] = aID;
        //dataObject[1] = aName;
        //dataObject[2] = pPrice;
        //dataObject[3] = pDate;
        //dataObject[4] = supp;
        //dataObject[5] = cValue; 
        //dataObject[6] = aOwner;
        //dataObject[7] = wEdate;
        //dataObject[8] = dDate;
        //dataObject.toString();
        //document.getElementById("demo").innerHTML = dataObject;

        var dataObject = { assitID: aID,
                           assitName: aName,
                           purchasedPrice: pPrice,
                           purchasedDate: pDate,
                           supplier: supp,
                           currentValue:  cValue, 
                           actualOwner: aOwner,
                           warrantyExpiryDate: wEdate,
                           destroyedDate: dDate };  

         $.ajax
         ({
            type: "POST",
            url: "create_asset_v1.0.php",
            data: dataObject, 
            cache: false,
            success: function()
            {
                alert("OK");
                location.reload(true);
                //window.location = 'create_asset_v1.0.php';
            }
        }); 
    }

我的PHP:

<?php
// Get Create form values and assign them to local variables.
$assetID = $_POST['aID'];
$assetName = $_POST['aName'];
$purchasedPrice = $_POST['pPrice'];
$purchasedDate = $_POST['pDate'];
$supplier = $_POST['supp'];
$currentValue = $_POST['cValue'];
$actualOwner = $_POST['aOwner'];
$warrantyExpiryDate = $_POST['wEdate'];
$destroyedDate = $_POST['dDate'];

// Connect to the SQL server.
$server='PC028\ZIRCONASSETS';               //serverName\instanceName
$connectinfo=array("Database"=>"zirconAssetsDB");
$conn=sqlsrv_connect($server,$connectinfo);

if($conn)
{
    echo "Connection established.<br/><br/>";
}
else
{
    echo "Connection couldn't be established.<br/><br/>";
    die(print_r( sqlsrv_errors(), true));
} 

// Query the database to INSERT record.
$sql = "INSERT INTO dbo.inHouseAssets 
        (Asset_ID, Asset_Name, Perchased_Price, Date_Perchased, Supplier, Current_Value, Actual_Owner,Worranty_Expiry_Date, Destroyed_Date) 
        VALUES 
        (?, ?, ?, ?, ?, ?, ?, ?, ?)";

$params = array($assetID, $assetName, $purchasedPrice, $purchasedDate, $supplier, $currentValue, $actualOwner, $warrantyExpiryDate, $destroyedDate);

// Do not send query database if one or more field have no value.
if($assetID && $assetName && $purchasedPrice && $purchasedDate && $supplier && $currentValue && $actualOwner && $warrantyExpiryDate && $destroyedDate != '')
{
    $result = sqlsrv_query( $conn, $sql, $params);

    // Check if query was executed with no errors.
    if( $result === false ) 
    {
        // If errors occurred print out SQL console data.  
        if( ($errors = sqlsrv_errors() ) != null) 
        {
            foreach( $errors as $error ) 
            {
                echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br/>";
                echo "code: ".$error[ 'code']."<br/>";
                echo "message: ".$error[ 'message']."<br/>";
            }
        }
    }
    else
    {
        echo "Record Created!<br/>";
    }
}

// Close server connection
sqlsrv_close( $conn );
if($conn)
{
    echo "<br/>Connection still established.";
}
else
{
    echo "<br/>Connection closed.";
}?>

就像我的代码中不明显的额外信息一样,我试图将用户数据从html表单发送到处理它并用它来查询MSSQL数据库的php脚本。我现在正在处理的这个函数是create database entry函数。

2 个答案:

答案 0 :(得分:2)

您需要匹配通过AJAX发送的密钥:

var dataObject = { assitID: aID,
                           assitName: aName,
                           purchasedPrice: pPrice,
                           purchasedDate: pDate,
                           supplier: supp,
                           currentValue:  cValue, 
                           actualOwner: aOwner,
                           warrantyExpiryDate: wEdate,
                           destroyedDate: dDate };

使用POST数组键:

$assetID = $_POST['aID'];
$assetName = $_POST['aName'];
$purchasedPrice = $_POST['pPrice'];
$purchasedDate = $_POST['pDate'];
$supplier = $_POST['supp'];
$currentValue = $_POST['cValue'];
$actualOwner = $_POST['aOwner'];
$warrantyExpiryDate = $_POST['wEdate'];
$destroyedDate = $_POST['dDate'];

您的代码应如下所示:

$assetID = $_POST['assitID'];
$assetName = $_POST['assitName'];
$purchasedPrice = $_POST['purchasedPrice'];
...

答案 1 :(得分:1)

您正在读错键。

$assetID = $_POST['aID'];

必须:

$assetID = $_POST['assitID'];

根据您发送的对象。