html/javascript 'alert box' in php???

A

Anonymous

Guest
I'm trying to do the following::

Code:
<html>
<head>
<script LANGUAGE="JAVASCRIPT">

</script>
</head>
in one php file above the actual php code... there's nothing wrong there.

and making an included php file that has ...

Code:
<body>
<a onclick="AlertBox1()"> (Open Alert Box)</a>
</body>
</html>

in the body portion... (there's the problem).

When I try to use the onclick portion in the body tag, it makes the php fail.

(parse error)when using
Code:
<a onclick="AlertBox1()"> (Open Alert Box)</a>
also with
Code:
<a onclick=\"AlertBox1()\"> (Open Alert Box)</a>

Can anyone help me with this one?

Thanks a bunch in advance.

B.
 
You need to paste the actual error that you're getting. And what HTML is getting sent to the browser? Do View > Source and take a look at what you see there. Is it correct? Paste it here.
 
Let me try to explain a little better... (getting a brain cramp, so please, bear with me)..lol

I have this page as main.php
Code:
<html>

<head>
<script LANGUAGE="JAVASCRIPT">

</script>
<script LANGUAGE="JAVASCRIPT">

</script>
<script LANGUAGE="JAVASCRIPT">

</script>
</head>

<body>

<?php

include "insert.php";

?>

</body>

</html>

Which determines what the alert boxes are to contain.

And insert.php below (which will be changing depending on certain variables --- not finished, yet...But, will be one of those if ($poss1 == "1" -<showing the link for alert box 1>- and so forth.)

Code:
<?php
print
("
<a onclick=\"AlertBox1()\">(Alert Box 1)</a>
<a onclick=\"AlertBox2()\">(Alert Box 2)</a>
<a onclick=\"AlertBox3()\">(Alert Box 3)</a>
");

?>

I did have nothing but errors before this... now it only displays this text...

(Alert Box 1)(Alert Box 2)(Alert Box 3)
with nothing clickable...

How do I fix that? Am I close?

Thanks for the help so far,

B.
 
I don't understand why you do this:
Code:
<?php
print
("
<a onclick=\"AlertBox1()\">(Alert Box 1)</a>
<a onclick=\"AlertBox2()\">(Alert Box 2)</a>
<a onclick=\"AlertBox3()\">(Alert Box 3)</a>
");

?>

When you could just do this:
Code:
<a onclick="AlertBox1()">(Alert Box 1)</a>
<a onclick="AlertBox2()">(Alert Box 2)</a>
<a onclick="AlertBox3()">(Alert Box 3)</a>

PHP can include any file -- it doesn't have to have any PHP in it.

At any rate, what the server is actually sending to the browser is a lot more important than what the browser is displaying, which is why you should do View > Source and see if the output is what you expect.
 
Like I said... right now, it shows all of the links for the alert boxes. Later, I'm going to make them in only show in certain conditions... php can do this, but html can't.

changing the insert.php to the following worked!

Code:
<?php
print
("
<a onclick='AlertBox1()'>(Alert Box 1)</a>
<a onclick='AlertBox2()'>(Alert Box 2)</a>
<a onclick='AlertBox3()'>(Alert Box 3)</a>
");

?>
 
WRStrong said:
Like I said... right now, it shows all of the links for the alert boxes. Later, I'm going to make them in only show in certain conditions... php can do this, but html can't.[/quit]

I still don't understand why you insist on enclosing the whole thing inside a PHP print statement. There's no advantage to that. And trust me, once you start plugging parameters into your JavaScript you're gonna have the same escaping issues. They're not a big deal, but it's easier to avoid them altogether by exiting PHP when you want to output HTML (or JavaScript especially). Take a look at the following:

Code:
<?php
if($condition) { ?>
  <a onclick="AlertBox1()">(Alert Box 1)</a>
<?php } elseif($condition2) { ?>
  <a onclick="AlertBox2()">(Alert Box 2)</a>
<?php } else { ?>
  <a onclick="AlertBox3()">(Alert Box 3)</a>
}
?>

It looks a little less funny if you use the alternate syntax:

Code:
<?php if($condition): ?>
  <a onclick="AlertBox1()">(Alert Box 1)</a>
<?php elseif($condition2): ?>
  <a onclick="AlertBox2()">(Alert Box 2)</a>
<?php else: ?>
  <a onclick="AlertBox3()">(Alert Box 3)</a>
<?php endif; ?>
?>

There's no reason to enclose chunks of HTML or JavaScript code like this in PHP print statements.
 
Back
Top