New to PHP

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
Rmias
Forum Newbie
Posts: 24
Joined: Wed Nov 26, 2003 11:02 am

New to PHP

Post by Rmias »

Hi Can someone tell me on how to pass form values to a php page and display those values. As I am new to php I tried every possible way to do this without a success. Am I missing something or need to configure php to read the variables. here are the create form.php and processform.php.
---------------------------------------------------------------------------
<html>
<head>
<title>Create a form</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<form action="processForm.php" method="get">
Enter your name :
<input type="text" name="username"><br>
Where do you live :
<input type="text" name="region"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
---------------------------------------------------------------------------
<html>
<head>
<title>Process Form Data</title>
</head>
<body>
<h3>Your information has been processed.</h3>
<?php
print "Thank you $username<br>";
Print "You live in :$region";
?>
</body>
</html>

I am using PHP 4.2.3 on Apache web server

Thank you
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

read this

viewtopic.php?t=511

and your questions will be answered.

basically you need to do this

Code: Select all

print "Thank you ".$_GET['username']."<br>";

Mark
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

try this

form.php <---No need for this to be a php file, it can be plain html.

Code: Select all

<html>
<head>
<title>Create a form</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<form action="processForm.php" method="post"//i changed your method to post from get>
Enter your name :
<input type="text" name="username"><br>
Where do you live :
<input type="text" name="region"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
heres processform.php

Code: Select all

<html>
<head>
<title>Process Form Data</title>
</head>
<body>
<h3>Your information has been processed.</h3>
<?php
$username = $_POST['username'];//you have to define the $vars as info thats gonna get pulled out of the form
$region = $_POST['region'];
print "Thank you $username<br>";
Print "You live in :$region";
?>
</body>
</html>
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

Code: Select all

<html>
<head>
<title>Process Form Data</title>
</head>
<body>
<h3>Your information has been processed.</h3>
<?php
$username = $_POST['username'];//you have to define the $vars as info thats gonna get pulled out of the form
$region = $_POST['region'];
print "Thank you $username<br>";
Print "You live in :$region";

</body>
</html>
sorry i forgot to close the syntax highlighter tag
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

you can edit post you know? ;)
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

I'm also new to php and would like some clarification. I have done something similar to what rmias is talking about. I used the Post method, but I did not use $_POST['variable'] and my code still worked. Is there a reason to use the $_POST that I should be aware of?

Here is my code:

Form page

Code: Select all

<html>
<head>
<title>This is the input page</title>
</head>

<body bgcolor = #ffffff>

<h2>This is a test</h2>

<form action = "test.php" method = "post">

Salesperson: <input type="text" name="salesperson">
<p>
Cost of Car: <input type="text" name="cost">
<p>
<input type="submit" name="submit" value="Calculate commission">
</form>
</body>
</html>
Here is the php:

Code: Select all

<html>
<head>
<title>This is a test page.</title>
</head>

<body bgcolor = #ffffff>
<?php
$commission_percent = .2;

function Commission ($cost, $commission_percent, $salesperson) &#123;
	$commission_total = ($cost * $commission_percent);
	
	echo "$salesperson has sold a car for \$$cost."; 
	echo "<p>";
	echo "$salesperson's commission is \$$commission_total";
	
&#125;

commission ($cost, $commission_percent, $salesperson);
?>
</body>
</html>
It's just something I threw together as a test. I would much prefer to write proper code than just trash that works.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

may want to view :

viewtopic.php?t=511
Rmias
Forum Newbie
Posts: 24
Joined: Wed Nov 26, 2003 11:02 am

Post by Rmias »

Thank you very much for all your help. The php displays the result I wanted.

Rmias
Post Reply