Question

I am using PHP to parse XML from the end user, and then passing that to a Javascript function to handle. I only need a few of the values to be required and the rest of the values are optional. I have tried setting the values to empty strings but am getting an error for passing empty variables.

How can I fix this?

    <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>
Was it helpful?

Solution

Wrap parameters by using " or '

Like

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top