开发者

PHP - Fill pulldown with array results from web service method

开发者 https://www.devze.com 2023-04-10 14:36 出处:网络
I currently have a web service (C#) that returns an array of strings to my client in PHP. I want to fill a pulldown with the string elements in the array results.

I currently have a web service (C#) that returns an array of strings to my client in PHP.

I want to fill a pulldown with the string elements in the array results.

With the following code, no items appear in the pulldown.

<select name="name-list" id="name-list" class="pulldown" onchange="exportName();" >

        $client = new SoapClient("http://localhost/MyService.asmx?wsdl", array('features' => SOAP_SINGLE_ELEMENT_ARRAY开发者_如何学运维S));
        $res = $client->GetServiceArray()->GetServiceArrayResult;

        $array  = (array)$res->ArrayOfString;
        foreach($array as $val)
        {
          echo "<option value=\"" . $val . "\">" . $val . "</option>\n";
        }

    ?>

I have also done var_dump($res) to make sure the web service is returning the data. I can confirm that the values show in the page source.

It seems to be doing nothing on this line:

$array  = (array)$res->ArrayOfString;

Is there an alternative way of doing this?

Please can I have some advice on how to make the items appear.

Thank you.


It should be

foreach($array as $val)
{
    echo "<option value='$val'>$val</option>";
}


Most likely you need to do this

echo "<option value=\"" . $val . "\">" . $val . "</option>\n";

And don't forget to escape $val, because if there are any quotation characters inside, they will break your HTML. You can use

$val = addslashes($val);


Can it be $client->GetServiceArray()->GetServiceArrayResult is actually a function? It should be $client->GetServiceArray()->GetServiceArrayResult() then. In any way you should first check, if $res does contain any data: var_dump($res);

0

精彩评论

暂无评论...
验证码 换一张
取 消