...

View Full Version : Wrong syntax?



ScottInTexas
02-21-2003, 03:22 PM
I'm following a turtorial on webmasterbase.com to learn PHP. The author creates a simple html file that calls a PHP file and passes a variable.

The contents of the html file are


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<a href="target.php?name=Scott">Fire</a>
</body>
</html>


The contents of the PHP file are;


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php echo("This is the target file. I sent $name"); ?>
</body>
</html>


I've also tried this file without the header. The results are;

This is the target file. I sent

This is all on the "http://localhost" running Apache server.

What's missing here? I have another file that uses $_Post["name"] that works.

Spookster
02-21-2003, 03:36 PM
There is this little setting in PHP called register_globals. In your case on your server it is set to off. The author of that tutorial is explaing things as if it were set to on.

With register_globals set to on (this is a bad idea) then all you have to do to use a passed value is create a variable of the same name as you had done. With register_globals on the proper way would have been to declare it first like so:

$name = $HTTP_POST_VARS["name"];
$name = $HTTP_GET_VARS["name"];

That is the proper way.

In the latest versions of PHP the register globals setting is set to off by default so in that case you cannot automatically access thos passed values anymore by just declaring a variable of the same name as that tutorial suggests. Now to access that passed variable you would need to declare it like so:

$name = $_POST["name"];
$name = $_GET["name"];

ScottInTexas
02-21-2003, 04:08 PM
Thanks for clearing that up. That is why my other test worked and this one didn't. I did find a link that described the .htaccess entry for global variables, but putting that in the file (using notepad) and restarting apache didn't seem to do anything. I guess I'll take that out and just use the sample you provided.

Should I always initialize the variables like $name=_POST["name"] or is using the <?php echo $_POST["name"]; ?> as good or no different?

edited post:

BTW I tried this
<head>
<?php
$name=_GET["name'] <--- line 6
?>
</head>

<body>
I sent this <?php $name ?>
...
</body>

But I get an error on line 6

Thanks again,

Íkii
02-21-2003, 04:56 PM
all declarations in php end with a ;

that should clear your error.

$name=_GET["name'];

Nightfire
02-21-2003, 05:45 PM
I'm surprised okii didn't pick up on this :p

$name=_GET["name']

should be

$name=$_GET["name"];

3 errors,

1. No ; at the end of the line
2. No $ at the start of the global
3. Incorrect use of " and ' around the name, you can use a set of " or ', but not like "name' or 'name"

Spookster
02-21-2003, 05:47 PM
Ooops. That's what I get for posting early in the morning. :o

ScottInTexas
02-21-2003, 06:29 PM
Thanks All,

I was able to fix the problems within this tutorial. There are still problems but I got the gist of it. I was able to open a DB and gather the data, so the prupose was served. I see a major problem with variables though. One statement in the code was written like a javascript if. A button was defined in a form - "<input Type="submit" name="submitbutton" value="Submit">"




if ($submitbutton=="Submit"){
echo("Submit is true");
.
.
}



The problem is the if never returned true even though nothing was changing the name value pair of the button.

Open to input.

SYP}{ER
02-22-2003, 03:43 AM
Same thing buddy.


if ($_POST["submitbutton"]=="Submit"){
echo("Submit is true");
.
.
}

Don't forget the $_POST or $_GET (depends on your <form> method).

Variables transfered through the query string (page.php?variable=value) are always $_GET. Variables that are transfered through forms where method="post" are always $_POST :)

That help?

ScottInTexas
02-22-2003, 02:50 PM
Thanks Sypher,

Here is another example I tried. To me the problem seems like calling a javascript with the wrong name except no error is invoked.



<body>

<?php
if($submit){
echo "the $_Post array contains<br /><br />";
while(list($key,$var)=each($_POST)){echo "$key , $var<br />";}
}
?>
<form name="bella" method="post" action="<?echo $_SERVER[PHP_SELF];?>">
<input type="hidden" name="hidden_value" value="a hidden value">
<input type="text" name="text_value" value="">
<textarea name="textarea_value"></textarea>
<select name="select_value">
<option value="select_val_1">value 1</option>
<option value="select_val_2">value 2</option>
<option value="select_val_3">value 3</option>
</select>
<input type="submit" name="submit" value="submit">


</body>


This was lifted right out of a sample tutorial and pasted into the body of my doc. It is saved with the php extension and run from localhost. The form shows up and the submit does submit the data but is does not show the true side of the IF.

I have to assume that it worked before the authro put it up as a sample.

Íkii
02-22-2003, 04:37 PM
same again

if($_POST['submit'])

or better

if($_POST['submit'] == "submit")

Spookster
02-22-2003, 06:39 PM
There is a nice little function in PHP called isset(). This is what you should use in this situation:




<?php

$submit = $_POST["submit"];

if(isset($submit)){
echo "the $_POST array contains<br /><br />";
while(list($key,$var)=each($_POST)){echo "$key , $var<br />";}
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum