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

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.

douyinzha5820
douyinzha5820 你在php端使用了错误的键,你必须使用php端的json对象的关键部分来获取post值($_POST['assitID'],$_POST['assitName']...)
大约 6 年之前 回复
dqmq0654
dqmq0654 我假设我传入函数的每个刺都必须被声明为函数的参数。
大约 6 年之前 回复
doulei8861
doulei8861 为什么你在这里多次启动一个具有相同属性的函数createAsset(str,str,str,str,str,str,str,str,str)......???
大约 6 年之前 回复
dtf76989
dtf76989 您正在使用的数组键错误。
大约 6 年之前 回复

2个回答



您正在读错键。 </ p>

  $ assetID = $ _POST ['aID']; 
</ code> </ pre>

必须:</ p> \ n

  $ assetID = $ _POST ['assitID']; 
</ code> </ pre>

根据您发送的对象。 </ p>
</ div>

展开原文

原文

You are reading the wrong keys.

$assetID = $_POST['aID'];

Must be:

$assetID = $_POST['assitID'];

As per your sent object.



您需要匹配通过AJAX发送的密钥:</ p>

  var dataObject =  {assitID:aID,
assitName:aName,
purchasePrice:pPrice,
purchaseDate:pDate,
supplier:supp,
currentValue:cValue,
actualOwner:aOwner,
warrantyExpiryDate:wEdate,
destroyedDate :dDate};
</ code> </ pre>

使用POST数组键:</ p>

  $ assetID = $ _POST ['aID'  ]; 
$ assetName = $ _POST ['aName'];
$ purchasePrice = $ _POST ['pPrice'];
$ purchaseDate = $ _POST ['pDate'];
$ supplier = $ _POST [' supp'];
$ currentValue = $ _POST ['cValue'];
$ actualOwner = $ _POST ['aOwner'];
$ warrantyExpiryDate = $ _POST ['wEdate'];
$ destroyedDate = $ _POST ['dDate'];
</ code> </ pre>

您的代码应如下所示:</ p>

  $ assetID = $  _POST ['assitID']; 
$ assetName = $ _POST ['assitName'];
$ buyingPrice = $ _POST ['soldPrice'];
...
</ code> </ pre>
</ DIV>

展开原文

原文

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

drphfy1198
drphfy1198 非常感谢你Mihai它现在工作,现在你已经说过,我可以看到我肯定有错误的结束。
大约 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