2016-04-05 54 views
0

次のコードは、CodeIgniter jQuery Ajaxメソッドを使用してデータをコントローラに送信します。送信時にヒットした後、値が表示されます。私は、ユーザーが入力した値を表示するだけでなく、データベースに値が入力されている場合は、この値をデータベースのデータと比較したいと考えています値が存在しない場合はと表示されます。 。私は値をチェックするモデルを持っていますが、私はコントローラでどのように使用するのか分かりません。値の入力に応じたフォームの表示

ご協力いただきありがとうございます。

コントローラ

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
public function __construct() 
{ 
    parent::__construct(); 

    $this->load->model('get_value'); 

} 
class Ajax_Post_Controller extends CI_Controller { 

// Show view Page 
public function index(){ 
$this->load->view("ajax_post_view"); 
} 

// This function call from AJAX 
public function user_data_submit() { 
$data = array(
    'username' => $this->input->post('name') 

    ); 

echo json_encode($data); 
} 
} 

ビュー

<script type="text/javascript"> 

// Ajax post 
$(document).ready(function() { 
$(".submit").click(function(event) { 
event.preventDefault(); 
var user_name = $("input#name").val(); 

jQuery.ajax({ 
type: "POST", 
url: "<?php echo base_url(); ?>" + "index.php/ajax_post_controller/user_data_submit", 
dataType: 'json', 
data: {name: user_name}, 
success: function(res) { 
if (res) 
{ 
// Show Entered Value 
jQuery("div#result").show(); 
jQuery("div#value").html(res.username); 

} 
} 
}); 
}); 
}); 
</script> 
<?php 

// Form Open 
echo form_open(); 

// Name Field 
echo form_label('User Name'); 
$data_name = array(
'name' => 'name', 
'class' => 'input_box', 
'placeholder' => 'Please Enter Name', 
'id' => 'name' 
); 
echo form_input($data_name); 
echo "<br>"; 

?> 
    </div> 
    <div id="form_button"> 
    <?php echo form_submit('submit', 'Submit', "class='submit'"); ?> 
    </div> 
    <?php 
    // Form Close 
    echo form_close(); ?> 

モデル

function get_search_form() { 
$match = $this->input->post('name'); 
$this->db->where('name',$match); 
$query = $this->db->get('transaction'); 
return $query->result(); 

答えて

0

あなたは012を使用してモデルをロードしたら、

class User extends CI_Model{ 

    function __construct(){ 
    parent::__construct(); 
    } 

    function checkUser($id){ 
     // here some code to check if the user exists in db 
    } 


    function addUser($data){ 
     // here some code to add users 
    } 

    // ... and here other methods to do the other 'crud' operations 
} 

そして、あなたはあなたのためにそれを呼び出すことができます あなたのコントローラ内の前のモデルをロード: あなたは、我々はこの単純なCodeIgniterのUserモデルを持っているとしましょう、あなたのモデルに

を参照されthis->get_valueを使用することができますこの例に示すようにユーザーが存在するかどうかを確認する例:

class Home extends CI_Controller{ 

    function __construct(){ 
     parent::__construct(); 

     // load the user model 
     $this->load->model('User'); 
    } 

    function checkUser($id){ 
     // now make use of the 'User model' 
     if(this->User->checkUser($id)){ 
     // display the appropriate message 
     } 

    } 

} 

希望すると便利です。

+0

私の問題は、ポスト値を送信する関数にcheckuser関数を統合する方法です。ユーザーがチェックを完了し、入力された値が表示されることがあります... – baptpro

+0

ユーザーが「check sell」をクリックするとjquery '' $ .post( 'localhost/mysite/home.php/checkuser/1'、{}、function(serverResponse){ console.logのように、 '' $。post ' (serverResponse); }); '' – Ismail

関連する問題