简体   繁体   English

在两个texbox中比较mysql数据库中的值而不提交表单

[英]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. keypress事件正在处理此问题,将值从jour_info.php页面发送到get_sid.php页面。 I have two files 我有两个档案

  1. jour_info.php jour_info.php
  2. get_sid.php 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. 您还需要修复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. if ($check_num_rows == 0)检查不应该在while循环内,因为没有行时循环将永远不会执行。 You don't even need a loop at all -- I assume these queries can only return 1 row. 您甚至根本不需要循环-我假设这些查询只能返回1行。 So just call mysql_fetch_array() -- if it returns the row, you have a match, otherwise there's no match and you should echo '' . 因此,只需调用mysql_fetch_array() -如果返回行,则说明您有匹配项,否则没有匹配项,应echo ''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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