简体   繁体   中英

send the php webservice json response to javascript

i would like to bring up the php web service response in json to javascript which is obtained from a php web service call here is my code

<?php
session_start();
$url='webservice url';
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($useridofuser));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
    $response=  curl_exec($ch);
    //echo('\n'."Server response : \n \n".$response);
    curl_close($ch);
    //parsing the json response from server
    $jsonde="$response";
    $_SESSION['json']=$jsonde;
 $org = array();
 $loc = array();
 $bui = array();
 $items = json_decode($response);
 foreach( $items as $each ){
 $loc[]=$each->location[0]->name;
 $bui[]=$each->location[0]->building[0];
 $org[]=$each->name;
 }
 ?> 

here i obtain organisation names in $org,$bui contain buildings ,and $loc contain locations i have a form below this code

    <sript>
    var json = "<?php echo $response ?>";
    alert(json);
 </script>
 <form>
 <label for="orgname">Organisation Name</label>
                    <select style="width: 305px;text-align:left ;"  name="category_id" id="category_id">
                    <option value="">Select</option>
                    <?php foreach($org as $key=>$val){?>
                    <option value="<?php echo $val; ?>"><?php echo $val;?></option>
 <?php
 }
 ?>                      </select>

                    <p>
        <label name="location">Location</label>

                     <select style="width: 305px;" name="category_id1" id="category_id1" onchange="ajax(this);">
                     <option value="">Select</option>
                     </select>
                    </p>
                    <p>
        <label for="building">Building</label>

                    <select style="width: 305px" name="category_id2" id="category_id2">
                    <option value="">Select</option>
                    </select>

in the organisations i would like to compare the value i have selected from combo box for organisation with the json array returened from the php web service and return the object that contain that particaular organisation,really my question is how can i store a json array into a javascript variable or some thing . thank you

Why not just setting a javascript variable with the json content you retrieved like this:

<?php 
    $response = "YOUR JSON RESPONSE";
?>
<script type="text/javascript">
    var json = "<?php echo json_encode(json_decode($response)) ?>";
</script>

Be sure that you escape $response the correct way to prevent corrupting your javascript.

Does this help?

<!doctype html>
<html>
<head>
<script>
function func1()
{
    try
    {
        addedVariable = addedVariable;
        alert('can only add the script once');
    }

    catch(ex)
    {
        var sc = newEl('script');
        sc.appendChild( newTxt('var addedVariable = 100;' ) );
        document.head.appendChild(sc);
        alert('script element with variable added');
    }
}

function func2()
{
    try
    {
        alert("value of 'addedVariable' = " + addedVariable);
    }
    catch(ex)
    {
        alert("hit the first btn, then try again");
    }
}
</script>
</head>
<body>
    <button id='btn1' onclick='func1();'>click Me 1st</button>
    <button id='btn2' onclick='func2();'>click me 2nd</button>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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