how to call a javascript function w/in php block..?

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
tonting
Forum Newbie
Posts: 1
Joined: Thu Jul 17, 2008 8:32 pm

how to call a javascript function w/in php block..?

Post by tonting »

Code: Select all

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language = "text/javascript">
<!--
var p1=0;
function progress(x)
{
var p1=p1+x;
document.form1.div1.innerHTML = p1; 
}
//-->
</script>
</head>
 
 
<?php 
if (isset($_POST['Submit'])){
$myfile=$_FILES['userfile']['tmp_name'];
$serverfile=$_FILES['userfile']['name'];
$F = fopen($myfile,"rb");
$F1 = fopen($serverfile,"a");
$size = filesize($myfile);
$x=0;
do {
$data = fread($F,512);
$x= $x+strlen($data);
echo "<script type=\"text/javascript\">progress(512);</script>";
fseek($F1, SEEK_END);
fwrite($F1,$data);
} while (!feof($F));
fclose($F);
fclose($F1);
}
?>
 
<body>
 
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <table width="800" border="0" align="center" cellpadding="2" cellspacing="2">
    <tr>
      <td><input name="userfile" type="file" id="userfile" /></td>
    </tr>
    
    <tr>
      <td><div id="div1">Result here.. </div></td>
    </tr>
    <tr>
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
</form>
</body>
</html>
i wan to call this function

Code: Select all

echo "<script type=\"text/javascript\">progress(512);</script>";
unfortunately it wont run.. is there something wrong with my code?

Thanks...
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: how to call a javascript function w/in php block..?

Post by VladSun »

1.

Code: Select all

<script language = "text/javascript">
should be

Code: Select all

<script language = "javascript">
2. If you intend to change the value of the global variable p1 you should remove the var keyword in the function body, otherwise a local scope variable p1 is created.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply