• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

PHP w/ java script

this is a snipset of the program.. I'm trying to make the jscript run.

$err[9]="Please fill up the Citizenship field";
$err[10]="Please fill up the Age field";
$err[11]="Please fill up the Weight field";

echo'<script language="javascript">';
echo 'alert("'.$err[$ctr].'")';<--- having problem's with this part>
?>
history.go(-1)
</script>
<?

nevermind.but if can give an alternative i'll gladly give the 5.
0
mazkot
Asked:
mazkot
  • 3
1 Solution
 
jausionsCommented:
Hi,

Try the following (Basically, add line returns.)

Regards,

 -Philippe

----------------------------------------------

$err[9]='Please fill up the Citizenship field';
$err[10]='Please fill up the Age field';
$err[11]='Please fill up the Weight field';

echo "<script language=\"javascript\" type=\"text/javascript\">\r\n<!--\r\n";
echo 'alert("' . addslashes($err[$ctr]) . '")';
?>
history.go(-1)
// -->
</script>
<?
0
 
jausionsCommented:
Also, add the ";" at the end  of the JavaScript lines.
0
 
VGRCommented:
basically, you don't change anything from what he wrote.
You added the <!-- hide from impared browsers
you addslashes() strings that do not contain to-be-escaped characters...

First for the jscript to run it has to be in the BODY part of the HTML page.
This stated, you forgot the ending semicolon and add \r\n after each line in your script, like this :
echo 'alert("'.$err[$ctr].'");\r\n';

Please also copy-paste here the HTML code generated so that we may check if $err[$ctr] has received a proper value.

regards,
0
 
jausionsCommented:
Ok, here it is...

Key thing here is the line returns, otherwise web browser won't be happy...

I made it clearer on this version... (Note: echo() function doesn't output a line return.)

addslashes() is mandatory to avoid corrupting your JavaScript (Who knows if a quote or double-quote are in the 0-8 $err value...)

----------------------------------------------
<?php

...

$err[9]='Please fill up the Citizenship field';
$err[10]='Please fill up the Age field';
$err[11]='Please fill up the Weight field';

?>
<script language="javascript" type="text/javascript">
<!--
alert("<?php echo addslashes($err[$ctr]) ?>");
history.go(-1);
// -->
</script>
<?php

...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now