Home  >  Article  >  Backend Development  >  How to create an array through a form in php

How to create an array through a form in php

PHPz
PHPzOriginal
2023-04-19 09:19:24585browse

In PHP, array is a very useful data type. By using an array, you can store multiple values ​​into a variable and access the values ​​by index or key. When developing a web application, you may need to use arrays to handle user-submitted form data. In this article, I'll show you how to create an array from a form.

  1. Basic HTML form

First, let's create a basic HTML form to demonstrate how to obtain user-submitted data. Here is a simple form:

<form method="post" action="process-form.php">
  <label for="name">姓名:</label>
  <input type="text" id="name" name="name"><br>

  <label for="email">电子邮件:</label>
  <input type="email" id="email" name="email"><br>

  <label for="phone">电话号码:</label>
  <input type="tel" id="phone" name="phone"><br>

  <input type="submit" value="提交">
</form>

The form contains three input fields: name, email, and phone number. The submit button sends the form data to the "process-form.php" file.

  1. Processing form data

Next, we will process the form data in the "process-form.php" file. Through the $_POST super global variable, you can get the form data submitted by the user. Here is a basic handler:

<?php
  $name = $_POST[&#39;name&#39;];
  $email = $_POST[&#39;email&#39;];
  $phone = $_POST[&#39;phone&#39;];
?>

In this handler, I use $_POST['name'], $_POST['email'] and $_POST['phone'] to access the form Enter the values ​​of the fields and assign them to the variables $name, $email, and $phone respectively.

  1. Use an array to store form data

Now, we have obtained the form data and stored it in a variable. However, sometimes you need to store all form data in an array for easier processing. In PHP, you can create an associative array called $form_data using the following code:

<?php
  $form_data = array(
    &#39;name&#39; => $_POST['name'],
    'email' => $_POST['email'],
    'phone' => $_POST['phone']
  );
?>

In this array, each input field value is associated with a key. For example, the value of the input field "name" will be saved at the key "name" in the associative array.

  1. Accessing data in an associative array

Once you have your form data stored in an associative array, you can access them using the following code:

<?php
  echo "姓名:" . $form_data['name'] . "<br>";
  echo "电子邮件:" . $form_data['email'] . "<br>";
  echo "电话号码:" . $form_data['phone'] . "<br>";
?>

Here, I've used array keys to access the values ​​of each different input field. This makes working with form data much more convenient.

  1. Sending an array to another page

If you need to send an array storing form data to another page, you can use PHP's "$_SESSION" super Global variables. Before sending the array, you need to enable the session with the following code:

<?php
  session_start();
?>

Once the session is enabled, you can store the entire array into the session and access the array in another page.

<?php
  // 存储数组到会话中
  $_SESSION[&#39;form_data&#39;] = $form_data;

  // 转到另一个页面
  header(&#39;Location: view-data.php&#39;);
?>

In another page, you can get the stored array using the following code:

<?php
  // 从会话中获取数组
  $form_data = $_SESSION[&#39;form_data&#39;];
?>

Now, you have learned how to create an array from a form in PHP. Whether you're working on a personal blog or a large web application, working with form data is an important task. By using arrays, you can process and store user-submitted data more easily and reduce the chance of errors.

The above is the detailed content of How to create an array through a form in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn