Checking input text fields if empty.

rsbypi4

New member
I want to check if my textbox is empty using php, and if empty execute the code of ajax.
PHP:
<input type="text" class="form-control" id="form3Name" name="Search" autocomplete="off" placeholder="Search..." required minlength="19">

      <?php $str_text = $_GET['form3Name'];?>
   <?php if (empty($str_text)):?>
      <h6>search box empty</h6>
      
       <?php elseif (!empty($str_text)):?>
         <script type="text/javascript">
         $(document).ready(function(){
            $("#form3Name").keyup(function(){
               var input = $(this).val();

               if(input != "")
                  {
                     $.ajax({
                     url:"search",
                     method:"POST",
                     data:{input:input},

                     success:function(data){
                        
                        $("#searchresult").html(data);
                     }
                  
                  } )
               }
               else if(input == "")
               {
                  header("refresh: 1;");
                  $("#searchresult").css("display","none");

               }
            });
         });
      </script>
   <?php endif;?>

1st run of my code can detect my textbox ix empty but if I insert any text "means not empty" the ajax code does not execute. I don't know why. can someone help me to fix this bug. Thank you in advance.
 
I want to check if my textbox is empty using php, and if empty execute the code of ajax.
PHP:
<input type="text" class="form-control" id="form3Name" name="Search" autocomplete="off" placeholder="Search..." required minlength="19">

      <?php $str_text = $_GET['form3Name'];?>
   <?php if (empty($str_text)):?>
      <h6>search box empty</h6>
     
       <?php elseif (!empty($str_text)):?>
         <script type="text/javascript">
         $(document).ready(function(){
            $("#form3Name").keyup(function(){
               var input = $(this).val();

               if(input != "")
                  {
                     $.ajax({
                     url:"search",
                     method:"POST",
                     data:{input:input},

                     success:function(data){
                       
                        $("#searchresult").html(data);
                     }
                 
                  } )
               }
               else if(input == "")
               {
                  header("refresh: 1;");
                  $("#searchresult").css("display","none");

               }
            });
         });
      </script>
   <?php endif;?>

1st run of my code can detect my textbox ix empty but if I insert any text "means not empty" the ajax code does not execute. I don't know why. can someone help me to fix this bug. Thank you in advance.
The AJAX call is only included in the PHP block that checks if the input is not empty. However, the AJAX functionality is dependent on the JavaScript being executed on the client side, which does not get triggered correctly due to the PHP script's execution flow.
Try this:
Code:
<input type="text" class="form-control" id="form3Name" name="Search" autocomplete="off" placeholder="Search..." required minlength="1">

<?php $str_text = $_GET['form3Name'] ?? ''; ?>
<?php if (empty($str_text)): ?>
    <h6>search box empty</h6>
<?php endif; ?>

<script type="text/javascript">
$(document).ready(function(){
    $("#form3Name").keyup(function(){
        var input = $(this).val();

        if(input != "") {
            $.ajax({
                url: "search",
                method: "POST",
                data: {input: input},
                success: function(data) {
                    $("#searchresult").html(data);
                }
            });
        } else {
            $("#searchresult").css("display", "none");
        }
    });
});
</script>
 
The AJAX call is only included in the PHP block that checks if the input is not empty. However, the AJAX functionality is dependent on the JavaScript being executed on the client side, which does not get triggered correctly due to the PHP script's execution flow.
Try this:
Code:
<input type="text" class="form-control" id="form3Name" name="Search" autocomplete="off" placeholder="Search..." required minlength="1">

<?php $str_text = $_GET['form3Name'] ?? ''; ?>
<?php if (empty($str_text)): ?>
    <h6>search box empty</h6>
<?php endif; ?>

<script type="text/javascript">
$(document).ready(function(){
    $("#form3Name").keyup(function(){
        var input = $(this).val();

        if(input != "") {
            $.ajax({
                url: "search",
                method: "POST",
                data: {input: input},
                success: function(data) {
                    $("#searchresult").html(data);
                }
            });
        } else {
            $("#searchresult").css("display", "none");
        }
    });
});
</script>
Thank you so much Moorcam, It's working properly.
 
Back
Top