Jump to content

[SOLVED] Need help with a form


popoval

Recommended Posts

Hi.

I have just started learing PHP and have run into trouble.

I am trying to create a form.

on the first page (testform.php) i have this code:

 

<html>

<head>

<title>PHP Test</title>

</head>

<body>

<?php echo '<p>Hello World</p>';

?>

<br>

<form action="/processForm.php" method="get">

Enter youre name

<input type="text" name="userName"><br>

<input type="submit" name="submit">

</form></body>

</html>

If I enter my Name "Alex" an click Submit it openes the next page: ProcessForm.php?user_name=alex&submit=Submit+Query

 

On that page I have this code:

<html>

<head>

<title>PHP Test</title>

</head>

<body>

<?php

print "hi $userName<br>";

?></body>

</html>

 

According to my book the page would now display:

Hi alex

 

But it does not work. What am I doing wrong?

 

Thanks

Alex

Link to comment
https://forums.phpfreaks.com/topic/45402-solved-need-help-with-a-form/
Share on other sites

try using an echo "$userName";

or print ("$userName");

 

o yea, the form action should probobally be post, or you could put session_start(); at the top of the page (in php tages)

 

I did try with both echo "$userName"; and print ("$userName");

but did not work.

 

I must say I did not understand wat you ment with session_start();

where to you want me to put it? as first line after <?php ?

What should be between () ?

Simply try this

 

<?php
echo phpinfo();
?>

 

Or try ur form in this way.

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>';
?>
<form action="processForm.php" method="post">
Enter youre name
<input type="text" name="userName">

<input type="submit" name="submit">
</form>
</body>
</html> 

 

processForm.php code

 

 
<?php
If(isset($_POST['submit']))
{
$username=$_POST['userName'];
echo $username;
}
?>

first page:

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>';
?> 


<form action="/processForm.php" method="post">
Enter youre name
<input type="text" name="userName">

<input type="submit" name="submit">
</form></body>
</html> 

 

second page:

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
$userName = $_GET['userName'];
print "hi $userName";
?></body>
</html> 

or

firstpage:

<?php session_start(); ?>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>';
?> 


<form action="/processForm.php" method="get">
Enter youre name
<input type="text" name="userName">

<input type="submit" name="submit">
</form></body>
</html>

 

second page:

<?php session_start(); ?>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
$userName = $_GET['userName'];
print "hi $userName";
?></body>
</html> 

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.