1 Answer
  • 1
Votes
name
name Punditsdkoslkdosdkoskdo

Ajax get value from php

Ajax


$(document).ready(function(){


 $("#diklat").change(function(){ 

 var diklat = $("#diklat").val(); 

 $.ajax({ 

 url: "function.php", 

 data: {'action': 'diklat'},

 cache: false, success: function(msg){

 $("#angkatan").html(msg); 

 } 

 });

 });

PHP


$get_action = $_GET['action'];

if($get_action=='diklat'){

 $diklat = $_GET['diklat'];

 $angkatan = mysql_query("SELECT id,name FROM batches WHERE IdMasterDiklat='$diklat' order by id"); 

 echo "";

 while($p = mysql_fetch_array($angkatan)){

 echo " "; 

 } 

 }

The value didnt include in my ajax, ajax only read echo. how to get that value

You should pass 2 variables, one action and another id (diklat)

Ajax:

$(document).ready(function(){
          $("#diklat").on('change', function(){
            var diklat = $("#diklat").val();
            $.ajax({
                type: "POST",
                url: "function.php",
                data: {'action': 'diklat', 'diklat':diklat},
                cache: false,
                success: function(msg){
                    $("#angkatan").html(msg);
                }
            });
          });

PHP

$action = isset($_POST['action']) ? $_POST['action'] : '';

if ($action == 'diklat')
{
    $diklat   = isset($_POST['diklat']) ? $_POST['diklat'] : '';
    $angkatan = mysql_query("SELECT id, name FROM batches WHERE IdMasterDiklat='$diklat' order by id");
    echo "<option>-- Pilih Angkatan --</option>";
    while($p = mysql_fetch_array($angkatan))
    {
        echo "<option value="".$p['id']."">".$p['name']."</option>
";
    }
}
  • 1
Reply Report