2013-04-11 5 views
0

je voudrais apporter la réponse du service web php dans JSON javascript qui est obtenu à partir d'un appel de service web php voici mon codeenvoyer la réponse php webservice JSON javascript

<?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; 
} 
?> 

ici i obtenir Les noms d'organisation dans org $, bui de $ contiennent des bâtiments et des emplacements contiennent loc $ j'ai un formulaire ci-dessous ce 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> 

dans les organisations je voudrais comparer la valeur que j'ai choisi de combo box pour l'organisation avec le tableau json returened du service web php et retourner l'objet qui contient cette organisation particulière, vraiment ma question est de savoir comment puis-je stocker un tableau json dans une variable javascript ou quelque chose. merci

Répondre

3

Pourquoi ne pas mettre simplement une variable javascript avec le contenu JSON vous avez récupéré comme ceci:

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

Assurez-vous que vous échapper $response la bonne façon d'éviter de corrompre votre javascript.

+2

'json_encode (json_decode ($ response))' devrait l'assainir correctement. – Barmar

+0

monsieur j'ai édité mon code pouvez-vous plaese l'examiner – dscxz

+0

s'il vous plaît définir "l'imprimer". Je ne comprends pas votre question. Placez simplement ce code quelque part dans votre rendu html comme vous l'avez fait avec votre 'foreach' dans votre formulaire. De toute évidence, il doit s'agir d'une balise '

0

Cela aide-t-il?

<!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> 
Questions connexes