0

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>
1
  • can you tell which error you facing? Commented Sep 2, 2013 at 8:19

1 Answer 1

2

Wrap parameters by using " or '

Like

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that was the issue! Not sure why everyone down-rated it as it was a legitimate issue.
Welcome. If you know the answer at least say it in comment while down-vote the question. Stackoverflow recommend this. But don't know why, some are not follow that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.