簡體   English   中英

在兩個texbox中比較mysql數據庫中的值而不提交表單

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

我想檢查兩個文本框之一中與另一個值匹配的文本框中的值,而不提交表單。 keypress事件正在處理此問題,將值從jour_info.php頁面發送到get_sid.php頁面。 我有兩個檔案

  1. jour_info.php
  2. get_sid.php

第一個文件中的代碼

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

第二個文件中的代碼

    <?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;    
   } 
    }
   } 
   ?>

現在一切正常,問題是當在第一個文本框中輸入一個值時,在第二個文本框中顯示相應的匹配項。 但是,如果該值與兩個字段不匹配,並且用戶需要手動輸入數據,則會出現問題。 如果沒有匹配項,並且用戶在第一個文本框中輸入值,則第二個文本框值將消失。 怎么解決呢?

將結果放入其他輸入字段之前,請檢查結果是否有效:

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

您還需要修復PHP。 if ($check_num_rows == 0)檢查不應該在while循環內,因為沒有行時循環將永遠不會執行。 您甚至根本不需要循環-我假設這些查詢只能返回1行。 因此,只需調用mysql_fetch_array() -如果返回行,則說明您有匹配項,否則沒有匹配項,應echo ''

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM