1
/
のような文字をテキストボックスに使用すると、empty string
がresponse
に返されます。私は簡単に私の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
}
/あなたが送信し、recievingしているかどうか確認するために、コンソールログとのvar_dumpを使用し、エスケープ文字として使用されています。 – Aschab
@Aschabコンソール出力 "(空の文字列)" – user4756836
console.log($(this).val())は空文字列を返しますか? – Aschab