简体   繁体   中英

passing javascript array to php via jquery AJAX

I am having problems passing my javascript array to a php file. i know that the JS array has the correct users input data because I have tested this by using toString() and printing the array on my web page. My plan was to use send the JS array to my php script using AJAX's but I am new to using AJAX's so there is a good chance I am doing something wrong. I have look through a good lot of different posts of people having this same problem but everything i have tried has not worked so far. All I know at this point is the JS has data in the array fine but when I try to pass it to the php file via AJAX's the php script dose not receive it. i know this because I keep getting undefined variable errors. To be fully honest I'm not to sure if the problem in how I'm trying to pass the array to the php script or if it how I'm trying to request and assign the array values to variables on the php side. At the moment my code is as follows:

My 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';
            }
        }); 
    }

My 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.";
}?>

Just as extra info if its not obvious from my code I am trying to send user data from a html form to a php script that process it and uses it to query a MSSQL database. This function that I am working on now is the create database entry function.

You need to match the keys you send through AJAX:

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

with the POST array keys:

$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'];

Your code should look like this:

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

You are reading the wrong keys.

$assetID = $_POST['aID'];

Must be:

$assetID = $_POST['assitID'];

As per your sent object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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