Forum Moderators: open

Message Too Old, No Replies

php problem in external javascript function

         

nelsonm

7:41 pm on Nov 28, 2010 (gmt 0)

10+ Year Member



Hi all,

I have the following inline javascript function in my index.php file...

<html>
<head>
<script type="application/JavaScript">

function resetForm(){
var checkbox = document.MercyForm.elements["cb"];
var aomPreset = new Array;

<?php for($i=0;$i<count($aomPreset); $i++){ echo "aomPreset[$i]='".$aomPreset[$i]."';\n\t"; } ?>

if(window.confirm("Reset the form?")){
document.MercyForm.distance.value= <?php echo "'".$milPreset."';"; ?>
document.MercyForm.zipcode.value= <?php echo "'".$zipPreset."';"; ?>


for(i=0; i < checkbox.length; i++){
if(aomPreset[i] == "1" ){checkbox[i].checked = true;}else{checkbox[i].checked = false;}
}

return(true);
}else{
return(false);
}

}

</script>
</head>
<body>
<html>


It works fine inline from the index.php page. It does not work when it's called from an external javascript file in the index.php file as follows...

<html>
<head>

<script type="application/JavaScript" scr="scripts/keypad.js"></script>

</head>
<body>
<html>


Does anyone know why it works inline but not from an externally called .js file?

Does this have to do with telling Apache to look for php inside an external javascript file?

astupidname

9:41 pm on Nov 28, 2010 (gmt 0)

10+ Year Member



Right, the server does not treat external .js files as php code unless you tell it to, though I would do it in reverse and output a .php file as javascript, all it takes is the appropriate header and script call:

<?php
//>keypad.php

//output appropriate header
header('Content-type: text/javascript');

//you would need to have the definition of $aomPreset in this "keypad.php" file...
//as well as definitions for $milPreset and $zipPreset, this may be a problem for
//you if you are deriving them from submitted form values, in which case you
//could pass them to this script with url variables appended to the script tags src, see later script tag info
$aomPreset = array(1, 2, 3);
$milPreset = 'something';
$zipPreset = 'or other';

function resetForm(){
var checkbox = document.MercyForm.elements["cb"];
var aomPreset = new Array;

<?php for($i=0;$i<count($aomPreset); $i++){ echo "aomPreset[$i]='".$aomPreset[$i]."';\n\t"; } ?>

if(window.confirm("Reset the form?")){
document.MercyForm.distance.value= <?php echo "'".$milPreset."';"; ?>
document.MercyForm.zipcode.value= <?php echo "'".$zipPreset."';"; ?>

for(i=0; i < checkbox.length; i++){
if(aomPreset[i] == "1" ){checkbox[i].checked = true;}else{checkbox[i].checked = false;}
}
return(true);
}else{
return(false);
}
}
?>


Then:

<script type="text/javascript" src="scripts/keypad.php"></script>

OR, if you need to dynamically access $milPreset and $zipPreset from the index page:
<script type="text/javascript" src="scripts/keypad.php?milPreset=<?php echo $milPreset; ?>&amp;zipPreset=<?php echo $zipPreset; ?>"></script>

and then in keypad.php you could retrieve $_GET['milPreset'] and $_GET['zipPreset']

nelsonm

11:49 pm on Nov 28, 2010 (gmt 0)

10+ Year Member



I tried that method and it does not work. First of all, DW CS5 gives me "code hinting" errors. When i preview in IE or Firefox, the function does not work.

Click on the link below to see a snippit of the .php file in DQ CS5.

[nados.com ]

astupidname

12:18 am on Nov 29, 2010 (gmt 0)

10+ Year Member



Hi nelsonm,
I assure you it absolutely does work, if you follow my previous post to the letter. Re-read the comments and such, I never once showed "application/javascript", plus your definitions of $aomPreset, $milPreset and $zipPreset are missing. I have never used DW, if it's being b***hy use notepad or another text editor, then preview the index page on server.

astupidname

12:31 am on Nov 29, 2010 (gmt 0)

10+ Year Member



Oops, my bad, just realized after having another look, one thing I did miss was the need to jump in and out of php code there, unless we would echo the entire resetForm, so a correction to my earlier post is indeed required:

<?php
//>keypad.php

//output appropriate header
header('Content-type: text/javascript');

//you would need to have the definition of $aomPreset in this "keypad.php" file...
//as well as definitions for $milPreset and $zipPreset, this may be a problem for
//you if you are deriving them from submitted form values, in which case you
//could pass them to this script with url variables appended to the script tags src, see later script tag info
$aomPreset = array(1, 2, 3);
$milPreset = 'something';
$zipPreset = 'or other';

//exit php code temporarily, or else we could just echo the entire resetForm function..
?>
function resetForm(){
var checkbox = document.MercyForm.elements["cb"];
var aomPreset = new Array;

<?php for($i=0;$i<count($aomPreset); $i++){ echo "aomPreset[$i]='".$aomPreset[$i]."';\n\t"; } ?>

if(window.confirm("Reset the form?")){
document.MercyForm.distance.value= <?php echo "'".$milPreset."';"; ?>
document.MercyForm.zipcode.value= <?php echo "'".$zipPreset."';"; ?>

for(i=0; i < checkbox.length; i++){
if(aomPreset[i] == "1" ){checkbox[i].checked = true;}else{checkbox[i].checked = false;}
}
return(true);
}else{
return(false);
}
}

nelsonm

1:09 am on Nov 29, 2010 (gmt 0)

10+ Year Member



I did miss was the need to jump in and out of php code there

yea pretty much what you have to do when adding php code in an html doc.

well... since it works inline within index.php, i'll leave it. It seems a much easier solution.

Thanks anyway,

btw, "text/javascript" has been replaced by "application/javascript", though Internet Explorer versions 6 through 8 does not recognize scripts with this attribute.