Pages

Monday, March 28, 2011

Javascript - Like Google

index.php


<html>
<head>
<title>
Javascript Update
</title>

</head>
<body>
<select id="name">
    <option value="aura">Aura</option>
    <option value="luna">Luna</option>
    <option value="lady">Lady</option>
</select>
<input type="button" value="Get" onclick="getData()"/>
<br/>
<input type="button" value="Last Name" onclick="lastName()"/>
<input type="button" value="City" onclick="city()"/>
<input type="button" value="Age" onclick="age()"/>
<script>
function getData(){
    var script = document.createElement('script');
    var name = document.getElementById('name');
    var head = document.getElementsByTagName('head')[0];
    head.appendChild(script);
    script.setAttribute('src','hello.php?fname='+name.value);
    alert('');
}
function lastName(){
    alert(data.lname);
}
function city(){
    alert(data.city);
}
function age(){
    alert(data.age);
}


</script>


</body>
</html>


hello.php

<?php

    $data = array(
        'luna' => array('maya','denpasar',24),
        'lady' => array('gaga','new york',24),
         'aura' => array('kasih','jakarta',25),
    );
   
    $fname = $_GET['fname'];
   
    $output = 'var data = {};';
    $out = array();
   
    foreach($data as $key => $value){
        if(strcmp($fname, $key) == 0){
            foreach($value as $v){
                $out[] = $v;
               
            }
        }
    }
    $output .= 'data.lname = "'.$out[0].'";';
                $output .= 'data.city = "'.$out[1].'";';
                $output .= 'data.age = "'.$out[2].'";';
   
    header('Content-type: text/javascript');
    echo $output;
?>
   

No comments:

Post a Comment