Basically the new entry adds into the database, and the ajax i used updates the listbox (which contains the users), but when you click on the entry you just added in the listbox, it returns 'undefined' in every text field (when a index is selected in the listbox all the values appear in text fields to the right of it)
This seems really odd to me, as it's as if it hasn't realised the database has this new data, however before the clicking and values appearing even happens, PHP has to read the new database entry to the listbox, and i know this works because the correct first and last names appear in the listbox.
I realise this may not make much sense so here's some code and a screenshot...
HTML
Code: Select all
<html>
<head>
<script language="javascript" src="search.js"></script>
<script language="JavaScript">
function ajaxRequest(){
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
for (var i=0; i<activexmodes.length; i++){
try{
return new ActiveXObject(activexmodes[i])
}
catch(e){
//suppress error
}
}
}
else if (window.XMLHttpRequest) // if Mozilla, Safari etc
return new XMLHttpRequest()
else
return false
}
function add(){
var mypostrequest=new ajaxRequest()
var a=encodeURIComponent(document.getElementById("student_id").value);
var b=encodeURIComponent(document.getElementById("fname").value);
var c=encodeURIComponent(document.getElementById("sname").value);
var d=encodeURIComponent(document.getElementById("course").value);
var e=encodeURIComponent(document.getElementById("school").value);
var f=encodeURIComponent(document.getElementById("yos").value);
var url = "add.php";
mypostrequest.onreadystatechange=function(){
if (mypostrequest.readyState==4){
if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
document.getElementById("studentlistbox").innerHTML=mypostrequest.responseText
}
else{
alert("An error has occured making the request")
}
}
}
var parameters="stid="+a+"&fname="+b+"&sname="+c+"&crs="+d+"&sch="+e+"&yos="+f
mypostrequest.open("POST", url, true)
mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
mypostrequest.send(parameters)
}
//This is the function called when an element of the listbox is clicked on
function clicked()
{
var rows=[];
var MyElement;
var selobj;
var i;
<?php
$db=mysql_connect("[i]dbhost[/i]", "[i]username[/i]", "[i]password[/i]");
mysql_select_db("[i]dbname[/i]") or die('Error selecting database');
$result=mysql_query('SELECT * FROM students') or die ('Error performing query');
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
?>
rows[rows.length]=<?php echo '"'.$row[student_id].'"'?>;
rows[rows.length]=<?php echo '"'.$row[fname].'"'?>;
rows[rows.length]=<?php echo '"'.$row[sname].'"'?>;
rows[rows.length]=<?php echo '"'.$row[course].'"'?>;
rows[rows.length]=<?php echo '"'.$row[school].'"'?>;
rows[rows.length]=<?php echo '"'.$row[yos].'"'?>;
<?php
}
?>
selobj = document.getElementById('studentlistbox');
i = selobj.selectedIndex;
i = i*6;
MyElement = document.getElementById("student_id");
MyElement.value = rows[i];
MyElement = document.getElementById("fname");
MyElement.value = rows[i+1];
MyElement = document.getElementById("sname");
MyElement.value = rows[i+2];
MyElement = document.getElementById("course");
MyElement.value = rows[i+3];
MyElement = document.getElementById("school");
MyElement.value = rows[i+4];
MyElement = document.getElementById("yos");
MyElement.value = rows[i+5];
}
function clearing()
{
var MyElement;
MyElement = document.getElementById("student_id");
MyElement.value = "";
MyElement = document.getElementById("fname");
MyElement.value = "";
MyElement = document.getElementById("sname");
MyElement.value = "";
MyElement = document.getElementById("course");
MyElement.value = "";
MyElement = document.getElementById("school");
MyElement.value = "";
MyElement = document.getElementById("yos");
MyElement.value = "";
}
</script>
</head>
<body>
<form action="manageuser.php" method="post">
<table border="0">
<tr>
<table border='0'>
<tr>
<td rowspan="6">
<select size=10 name=userList id="studentlistbox" onchange="clicked();">
<?php
include 'manageuser.php';
populate();
?>
</select>
</td>
<td>
Student ID:
</td>
<td>
<input type="text" name="student_id" id="student_id" /><br />
</td>
</tr>
<tr>
<td>
First Name:
</td>
<td>
<input type="text" name="fname" id="fname" /><br />
</td>
</tr>
<tr>
<td>
Surname:
</td>
<td>
<input type="text" name="sname" id="sname" /><br />
</td>
</tr>
<tr>
<td>
Course:
</td>
<td>
<input type="text" name="course" id="course" /><br />
</td>
</tr>
<tr>
<td>
School:
</td>
<td>
<input type="text" name="school" id="school" /> <br />
</td>
</tr>
<tr>
<td>
Year Of Study:
</td>
<td>
<input type="text" name="yos" id="yos" /><br />
</td>
</tr>
<tr>
<td>
<input type="BUTTON" value="Clear" onClick="clearing();" />
</td>
<td>
<input type="BUTTON" value="Search" onClick="search(student_id.value, fname.value, sname.value, course.value, school.value, yos.value);" />
</td>
<td>
<input type="BUTTON" value="Add" onClick="add();" />
</td>
</tr>
</table>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
<div id="result" name="result">result</div>
</body>
</html>Code: Select all
<?php
function populate()
{
$con=mysql_connect("[i]dbhost[/i]", "[i]username[/i]", "[i]password[/i]");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("[i]dbname[/i]", $con);
$result = mysql_query("SELECT * FROM students");
while($row = mysql_fetch_array($result))
{
echo "<option>$row[sname], $row[fname]</option>";
}
mysql_close($con);
}
//http://www.mredkj.com/tutorials/tutorial002.html
//http://forums.devnetwork.net/viewtopic.php?f=1&t=112758
?>Code: Select all
<?php
$stid = $_POST['stid'];
$fname = $_POST['fname'];
$sname = $_POST['sname'];
$crs = $_POST['crs'];
$sch = $_POST['sch'];
$yos = $_POST['yos'];
$con=mysql_connect("[i]dbhost[/i]", "[i]username[/i]", "[i]password[/i]");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("[i]dbname[/i]", $con);
mysql_query("INSERT INTO students VALUES($stid, '$fname', '$sname', '$crs', '$sch', '$yos')");
mysql_close($con);
include ('manageuser.php');
populate();
?>
http://tinypic.com/r/2h6f5w6/6
Thanks is advance for any light you can shed on this, and if you need to know anything else dont hesitate to ask