简体   繁体   English

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

[英]passing javascript array to php via jquery AJAX

I am having problems passing my javascript array to a php file. 我在将我的JavaScript数组传递到php文件时遇到问题。 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. 我知道JS数组具有正确的用户输入数据,因为我已经通过使用toString()并将其打印在我的网页上进行了测试。 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. 我的计划是使用AJAX将JS数组发送到我的php脚本,但是我对使用AJAX还是陌生的,所以很有可能我做错了什么。 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. 我现在只知道JS数组中的数据很好,但是当我尝试通过AJAX将其传递给php文件时,php脚本不会接收到它。 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. 老实说,我不确定是否要尝试将数组传递给php脚本,还是要如何将数组值分配给php端变量的问题。 At the moment my code is as follows: 目前,我的代码如下:

My Javascript: 我的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:

<?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. 就像额外的信息(如果它在我的代码中不明显)一样,我试图将用户数据从html表单发送到php脚本,该脚本对其进行处理并使用它来查询MSSQL数据库。 This function that I am working on now is the create database entry function. 我现在正在使用的此功能是创建数据库条目功能。

You need to match the keys you send through AJAX: 您需要匹配通过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: 使用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'];

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. 根据您发送的对象。

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

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