dpthuyh1678 2014-08-29 10:07
浏览 108

通过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.

  • 写回答

2条回答

  • drvvepadi289421028 2014-08-29 10:13
    关注

    You are reading the wrong keys.

    $assetID = $_POST['aID'];
    

    Must be:

    $assetID = $_POST['assitID'];
    

    As per your sent object.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题