Jump to content

[SOLVED] Variable trouble -- beginner


soma12

Recommended Posts

I'm a beginner to PHP (and programming in general). Working from a book with the following code and the sample file included in the CD is not working. The code is below. I'm running Windows XP and XAMPP

 

<html>

<head>

<title>What's your name?</title>

</head>

<body>

<h1>What's your name?</h1>

<h3>Writing a form for user input</h3>

<form method = "get"

      action = "hiUser.php">

Please type your name:

<input type = "text"

      name = "userName"

      value = "">

<br>

<input type = "submit">

 

</form>

</body>

</html>

 

 

The above code refers to a php file, below

 

<html>

<head>

<title>Hi User</title>

</head>

<body>

<h1>Hi User</h1>

<h3>PHP program that receives a value from "whatsName"</h3>

 

<?

 

  print "<h3>Hi there, $userName!</h3>";

 

?>

 

</body>

</html>

 

When I try it out in firefox, running through //localhost.... second page looks like this "Hi There, !". I can't get the $userName to work. I've tried adding "php" to <?php.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/119758-solved-variable-trouble-beginner/
Share on other sites

1. when posting here wrap all code elements in [ php] and [/ php]

2. change

<?

 print "<h3>Hi there, $userName!</h3>";

?>

to

<?php //ALWAYS use long php tags for compatibility
echo "<h3>Hi there, $_GET['userName']!</h3>"; //echo in generaly better then print (IMO)
?>

 

And when you use a for with method='get' you need access the variable as $_GET['variableName'] if you use method='post' (more secure) you will use $_POST['variableName']

When was the book published? I ask this because the example you posted assumes that register_globals is enabled which hasn't been the default since about 2003/2004.

 

All references to values coming from forms need to written as

<?php
$x = $_GET['name']; // for method="get" or no method or a value on a URL
$y = $_POST['anothername']; // for method = "post"
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.