FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Help with php and java script





bukaida
Hi
I have written a page in html & javascript. When I am opening it directly
from my web browser(with .html extn), it is working fine. But when I am trying to open it from localhost(same code with .php extn) with the html tags and javascript tags inside php echo, the java script is partialy working. I am using php 4.4.0 under apache2. The source code is given below--
Code:

<?
echo'<html>
<head>
<title>Student Database Management System</title>
<script language="JavaScript" type="text/javascript">
/***** CUSTOMIZE THESE VARIABLES *****/

  // width to resize large images to
var maxWidth=150;
  // height to resize large images to
var maxHeight=150;
  // valid file types
var fileTypes=["bmp","gif","png","jpg","jpeg"];
  // the id of the preview image tag
var outImage="previewField";
  // what to display when the image is not valid
var defaultPic="images/pictures.png";

/***** DO NOT EDIT BELOW *****/

function preview(what){
  var source=what.value;
  var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
  for (var i=0; i<fileTypes.length; i++) if (fileTypes[i]==ext) break;
  globalPic=new Image();
  if (i<fileTypes.length) globalPic.src=source;
  else {
    globalPic.src=defaultPic;
    alert("THAT IS NOT A VALID IMAGE\nload an image of one of the following:\n\n"+fileTypes.join(", "));
  }
  setTimeout("applyChanges()",200);
}
var globalPic;
function applyChanges(){
  var field=document.getElementById(outImage);
  var x=parseInt(globalPic.width);
  var y=parseInt(globalPic.height);
  if (x>maxWidth) {
    y*=maxWidth/x;
    x=maxWidth;
  }
  if (y>maxHeight) {
    x*=maxHeight/y;
    y=maxHeight;
  }
  field.style.display=(x<1 || y<1)?"none":"";
  field.src=globalPic.src;
  field.width=x;
  field.height=y;
}
</script>
</head>
<body>
<fieldset>
<div align="center"><u><b><font color="#800000" size="+2">STUDENT ADMISSION FORM</font></b></u></div>
<form action="admission_details.php" method="post">
<fieldset><legend><h3><em><font color="#800000">Enter information in the form below:</font></em></legend></h3>
<div align="right" style="line-height: 1.9em;">
Upload the picture of the student:
<br>
<input type="file" id="picField" onchange="preview(this)">
<br>
<img alt="Graphic will preview here" id="previewField" src="images/pictures.png">
<br></div>
<table>
<p><tr><td><strong><font face="Sylfaen" color="#800000">Student Id:</font></strong></td><td><input type="text" name="s_id" size="10"></td></tr></p>
<p><tr><td><strong><font face="Sylfaen" color="#800000">First Name:</font></strong></td><td><input type="text" name="s_fname" size="20"></td></tr></p>
<p><tr><td><strong><font face="Sylfaen" color="#800000">Last Name:</font></strong></td><td><input type="text" name="s_lname" size="20"></td></tr></p>
<p><tr><td><strong><font face="Sylfaen" color="#800000"> Name of father/gurdian:</font></strong></td><td><input type="text" name="s_gname" size="30"></td></tr></p>
<p><tr><td><strong><font face="Sylfaen" color="#800000">Address:</font></strong></td><td><textarea name="s_address" cols="25" rows="5"></textarea></td></tr></p>
<p><tr><td><strong><font face="Sylfaen" color="#800000">Date of birth:</font></strong></td>
<td><select name="day">
<option value="dd">DD</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="month">
<option value="mm">MM</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="year">
<option value="yyyy">YYYY</option>
<option value="2000">2000</option>
<option value="1999">1999</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
</select></td></tr></p>
<p><tr><td><strong><font face="Sylfaen" color="#800000">Gender:</font></strong></td>
      <td><input type="radio" name="s_gender" value="Male" checked="checked"><font face="Sylfaen" color="#800000">Male</font>
      <input type="radio" name="s_gender" value="Female"><font face="Sylfaen" color="#800000">Female</font></td></tr></p>
