【发布时间】:2016-10-04 03:37:51
【问题描述】:
当我在文本框中使用/ 之类的字符时,我会在response 中返回一个empty string。我可以轻松地在我的 html 中检查 empty string,但我的问题是如何避免在我的 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
}
【问题讨论】:
-
/ 用作转义字符,使用控制台日志和 var_dump 检查您发送和接收的内容。
-
@Aschab 控制台打印出“(空字符串)”
-
console.log($(this).val()) 给你空字符串?
-
@Aschab 不...响应。输入值正确