2016-08-07 22 views
0
$(document).ready(function() { 
    $(".submit").click(function(event) { 
     event.preventDefault(); 
     var user_name = $("input#name").val(); 
     var password = $("input#pwd").val(); 
     jQuery.ajax({ 
      type: "POST", 
      url: "<?php echo base_url(); ?>" + "index.php/ajax_post_controller/user_data_submit", 
      dataType: 'json', 
      data: { 
       name: user_name, 
       pwd: password 
      }, 
      success: function(res) { 
       if (res) { 
        // Show Entered Value 
        jQuery("div#result").show(); 
        jQuery("div#value").html(res.name); 
        jQuery("div#value_pwd").html(res.pwd); 
       } 
      } 
     }); 
    }); 
}); 

// the function of controller that the url: contains 
public function user_data_submit() { 
    $data = array(
     'username' => $this - > input - > post('name'), 
     'pwd' => $this - > input - > post('pwd') 
    ); 
    echo json_encode($data); 
} 

der Code wird verwendet, um Wert aus dem Eingabefeld zu nehmen und es auf der Grundlage von ID auf dem HTML-Feed. aber mein Problem ist, dass ich es von der auserwählten Abfrage füttern muss, die Wert von der Datenbank holen, die bereits die Daten hat.Wie wird der Wert aus der Datenbank mit Ajax

Antwort

0

https://openenergymonitor.org/emon/node/107

$(function() 
{ 
//----------------------------------------------------------------------- 
// 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/ 
//----------------------------------------------------------------------- 
$.ajax({          
    url: 'api.php',     //the script to call to get data   
    data: "",      //you can insert url argumnets here to pass to api.php 
            //for example "id=5&parent=6" 
    dataType: 'json',    //data format  
    success: function(data)   //on recieve of reply 
    { 
    var id = data[0];    //get id 
    var vname = data[1];   //get name 
    //-------------------------------------------------------------------- 
    // 3) Update html content 
    //-------------------------------------------------------------------- 
    $('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname); //Set output element html 
    //recommend reading up on jquery selectors they are awesome 
    // http://api.jquery.com/category/selectors/ 
    } 
}); 
}); 
</script> 

Dies ist nicht die edxact Antwort auf meine Frage basiert, aber es ähnelt genau die Antwort, die ich will, die die immer ID und Name aus der Datenbank