将Null值从PHP传递给Javascript函数

时间:2013-09-02 08:09:10

标签: php javascript html ajax optional-parameters

我使用PHP从最终用户解析XML,然后将其传递给Javascript函数进行处理。我只需要一些值,其余的值是可选的。我已经尝试将值设置为空字符串但是传递空变量时出错。

我该如何解决这个问题?

    <script type="text/javascript">
    function CreateDevice(id, type, name, datacenter, os, whmcsid, ipmihost, ipmiuser, ipmipass, remotehost, remoteuser, remotepass)
    {
        $.ajax({
            type: "POST",
            url: "handler.php",
            data: { 'action_type': 'create_device',
                    device_id: id,
                    device_type: type,
                    device_name: name,
                    datacenter: datacenter,
                    operating_system: os,
                    whmcs_id: whmcsid,
                    ipmi_host: ipmihost,
                    ipmi_user: ipmiuser,
                    ipmi_pass: ipmipass,
                    remote_host: remotehost,
                    remote_user: remoteuser,
                    remote_pass: remotepass},
            dataType: 'json',
            success: function(data)
            {
                alert("SUCCESS!");
            }
        });
    }
<?
    if($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        if (empty($_POST["device_xml"]))
        {
            echo 'No Data Posted';
            exit;
        }

        $device_xml = $_POST["device_xml"];
        $devices = simplexml_load_string($device_xml);

        foreach ($devices->device as $device)
        {
            $device_id = $device->id;
            $device_name = $device->name;
            $device_type = $device->type;
            $datacenter = $device->datacenter;

            $os = $device->os;
            $whmcs = $device->whmcs;
            $ipmihost = $device->ipmihost;
            $ipmiuser = $device->ipmiuser;
            $ipmipass = $device->ipmipass;
            $remotehost = $device->remotehost;
            $remoteuser = $device->remoteuser;
            $remotepass = $device->remotepass;

            if ($device_id == "") { echo "Error: Device ID Must be Specified"; exit; }
            if ($device_name == "") { echo "Error: Device Name Must be Specified"; exit; }
            if ($device_type == "") { echo "Error: Device Type Must be Specified"; exit; }
            if ($datacenter == "") { echo "Error: Datacenter Must be Specified"; exit; }

            if (!$os) { $os = ""; }
            if (!$whmcs) { $whmcs = ""; }
            if (!$ipmihost) { $ipmihost = ""; }
            if (!$ipmiuser) { $ipmiuser = ""; }
            if (!$ipmipass) { $ipmipass = ""; }
            if (!$remotehost) { $remotehost = ""; }
            if (!$remoteuser) { $remoteuser = ""; }
            if (!$remotepass) { $remotepass = ""; }

            echo "CreateDevice(" . $device_id . "," . $device_name . "," . $device_type . "," . $datacenter . "," . $os . "," . $whmcs . "," .
                                    $ipmihost . "," . $ipmiuser . "," . $ipmipass . "," . $remotehost . "," . $remoteuser . "," . $remotepass . ");";
        }
    }

    ?>
    </script>

1 个答案:

答案 0 :(得分:0)

使用"'

换算参数

喜欢

CreateDevice('". $device_id."','".$device_name."','".$device_type."','".$datacenter."','".$os."','".$whmcs."','.$ipmihost."','".$ipmiuser."','".$ipmipass."','".$remotehost."','".$remoteuser."','".$remotepass."');";