Page 1 of 1

huh!? when did external js stop working? =S

Posted: Thu Feb 11, 2010 3:59 pm
by mattman098
RIGHT THEN

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>
... the html goes on for a while with more text fields and closing tags etc.

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];
}
PHP, incase it might be relevant for this issue (manageuser.php)

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

Re: huh!? when did external js stop working? =S

Posted: Thu Feb 11, 2010 5:36 pm
by requinix
Try adding a

Code: Select all

header("Content-Type: text/javascript");
at the beginning of the .js file.

Re: huh!? when did external js stop working? =S

Posted: Thu Feb 11, 2010 6:15 pm
by xjake88x
Your server isn't gonna execute php in a .js file.

Try navigating to that file manually and you will probably see PHP code.


But there is no rule saying a javascript dependency has to be .js. It can be .php.

Re: huh!? when did external js stop working? =S

Posted: Fri Feb 12, 2010 5:27 am
by mattman098
alright cheers guys, didn't realise you just plain old can't have PHP in a js file

I'll just leave it in the HTML header where it works =]