PHP: User Input

Written by Evan Petersen on Saturday, 22 October 2011. Posted in Programming

Handling HTML forms with PHP

Learn how to handle form input with PHP.  Topics include HTML forms, GET/POST variables and output.  If you made it through "PHP: Getting Started," then this tutorial is right for you!

 

INTRODUCTION

There are two basic concepts that drive every program ever created; input and output.  Without these two essential components, programs would accomplish next to nothing.  In "PHP: Getting Started", we covered basic output with the traditional "hello world" script.  Truth be told, there would be no reason to create a PHP script to simply output static text -- that's HTML's job.  

 

GOAL

In this script, we are going to output text based on a user's input, something that a strict HTML page can't do.  So open Notepad++ and we'll get started.

 

WRITING THE SCRIPT

As always, we will need to begin with a <?php tag so that our script is processed as PHP code.  Additionally, we are going to check for user input with a basic if statement.  

 

<?php
if (strlen($_GET['input'])>0) {
   echo "You said: ".$_GET['input'];
}
?>

 

On line two, you'll see that I use the PHP function strlen().  This function computes a given string's length, otherwise known as the number of characters present.  I am essentially checking the variable $_GET['input'] (which will be the variable that contain's our user input) for the presence of text.

 

$_GET['{varname}'] is one of two ways to retrieve user input.  The other method is $_POST['{varname}'].  The two are very similar with one difference; get variables are appended to the end of a URL.  If you ever see a URL with something like ?var=0&var2=1, then you've submitted a form using get variables.  Post variables, on the other hand, are not present in the URL and are, for all intents and purposes, not visible to the user.

 

So, how do we set up the HTML form to send a get variable? Well, it's a lot easier than you may think.

 

<form action="" method="get">
   <input name="input" value="Enter a string!" />
   <input type="submit" value="Submit" />
</form>

 

On line one, we specify that the method is get rather than post.  If all works as planned, everything that the user types should appear in the url following a ?input={user input}. 

 

PUTTING IT ALL TOGETHER

So, combine the two and you should have something that looks like this:

 

<?php
if (strlen($_GET['input'])>0) {
   echo "You said: ".$_GET['input'];
}
?>
<form action="" method="get">
   <input name="input" value="Enter a string!" />
   <input type="submit" value="Submit" />
</form>

 

PHP will pick up on the fact that there is data present after the user submits the form in the get variable "input" and will execute our IF block, thus returning You said: +the user's input.

 

And there you have it, your first dynamic PHP script!  If you have any questions regarding the information in this tutorial, leave a comment!

4.0/5 rating (1 votes)

About the Author

Evan Petersen

My name is Evan Petersen and I work as Chief Technology Officer at Dotcomjungle in Ashland, Oregon. You can visit the home page of my blog at: www.EvanPetersen.com.

I enjoy reserarching new methods to solve age old problems. Hopefully you'll find something of use!

Follow me on G+

Comments (6)

  • Beginner in PHP

    Beginner in PHP

    30 June 2013 at 06:50 |
    HI there! can you advise on how to get the substring from a user input text?
    • Evan Petersen

      Evan Petersen

      30 June 2013 at 11:37 |
      Certainly! PHP has a few different functions that are capable of returning a substring. The most applicable to what I am assuming you are asking about is the function

      string substr ( string $string , int $start [, int $length ] )

      (found here: http://php.net/manual/en/function.substr.php). With this function, we can pass in user input ($_GET['input']) and pass a start value (return the string that starts after x characters) and an optional length value (return x characters after start y). Let's take a look at some examples from php.net:

      echo substr('abcdef', 1); // bcdef
      echo substr('abcdef', 1, 3); // bcd
      echo substr('abcdef', 0, 4); // abcd
      echo substr('abcdef', 0, 8); // abcdef
      echo substr('abcdef', -1, 1); // f

      Now to get the substring of a user input variable, all we have to do is pass the user input as the first argument. Here goes:

      echo substr($_GET['input'], 1, 5);

      Now, I should warn you... If you are looking to perform any operation with user input, be sure that you are performing some sort of sanitization. When you start dealing with a database, take a look at the following post: http://www.evanpetersen.com/item/what-is-pdo-and-why-should-i-use-it.html. Additionally, do your best to verify that the user input is what you're expecting and not malicious.

      Regards,
      Evan
  • greatest string quartets

    greatest string quartets

    03 November 2012 at 15:14 |
    Hi there! This post could not be written any better! Reading this
    post reminds me of my previous room mate! He always kept talking about this.
    I will forward this post to him. Fairly certain he will have a good read.

    Thank you for sharing!
  • Auth

    Auth

    04 May 2012 at 19:34 |
    You can use sleep command to pause a bash scirpt.i=1while [ "$i" != "10" ];do i=`expr $i + 1` echo number $i sleep 1sdoneYou can use 1s for one second, 1m for one minute and 1h for one hour.sleep 1ssleep 1msleep 1hSimilarly you can change the number from 1 to whatever you want. For example, sleep of 25 seconds can be achieved by 25s ans so on.To pause a bash scirpt for user input like ENTER, you can use read command.echo Press any key to continue..read mykeyecho Thanks for pressing a key. I am going to continue Read these articles for more information.Like or Dislike: +1
  • exercise bike

    exercise bike

    02 January 2012 at 05:58 |
    Glad to read this blog! Keep it going!
  • Ryan

    Ryan

    22 October 2011 at 12:14 |
    Again, another helpful article. Thanks for writing this guide!

Leave a comment

You are commenting as guest. Optional login below.