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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mattman098
Forum Newbie
Posts: 16
Joined: Wed Feb 10, 2010 1:45 pm

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

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

Try adding a

Code: Select all

header("Content-Type: text/javascript");
at the beginning of the .js file.
User avatar
xjake88x
Forum Commoner
Posts: 50
Joined: Sun Aug 01, 2004 7:05 pm

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

Post 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.
mattman098
Forum Newbie
Posts: 16
Joined: Wed Feb 10, 2010 1:45 pm

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

Post 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 =]
Post Reply