Page 1 of 1

onChange in PHP Code not Working

Posted: Mon Dec 12, 2011 11:01 pm
by davidalwis
Hi All,

In this time, I would like to share about My PHP code.
This PHP code function for when User input the Badge ID, then textbox_name and textbox_dept will show automatically from database.
But now I have a problem with this. When I want to input Badge ID, textbox_name and textbox_dept not show the details.

1. index.php
<script>
function getXMLHTTP() {
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}

return xmlhttp;
}

function getCity(strURL) {

var req = getXMLHTTP();

if (req) {

req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('citydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>

<link rel="stylesheet" media="screen" href="style.css"/>
<html>
<head>
<title>Search the Database</title>
</head>

<body>
<form action="#" method="post" autocomplete="off">
<?php
echo
"
Badge ID <input type='text' name='badgeid' onChange='getCity('search.php?badgeid='+this.value)' maxlength='6' onkeyup='javascript:this.value=this.value.toUpperCase();'/><br/>
<div id='citydiv'>
Employee Name <input name='name' type='text' readonly><br>
Department <input type='text' readonly name='dept'>
</div>
"
?>
</form>
</body>
</html>
2. search.php
<?php
ini_set( "display_errors", 0);
$badgeid=$_REQUEST['badgeid'];
mysql_connect ("localhost", "root","") or die (mysql_error());
mysql_select_db ("sei");

$sql = mysql_query("select * from employee where badgeid like '%$badgeid%'");

if ($row = mysql_fetch_array($sql)){

echo "Employee Name <input type='text' name='name' value='".$row['name']."'";

echo '<br/><br/>';
echo "Department <input type='text' name='dept' value='".$row['dept']."'";
echo '<br/><br/>';
}
else
{
echo "Employee Name <input type='text' readonly><br>";
echo "Department <input type='text' readonly>";
}
?>
3. config.php
<?php
$dbUser = "root";
$dbPass = "";
$dbName = "sei";
$dbHost = "localhost";

mysql_connect($dbHost, $dbUser, $dbPass) or die(mysql_error());
mysql_select_db($dbName);
?>
* Before, It working properly if I make the code without PHP like below :
<form action="search.php" method="post" autocomplete="off">

Badge ID <input type="text" name="badgeid" onChange="getCity('search.php?badgeid='+this.value)" maxlength="6"/><br />
<div id="citydiv">
Employee Name
<input name="name" type="text" readonly><br>
Department
<input type="text" readonly name="dept">
</div></td>

</form>
Please help me to solve this problems.
Appreciate with your helps :D .

Thank you

Re: onChange in PHP Code not Working

Posted: Mon Dec 12, 2011 11:56 pm
by social_experiment
The php code returns no error so it has to be related to the javascript. Why do you use the php to generate the textbox?

Re: onChange in PHP Code not Working

Posted: Tue Dec 13, 2011 12:27 am
by davidalwis
Reason I'm using the PHP not to generate, but there are some function that I used in there.
Please help Me!

Re: onChange in PHP Code not Working

Posted: Tue Dec 13, 2011 1:21 am
by social_experiment

Code: Select all

<?php
//
echo 'Badge ID <input type="text" name="badgeid" onChange="getCity(\'search.php?badgeid=\'+this.value)" maxlength="6" 
onkeyup="javascript:this.value=this.value.toUpperCase();" /></br />
<div id="citydiv">
Employee Name <input name="name" type="text" readyonly /><br />
Department <input type="text" readonly name="dept" />
</div>';
?>
Try the snippet above; it encases the string in single quotes;
Hth

Re: onChange in PHP Code not Working

Posted: Tue Dec 13, 2011 2:09 am
by davidalwis
social_experiment wrote:

Code: Select all

<?php
//
echo 'Badge ID <input type="text" name="badgeid" onChange="getCity(\'search.php?badgeid=\'+this.value)" maxlength="6" 
onkeyup="javascript:this.value=this.value.toUpperCase();" /></br />
<div id="citydiv">
Employee Name <input name="name" type="text" readyonly /><br />
Department <input type="text" readonly name="dept" />
</div>';
?>
Try the snippet above; it encases the string in single quotes;
Hth
Hi... Thanks a lot for your helps.
May I ask for 1 question again ?
It's all about my real web program.
I have tried your solution and it's work and now I'm confused how to set the value textbox_name and textbox_dept in many tables.

This is my program :

1. index.php
Example : I type 3 person that will be overtime.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Add Overtime</title>
</head>

Add Overtime
<form name="form" action="submit-employee-users.php" method=post autocomplete="off">
Enter number of employee
<input class="text sign" type="text" id="jum" name="jum" maxlength="3">
<input type="submit" name="submit" value="Submit" />
</form>
</html>
Image

2. submit-employee-user.php
Add Overtime<br>
Enter employee details that will be overtime :
<form name="form" method="post" action="#" id="form" autocomplete="off">
<table>
<tr>
<td>Badge ID</td>
<td>Employee Name</td>
<td>Department</td>
<td>Overtime Date</td>
<td>From</td>
<td>To</td>
<td>Remarks</td>
</tr>

<?php
$n = $_POST['jum'];
for ($i=1; $i<=$n; $i++)
{
echo "
<tr>
<td><input type='text' size='7' name='badgeid".$i."' maxlength='6' onkeyup='javascript:this.value=this.value.toUpperCase();' onChange='getCity('search.php?badgeid='+this.value)'></td>
<div id='citydiv'>
<td><input type='text' size='15' name='name".$i."' readonly></td>
<td><input type='text' size='12' name='dept".$i."' readonly>
</div>
<td><input type='text' size='12' name='otdate".$i."' id='date' class='date-pick' onChange='javascript:this.value=this.value.toUpperCase();' readonly></td>
<td><input type='text' size='4' name='otfrom".$i."' id='time1' value='' readonly></td>
<td><input type='text' size='4' name='otto".$i."' id='time2' value='' readonly></td>
<td><input type='text' size='25' name='remarks".$i."'></td>
</tr>";
}
?>
</table>
</form>
Image
So, if I type badgeid ex: 110011 then name and dept will show automatically. Even so table 2 and table 3.


Please help me. Really thanks with your support.
:D

Re: onChange in PHP Code not Working

Posted: Tue Dec 13, 2011 2:38 am
by social_experiment
davidalwis wrote:So, if I type badgeid ex: 110011 then name and dept will show automatically. Even so table 2 and table 3.
If you want work across multiple pages you need to carry the value along in some way; you could use $_GET or session variables. If you are going to work with a specific record set (the same user but a different table for them; say "Days worked" or "Leave available" this will work)

For the example you propose you will again need to use ajax; similar to the example you have already. Create a function to pass along the value (badgeid) and then search the database; if the information is found, return it and display it. This is a basic idea for this so you can most likely improve it :)