doushang1880 2018-10-26 03:05
浏览 73
已采纳

php mysql jquery javascript如果输入为空则不重定向

what i'm trying to achieve is this:

if the input is empty, the user will not be redirected to the update.php page, but if the value of the input is not empty, the user will be redirected to the update.php with the corresponding id from the input that i am talking about..

so far i was not abLe to achieve this, and this is what i have so far:

i am working on the fiLes backend-search.php that feeds the hidden input field to my index.php page, and the main page which is the index.php page..

here are the relewvant codes:

(backend-search.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);
 }

and the relevant codes from my index.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>

please bear with my code-writing as i am a screenreader user and i only rely on this technology as i have visual disability..

though i have tried this variation i have made, it still doesn't work:

     <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>

i am willing to accept any relevant explanation and fix if one can provide..

thanks in advance.. and please if the moderators are about to flag this as duplicate, i am looking for an specific answer and precise explanation so please do not flag this right away without completely reading the entire post.

  • 写回答

1条回答 默认 最新

  • doudiyu1639 2018-10-26 12:04
    关注

    As clarified in the question comments.

    I would stick to your first solution and just change some code

    $(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();
    }
    

    The redirect happens immediately after you change window.location. Therefore you check before if the current clicked <p> Element has an hidden-Input (and if it's not empty). Otherwise no redirect will happen and the user stays on the current page - maybe you want to show some additional Info in this case.

    And just a sidenote (my opinion) - Everything is possible - so if you want your code to do something it's archivable - but you need to know what you want :-)

    Edit: If you want to keep the user's input just remove this line

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

    Here you set the the textbox input to the value of the clicked p - in every case, and that's maybe not what you want

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站