简体   繁体   中英

Compare values from mysql database in two texbox without submitting the form

I want to check the values from either one of the two textboxes text boxes that matches the value with the other without submitting the form. The keypress event is handling this, sending the value from jour_info.php page to get_sid.php page. I have two files

  1. jour_info.php
  2. get_sid.php

The code in the first file

 <form method="post" name="journ_form" >
P-ISSN/ISBN<br/><input name="printissn" id="printissn_input" type="text" value="">
                <input type="text" name="pissnsid"  id="pissnsid" style="width: 30px;" autocomplete="off" value="">

                <span style="color: red;" id="feedback"></span>
 </form>

   <script type="text/javascript">
    $(document).ready(function(){
    $('#feedback').load('get_sid.php').show();

    $('#printissn_input').keyup(function(){

    $.post('get_sid.php', {printissn: journ_form.printissn.value},
      function(result) {
      document.getElementById('pissnsid').value = result;
   }); 
  });


   $('#pissnsid').keyup(function(){
    $.post('get_sid.php', {pissnsid: journ_form.pissnsid.value},
      function(result) {
        document.getElementById('printissn_input').value = result;
     });
   });
  });

The code in the second file

    <?php
    include 'auth.php';

     $printissn = $_POST['printissn'];
     $pissnsid = $_POST['pissnsid'];

     if($printissn){
      $check = mysql_query("SELECT printissn, pissnsid FROM jour_entries WHERE    printissn='$printissn'");
      $check_num_rows = mysql_num_rows($check);
      while($row = mysql_fetch_array($check)){
        $get_printissnsid = $row['pissnsid'];
     if($check_num_rows == 0){
      echo '';
      } else if($check_num_rows == 1){
          echo $get_printissnsid; 
       }
        }
     } else if($pissnsid){
    $check = mysql_query("SELECT printissn, pissnsid FROM jour_entries WHERE  pissnsid='$pissnsid'");
     $check_num_rows = mysql_num_rows($check);
     while($row = mysql_fetch_array($check)){
    $get_printissn = $row['printissn'];

   if($check_num_rows == 0){
    echo '';
   } else if($check_num_rows == 1){
     echo $get_printissn;    
   } 
    }
   } 
   ?>

Now everything is working fine, the problem is when a value is entered in the first text box its showing the corresponding match in the second text box. But in case if the value doesn't match with the two fields and user needs to enter manually the data, the problem arises. When there is no match and user enters a value in first textbox, then the second textbox value disappears. How to solve that?

Check whether the result is valid before putting it in the other input field:

function(result) {
    if (result != '') {   
        document.getElementById('pissnsid').value = result;
    }
}); 

You also need to fix your PHP. The if ($check_num_rows == 0) check should not be inside the while loops, because the loops will never execute when there are no rows. You don't even need a loop at all -- I assume these queries can only return 1 row. So just call mysql_fetch_array() -- if it returns the row, you have a match, otherwise there's no match and you should echo '' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM