PHP MySQL jquery JavaScript 如果输入为空,则不会重定向



我要实现的是:

如果输入为空,则用户不会被重定向到update.php页面,但如果输入的值不为空,则用户将被重定向到更新.php具有我正在谈论的输入中的相应ID。

到目前为止,我还没有实现这一点,这就是我到目前为止所拥有的:

我正在研究 fiLes 后端搜索.php它将隐藏的输入字段提供给我的索引.php页面,以及作为索引.php页面的主页。

以下是相关代码:

(后端搜索.php(

if(isset($_REQUEST['term'])){
$sql = "SELECT * FROM productslist WHERE brand LIKE ?";
if($stmt = mysqli_prepare($link, $sql)){
$param_term = '%'.$_REQUEST['term'].'%';    mysqli_stmt_bind_param($stmt, 's', $param_term);
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo '<p>'.$row['brand'].' - '.$row['wgtvol'].''.$row['unit'].'('.$row['generic'].') <input id="hiddenid" type="hidden" value="'.$row['id'].'" /></p>';
}
}
else{
echo '<p style="color: #FFFFFF; background: #b20101; font-weight: 700; "> No matching records found, brand name is still available.</p>';
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
mysqli_stmt_close($stmt);
}

以及我的索引中的相关代码.php:

<div class="search-box pull-left">
<input id="search" class="m-top m-bot" type="text" autocomplete="off" placeholder="Search by brand">
<div class="result" id="result"></div>
</div>

<script type="text/javascript">
$(document).ready(function(){
$('.search-box input[type="text"]').on("keyup input", function(){
var inputVal = $(this).val();
var resultDropdown = $(this).siblings(".result");
if(inputVal.length){
$.get("backend-search.php", {term: inputVal}).done(function(data){
resultDropdown.html(data);
});
} else{
resultDropdown.empty();
}
});
$(document).on("click", ".result p", function(){
$(this).parents(".search-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
var link = "view-or-add-stock-modal.php?id=";
console.log(link);
console.log($(this).text());
window.location = link + $(this).find('input[type=hidden]').val(); ;
});
});
</script>

请耐心等待我的代码编写,因为我是屏幕阅读器用户,我只依赖这项技术,因为我有视觉障碍。

尽管我已经尝试了我所做的这种变体,但它仍然不起作用:

<script type="text/javascript">
$(document).ready(function(){
$('.search-box input[type="text"]').on("keyup input", function(){
var inputVal = $(this).val();
var resultDropdown = $(this).siblings(".result");
if(inputVal.length){
$.get("backend-search.php", {term: inputVal}).done(function(data){
resultDropdown.html(data);
});
} else{
resultDropdown.empty();
}
});
$(document).on("click", ".result p", function(){
$(this).parents(".search-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
var link = "view-or-add-stock-modal.php?id=";
console.log(link);
console.log($(this).text());
window.location = link + $(this).find('input[type=hidden]').val(); ;
if ($("input:text").val().replace(/^s+|s+$/g, "").length == 0){
$('.search-box input[type="text"]').hide();
}
});
});
</script>

我愿意接受任何相关的解释,如果可以提供的话,我会修复。

提前感谢..如果版主即将将其标记为重复,我正在寻找一个具体的答案和准确的解释,所以请不要在没有完全阅读整篇文章的情况下立即标记它。

如问题评论中所述。

我会坚持您的第一个解决方案,只需更改一些代码

$(this).parent(".result").empty();
var link = "view-or-add-stock-modal.php?id=";
console.log(link);
console.log($(this).text());
var hiddenInputElem = $(this).find('input[type=hidden]');
//check first if the hidden Input Field is available and has a value
if(hiddenInputElem.length > 0 && hiddenInputElem.val().trim() != ""){
window.location = link + hiddenInputElem .val();
}

重定向在您更改window.location后立即发生。因此,您之前检查当前单击的<p>元素是否具有隐藏输入(以及它是否不为空(。否则不会发生重定向,用户将停留在当前页面上 - 在这种情况下,您可能希望显示一些其他信息。

只是一个旁注(我的意见( - 一切皆有可能 - 所以如果你想让你的代码做一些事情,它是可存档的 - 但你需要知道你想要什么:-(

编辑: 如果要保留用户的输入,只需删除此行

$(this).parents(".search-box").find('input[type="text"]').val($(this).text());

在这里,您将文本框输入设置为单击p的值 - 在每种情况下,这可能不是您想要的

最新更新