<p><tr><td><strong><font face="Sylfaen" color="#800000">Class:</font></strong></td>
<td><select name="s_class">
<option value="select">Select One</option>
<option value="I">I</option>
<option value="II">II</option>
<option value="III">III</option>
<option value="IV">IV</option>
<option value="V">V</option>
<option value="VI">VI</option>
<option value="VII">VII</option>
<option value="VIII">VIII</option>
<option value="IX">IX</option>
<option value="X">X</option>
<option value="XI">XI</option>
<option value="XII">XII</option>
</select></td></tr></p>
<p><tr><td><strong><font face="Sylfaen" color="#800000">Section:</font></strong></td>
<td><select name="s_section">
<option value="select">Select One</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select></td></tr></p>
<p><tr><td><strong><font face="Sylfaen" color="#800000">Height (in cms.):</font></strong></td><td><input type="text" name="s_height" size="10"></td></tr></p>
<p><tr><td><strong><font face="Sylfaen" color="#800000">Weight (in kgs.):</font></strong></td><td><input type="text" name="s_weight" size="10"></td></tr></p>
<p><tr><td><strong><font face="Sylfaen" color="#800000">Father/Guardian income (in Lakhs):</font></strong></td><td>
<select name="s_g_income">
<option value="select">Select One</option>
<option value="Less than 1">Less than 1</option>
<option value="Between 1 and 5">Between 1 and 5</option>
<option value="Between 5 and 10">Between 5 and 10</option>
<option value="Between 10 and 15">Between 10 and 15</option>
<option value="Between 15 and 20">Between 15 and 20</option>
<option value="More than 20">More than 20</option>
</select></td></tr></p>
<p><tr><td><strong><font face="Sylfaen" color="#800000">Percentage of marks obtained in last examination:</font></strong></td><td><input type="text" name="s_marks" size="10"></td></tr></p>
<p><tr><td><strong><font face="Sylfaen" color="#800000">Ranking in last examination:</font></strong></td><td>
<select name="s_rank">
<option value="select">Select One</option>
<option value="1st">1st</option>
<option value="2nd">2nd</option>
<option value="3rd">3rd</option>
<option value="4th">4th</option>
<option value="5th">5th</option>
<option value="6th">6th</option>
<option value="7th">7th</option>
<option value="8th">8th</option>
<option value="9th">9th</option>
<option value="10th">10th</option>
<option value="11th">11th</option>
<option value="12th">12th</option>
<option value="13th">13th</option>
<option value="14th">14th</option>
<option value="15th">15th</option>
<option value="16th">16th</option>
<option value="17th">17th</option>
<option value="18th">18th</option>
<option value="19th">19th</option>
<option value="20th">20th</option>
<option value="none">No Rank</option>
<select></td></tr></p>
<p><tr><td><strong><font face="Sylfaen" color="#800000">Hobbies:</font></strong></td><td><input type="checkbox" name="s_hobbies[]" value="soccer" />Soccer
<input type="checkbox" name="s_hobbies[]" value="cricket" />Cricket
<input type="checkbox" name="s_hobbies[]" value="tennis" />Tennis
<input type="checkbox" name="s_hobbies[]" value="dance" />Dance
</td></tr></p>
</table>
<br>
</fieldset>
</fieldset>
<br>
<div align="center"><p><input type="submit" name="submit1" value="Submit">
<input type="reset" name="reset" value="Clear"></p>
</div>
</form>
</body>
</html>';
?>


please help.
hexkid
bukaida wrote:
Code:
<?
echo'[...]';
?>


please help.
Apart from what I've retained from your code, there is absolutely no PHP in it. In fact your code is absolutely identical to
Code:
[...]
If your server is configured with short_open_tag disabled, your code will fail. Using "<?php" (instead of "<?") makes your code work no matter what the configuration of the server is.

I suggest you ask in the Html, CSS and Javascript forum.
bukaida
@hexkid
My php pages work perfectly with <? ....?>. The main problem is that, the javascript part is partialy executing. The alert part is working fine when I am selecting a different type of file other than image. Also if I remove the php tag and save it as pure html, and open directly in browser(not from localhost), it is working perfectly. So there is no problem in html-java script section. Only problem is when I am trying to run it from server, it is not working. So it is the problem with php part.

Anyway, How can I shift my post from one forum to another.I think only a moderator can do it.
hexkid
bukaida wrote:
My php pages work perfectly with <? ....?>.

