View Full Version : javascript and php
phpN00b
07-05-2005, 08:11 PM
hi,
in my php code i have the following line:
<td><a href="showMessage.php?convID=<? print $convoID; ?>" target='messages' onClick="vars('<? print $subject?>','<? print $memberFrom?>','<? print $fromName?>','<? print $convoID?>');"><? print $subject; ?></a></td>
when this code is executed, after three records it hangs for 3 seconds then loads the rest. so in order to remove this, i changed that line to this:
<? print "<td><a href=showMessage.php?convID=$convoID target='messages' onClick='vars('$subject','$memberFrom','$fromName' ,'$convoID');'>$subject</a></td>" ?>
and i declared those variables at the top. After doing this the speed problem was solved as the whole things loaded up fast. But now it's not calling the vars function i declared at the top which is:
<script language="javascript">
function vars(subject,toID,toName,convID) {
document.form1.subject.value = subject;
document.form1.toID.value = toID;
document.form1.toName.value = toName;
document.form1.convoID.value = convID;
}
</script>
any ideas?
phpN00b
07-05-2005, 09:07 PM
there is another problem.. even with the first line i used, it's still slow loading up the result. It shows 5 records and then it slows down for a couple of seconds and then it shows the rest. How can I fix this?
try this instead:
<?php print "<td><a href=showMessage.php?convID=" . $convoID . "target='messages' onClick='vars('" . $subject . "','" . $memberFrom . "','" . $fromName . "','" . $convoID . "');'>" . $subject . "</a></td>"; ?>
phpN00b
07-05-2005, 09:48 PM
still same problem. It loads fine in IE.. but in firefox it screws up.
phpN00b
07-05-2005, 10:16 PM
still same problem. It loads fine in IE.. but in firefox it screws up.
just to correct that phrase, it doesnt load fine in either. in IE it loads faster but i still get javascript errors. and doesn't car the vars function in either of them
I need to know more about what's happening on this page to figure out what the problem is.
Post the entire code.
kibboy
07-06-2005, 01:04 AM
Is this code contained within a loop? If so, and you loop many times, the decrease in speed could be due to a large text size and PHP flushing the buffer is the delay. I've created HTML pages 1MB+ in size with only text. This takes a while to downoad and you may see a slight increase in parser time for the browser. To check if this is the problem view source,save as and then check the file size. Either way, post the code and a snippet of the HTML as Tim said.
Your javascript is a seperate problem and probably due to the fact that a subject or name has a ' character in it. You need to do a replace("'","\'") on just about every print to be sure but the subject is usually the culprit.
kibb
phpN00b
07-06-2005, 11:00 AM
here is the code:
<?php
session_start();
include("db_connect.php");
?>
<script language="javascript">
function vars(subject,toID,toName,convID) {
document.form1.subject.value = subject;
document.form1.toID.value = toID;
document.form1.toName.value = toName;
document.form1.convoID.value = convID;
}
</script>
<?
if(! empty($_SESSION['memberID'])){
$memberID = $_SESSION['memberID'];
$query = "select distinct convID,memberTo,msgDate,msg_read,memberFrom,main,m emberTo,messageID,toName,fromName,subject,active from messages WHERE memberTo = '$memberID' and active = '1' ORDER by msg_read,msgDate DESC";
$result = mssql_query($query,$link);
$convoArray[] = 'test';?>
<table width="720" border="1" >
<tr>
<td rowspan="2" valign="top">
<form name="deleteForm" method="post" action="deleteMessage.php" target="messages">
<table width='500' border='1' >
<input name="delSubmit" type="submit" value="Delete"> <?
while($a_row = mssql_fetch_array($result)){
$convoID = $a_row['convID'];
if(! array_search($convoID, $convoArray)){
$convoArray[] = $a_row['convID'];
$new = $a_row['msg_read'];
if($new == '0' && $a_row['memberFrom'] == $memberID ){
$new = '1';
}
if($a_row['fromName'] == $_SESSION['name']){
$fromName = $a_row['toName'];
}else{
$fromName = $a_row['fromName'];
}
$subject = $a_row['subject'];
$memberFrom = trim($a_row['memberFrom']);
$fromName = trim($a_row['fromName']);
$msgDate = trim($a_row['msgDate']);
$messageID = trim($a_row['messageID']);
$toName = trim($a_row['toName']);
$toID = trim($a_row['toID']);
if($new == '1'){
?>
<tr valign="top">
<td><? print $fromName;?></td>
<? print "<td><a href=showMessage.php?convID=" . $convoID . " target='messages' >" . $subject . "</a></td>"; ?>
<td><? print $msgDate;?></td>
<td><input name="delete[]" type="checkbox" value="<? print $messageID; ?>"></td>
<input name="ID" type="hidden" value="<? print $_SESSION['memberID']; ?>">
</tr>
<? }else{ ?>
<tr bgcolor="#CC3399" valign="top">
<td valign="top"><? print $fromName;?></td>
<? print "<td><a href=showMessage.php?convID=" . $convoID . " target='messages' >" . $subject . "</a></td>"; ?>
<td><? print $msgDate;?></td>
<td><input name="delete[]" type="checkbox" value="<? print $messageID; ?>"></td>
<input name="ID" type="hidden" value="<? print $_SESSION['memberID']; ?>">
</tr>
<? }
}
}
mssql_close($link);
?>
</table>
</td>
</form>
<td><iframe name="messages" src="showMessage.php" width="400" height="400"></iframe></td>
</tr>
<tr>
<td>
<form name="form1" method="post" action="reply_message.php">
<textarea name="txtMessage" cols="40" rows="10"></textarea>
<input type="hidden" name="toID">
<input type="hidden" name="toName">
<input type="hidden" name="convoID">
<input type="hidden" name="subject">
<input name="reply" type="submit" value="Send">
</form>
</td>
</tr>
</table>
<? }else{
print "Please <a href=login.php>login</a> first.";
}
?>
sorry was outta town for a bit. I'll have a peek at it, and if I can spot anything, I'll post it.
a few minutes looking at this code, and I have to say it's kind of messy.
(not that I don't write messy code ;))
What I wonder, is when you check for a session memberID in the if statement, you have all that html that runs, do you want that stuff printing if the if statement runs to the else?
I'd start by cleaning that up, I'd have php print all that. It makes it much harder to look at your if statements and while loops when you have all those php and end php things popping in and out like that.
Then put in comment lines that spell out what happens. It may be an exercise that helps debug this (helps everytime when I'm stuck)
I haven't yet looked at the meat of the code as it's a little fun to figure out what's happening without comments and the in and out php stuff.
Powered by vBulletin® Version 4.1.7 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.