PHP and MySQL Basics: Saving form input in PHP variables

form can add interactivity to your site, it enables you to get information or comments from your visitors. saving a value from a form input to a php variable is very easy. refer to the example below.

Example 1: Processing forms in the same php file

Copy and Paste the code below to your text editor and save it as form.php, then run it on your localhost

<html>
<head>
<title>Saving Form Input in PHP Variables</title>
</head>
<body>

<form method="post">
Enter message : <input type="text" name="msg">
<input type="submit" value="Print Msg">

<?php
// stores the form data into a php variable
$msg = $_POST['msg'];
//Prints the data retrieve from the form
echo "You said: <b>$msg</b>";
?>

</body>
</html>

Example 2: Processing Form using external php file

Copy and Save the code below to your text editor and save it as form2.php

<html>
<head>
<title>Saving Form Input in PHP Variables</title>
</head>
<body>

<form action="msghandler.php" method="post">
Enter message : <input type="text" name="msg">
<input type="submit" value="Print Msg">

</body>
</html>

Copy and Save the code below to your text editor and save it as msghandler.php in the same folder where form2.php is located, this is the php script that will process the form2.php above

<?php
// stores the form data into a php variable
$msg = $_POST['msg'];
//Prints the data retrieve from the form
echo "You said: <b>$msg</b>";
?>

You Should Get this output in your browser. .

Before clicking the print button

Saving Form Input Into a PHP Variable

After Clicking the print button

Saving Form Input Into a PHP Variable

Share and Bookmark this Post..

  • Digg
  • Yahoo! Buzz
  • Twitter
  • Facebook
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
  • Technorati
  • Reddit
  • Sphinn
  • MySpace
  • Slashdot
  • BlinkList
  • Live
  • Mixx
  • NewsVine
  • Yahoo! Bookmarks
  • Ratimarks
  • LinkedIn





Tags: ,

2 Responses to “PHP and MySQL Basics: Saving form input in PHP variables”



Leave a Reply

This blog will make your link do follow after you leave 5 comments. i check every comment posted and reply to each also. please do not spam i hate spam, there is no room for spam here in my blog thanks!

CommentLuv Enabled
:argh: :| :$ :B :cool: :*( :curious: x_x :gasp: =D :happy: :/ :D :injured: :lol: :lol2: :love: :X :meh: O_o :ohhyeahh: D: :omg: :raisebrow: :P :( 0_0 :shocked: :sly: :) :] ;] :worry: :wow:

Subscribe to feed

this blog use Comment Luv

Featured Post


Make your Wordpress URL Search Engine Friendly
Simple steps to make your wordpress url search engine friendly..


Give some luv with comment luv
This blog is now comment luv powered..


Fix Wordpress PHP fatal error memory size exhauster
Ways on how to solve PHP fatal error or PHP memory limit on wordpress blogs anr other opensource web apps..


Download Norton Antivirus Internet Security for free
Read this post to know how you can download a free norton antivirus or internet security..


Sponsored Links


 5 Users Online