2016-10-04 8 views
1

/のような文字をテキストボックスに使用すると、empty stringresponseに返されます。私は簡単に私のHTMLのempty stringのチェックを行うことができますが、私の質問は、私のphpコードに送信されてから空の文字列を避けることができますか?空の文字列がPHPで返されないようにする

$("#search").keyup(function(){ 
    var newVal = $(this).val(); 
    if(!newVal==""){ 
     $.ajax({ 
       type : 'get', 
       dataType: 'html', 
       url : '/searchpost/' + newVal , 
       success : function(response) { 
        console.log(response); 
       } 
     }); 
    } 
}); 

public function searchpost() { 

     $q = $this->uri->segment(3); 
     if(!$q) die(); 

     $string = trim(strip_tags($q)); 
     $db_string = urldecode($string); 

     $this->db->select("postID, post_title, post_url, post_status"); 
     $this->db->like("post_title", $db_string); 
     $this->db->or_like("post_url", $db_string); 

     $posts = $this->db->get('posts', 10); 

     if(!count($posts->result())) { 
      die('Nothing to display'); 
     } 

     ?> 

     <ul> 
     <?php 
     foreach($posts->result() as $m) : 
      if($m->post_status != 'active' OR empty($m->post_title)) continue; 
     ?> 
     <li> 
      <a href="<?php echo '/posts/'.$m->postID.'/'.url_title($m->post_title); ?>" class="url-post-title" style="font-size:14px;"><?php echo $m->post_url; ?></a> 
      </a> 
     </li> 
     <?php endforeach; ?> 
     </ul> 

     <?php 

    } 
+0

/あなたが送信し、recievingしているかどうか確認するために、コンソールログとのvar_dumpを使用し、エスケープ文字として使用されています。 – Aschab

+0

@Aschabコンソール出力 "(空の文字列)" – user4756836

+0

console.log($(this).val())は空文字列を返しますか? – Aschab

答えて

1

あなたのjsの関数を変更し、要求を送信する前に値をエンコードするためにencodeURIComponentを使用することができます。

$("#search").keyup(function(){ 
    var newVal = $(this).val(); 
    if(newVal !== undefined && newVal.length > 0){ 
     $.ajax({ 
       type : 'get', 
       dataType: 'html', 
       url : '/searchpost/' + encodeURIComponent(newVal) , 
       success : function(response) { 
        console.log(response); 
       } 
     }); 
    } 
}) 

次に、あなたのPHP関数で今URLエンコード値を処理する必要があります。

ので、代わりの:

$db_string = urldecode($string); 

使用rawurldecode

$db_string = rawurldecode($string); 
関連する問題