why doesn't this work?

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
Brakanjan
Forum Newbie
Posts: 19
Joined: Wed May 14, 2003 9:57 am
Location: South Africa

why doesn't this work?

Post by Brakanjan »

<?
echo '<script language="Javascript">
document.form1.hidden.value = "Hello";
</script>
<body>
<form name=form1 action=Doit.php method=GET>
<input type=text name=hidden>
</form>
<body>';
?>

I though php was easy!
Why can't I inlcude src="name.js" files in php?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

it's not a php problem.
Before giving you the answer a small side-note ;)
php is executed server-side, the browser will only receive the outout. Therefor for the browser there is no difference between your script and the static html-page

Code: Select all

<script language="Javascript">
document.form1.hidden.value = "Hello";
</script>
<body>
<form name=form1 action=Doit.php method=GET>
<input type=text name=hidden>
</form>
<body>
side-note #2 (we're getting closer): always quote the values of properties: <form name="form1" action="Doit.php" method="GET"> unless you're absolutely sure about what you're doing. It's also nice to close each element you're opening, even those that have no content: <input type=text name=hidden />
ok, now for the current problem: The script is executed before the form exists, so there is no object document.form1 (yet). Try

Code: Select all

<html>
	<body>
		<form name="form1" action="Doit.php" method="GET">
			<input type="text" name="hidden" />
		</form>
		<script type="text/javascript">
			document.form1.hidden.value = "Hello";
		</script>
	</body>
</html>
Brakanjan
Forum Newbie
Posts: 19
Joined: Wed May 14, 2003 9:57 am
Location: South Africa

Post by Brakanjan »

I should have known.... :x
tx!
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

I am still converting from using all php from a mix of php and html.

Ie <tr><td>Hello <? echo $_SESSION['user']; ?></td></tr>

to

echo "<tr><td>Hello $_SESSION['user']</td></tr>";
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

oldtimer wrote:I am still converting from using all php from a mix of php and html.

Ie <tr><td>Hello <? echo $_SESSION['user']; ?></td></tr>

to

echo "<tr><td>Hello $_SESSION['user']</td></tr>";
String concatenation with the dot. You need to distinguish between php code and strings in the echo statement. Like

echo "<tr><td>Hello ".$_SESSION['user']."</td></tr>";
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

Typo on my part. That is how I do it. That is what I get when I have too many things going on at one time :)
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

Ah well, maybe someone else will learn from it then. :)
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

We are always learning. I have figured that much out so far.
Post Reply