Monday, 26 August 2013

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

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
jour_info.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?

No comments:

Post a Comment