Until you have to use them on a server with short_open_tags off.
Why run that risk? Now that you know you can make your script run everywhere no matter what the server configuration is, use that knowledge Smile

Also don't do
Code:
Your name is <?=$name?>, good morning.

If the server is configured with short_open_tags off the way to do that is
Code:
Your name is <?php echo $name; ?>, good morning.


bukaida wrote:
Only problem is when I am trying to run it from server, it is not working. So it is the problem with php part.

Your form needs an enctype attribute to enable it to upload files. See Handling file uploads in the PHP manual.

Just for fun (I don't care about JavaScript) I tried your code with and without the php stuff. Neither version worked. The alert came up with both versions.
kv
since it is partially working, it means that there is no issue with php tags. May be the browser treats the javascript differently when run from local machine and when accessed from a server (the latter might have more restrictions). Just to make sure, check the html source in your browser and see that it contains the entire html you are trying to push from server.
yjwong
You might want to debug the Javascript problem. Access your page, and look for any errors found in (Firefox only;) Tools > Error Console. If it was a security error, the error would be listed there.
bukaida
@hexkid: <?PHP..?> did not work either. And as for the fun stuff, run the file as pure html, directly from browser and not from server.It is working.
It should show the preview of the image file selected.From the server, neither PHP nor the html version is working. I have checked in httpd of apache and the short_open_tag is on. What to do now?

@kv: The code listed here is from my browser's View source only with php tag added manualy. Sad
sonam
I am not sure that you can run JavaScript with echo function because this is different language then PHP. Only what you can do is:
1. Copy your full HTML and paste in pure txt file eg. myfile.txt
2. include this in your php page e.g.:

Code:
<?PHP
    $myPage = include ("myfile.txt");

// and then echo
    echo $myPage;
?>



Quote:
bukaida wrote:
My php pages work perfectly with <? ....?>.

Until you have to use them on a server with short_open_tags off.


hexkid is right. Scripts is perfectly working on your localhost but what happend when you upload it's on server??? Shocked

Sonam
spider
(So you know: I dont know javascript)\

I had to redew mine for php to add information to javascript as follows..
your so called script only has the echo... are you trying to use this page with and include for php?



echo "ads[i] = \"$anImage\";\n";
echo "linx[i] = \"".rtrim($URL)."\";\n";
echo "delays[i++] = $adDurationSecs;\n";

which puts something like this in the actual page for in the middle of a javascript:

ads[i] = "728x90.gif";
linx[i] = "http://tusc-oh.com/b/index.php";
delays[i++] = 15;

Such as in my script I just posted...
all your doing is replacing...
of course this is just for variables pretty much,,

I took off all the php stuff from your script, and seams to put an image under the browse button for getting the image,,, this is running it as test.php on a localhost... not sure what your wanting out of it,,, but puts the image on my home server just fine,,, with that and not having any more information on it, it seams to work for me,,, but without understanding javascript,,, you get my point...

but if you want to use php,,, dont use php around the entire page,,,
php runs faster if you use something like
<html><head><title><? echo $title; ?></title></head> and so on...

and if you need php to be used more in the html... you can do it this way
like for a table:

<table border="<? echo $tborder; ?>" cellpadding="0" cellspacing="0" width="100%"><tbody><tr>
<td align="left" background="http://<echo $domain; ?>/img/<? echo $image; ?>" width="25%">

</td></tr></tbody></table>

it takes php longer to try to read all the html when the browser does that automaticly when you try to do it your way,,, and with adding php for mostly your variables, functions, mysql, ect... ive found it easier this way, and found it faster loading... depending on what you want...
bukaida
Actualy I am trying to post some information along with an image which has a preview capability before posting.Here came the java script part.The script without the preview part worked fine for upload and download the image(of course with some added php stuff ). Any other solution for doing the same?
Related topics
Hot or Not PHP-Nuke script
What's the best PHP Wiki script?
Some questions
[man][help]PHP...
Php voting script needed (URGENT)
PHP contact script...
problem with java script file
java script checking page
java script search engine..
java script popup tool tip
java script help please!!!
Java script error
need help in a simple php register script based on flat file
I want to learn HTML, DHTML CSS
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.