Post to String PHP

Post to String PHP thumbnail
Web forms can include text fields, drop-down lists, check boxes and buttons.

Websites can use HTML form elements to capture and process user input. When a user submits information through a Web page form, this is normally sent to another page or a script running on the Web server in a language such as PHP. Websites can use PHP code to access and process the user data passed from forms, sometimes returning information back to the user's browser or submitting their data to a database.

  1. Web Forms

    • HTML forms use opening and closing form tags, as the following sample markup code demonstrates:

      <form action="input_data.php" method="post">
      <!--HTML input elements here-->
      </form>

      Inside this form, the page can present user input elements. The opening form tag specifies a server-side script in PHP as the action attribute. This is the script the user data is sent to when the form is submitted. To do this using a standard Submit button, forms can include the following markup, normally after all other input elements have been listed:

      <input type="submit" />

      Pressing this button causes any entered data to be sent to the PHP script.

    Input Elements

    • To capture user data that can be processed in PHP, Web pages can include name attributes in the input elements within a form, as the following sample markup demonstrates:

      What is your favorite food? <input type="text" name="favfood" />

      The name attribute acts as a reference point in the PHP script. Using the post variable and the name attribute value, the PHP script can access the text entered by the user into this field. Developers can use this pattern with all of their input elements, making sure they use unique names for each unique element.

    PHP Capture

    • Inside the PHP script -- for example, the "input_data.php" file -- the post variable provides access to user entered data, as the following sample PHP code demonstrates:

      $_POST["favfood"]

      This code provides a reference to the data entered by the user in the text field with "favfood" as its name attribute. The PHP code can carry out further processing using the variable -- for example, building it into a query string to execute on a database. The PHP script can access each item in the post variable using this syntax, altering the name attribute reference accordingly.

    String Variables

    • Once a PHP script has access to post data values, it can store these as variables or output them to the user's browser. The following extended code demonstrates storing a passed value as a string variable:

      $fav_food = $_POST["favfood"];

      The code can refer to this variable at any subsequent point in processing. The following sample code demonstrates writing the value to the user's browser as part of the Web page, along with HTML structures and text:

      echo "<p>Your favorite food is: ".$fav_food."</p>";

Related Searches:

References

Resources

  • Photo Credit Hemera Technologies/AbleStock.com/Getty Images

Comments

Related Ads

Featured