So I've always done external js scripts from a html by taking out the script tags and linking the src file in the header, and i know this is a PHP forum, but the only thing I can think it might be is that i have PHP in the js file.
Basically when i try and run this, the listbox populates with entries from a database fine, but when i click a item on the listbox, the values dont show up in the text fields like i told it to. I also know it's not a coding issue as the code runs just fine when i put it in the html header with the script tags...
Here's the code so you can point out what painfully obvious thing i may have missed out
HTML (manageusers.html)
Code: Select all
<html>
<head>
<script type="text/javascript" src="clicked.js"></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="id" id="txtid" /><br />
</td>
</tr>JavaScript (clicked.js)
Code: Select all
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("txtid");
MyElement.value = rows[i];
MyElement = document.getElementById("txtfn");
MyElement.value = rows[i+1];
MyElement = document.getElementById("txtsn");
MyElement.value = rows[i+2];
MyElement = document.getElementById("txtcrs");
MyElement.value = rows[i+3];
MyElement = document.getElementById("txtsch");
MyElement.value = rows[i+4];
MyElement = document.getElementById("txtyos");
MyElement.value = rows[i+5];
}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
?>thanks in advance guys, hope you can help
xx