简体   繁体   English

如何将输入值插入数组数据php?

[英]How to insert input value into array data php?

I created a form like this, How should I do so that the value of the form field is entered into the php data array?我创建了一个这样的表单,我应该怎么做才能将表单字段的值输入到php数据数组中?

Expected that the value of the submitted data is entered into this line:期望将提交数据的值输入到这一行:

$customer_details = array(
  'first_name'    => "value input first_name",
  'email'         => "value input email",
  'phone'         => "value input phone"
);

form input:表单输入:

<form>
  <label>Name:</label>      
  <input name="first_name" type="text" maxlength="50"><br>
  <label>Phone:</label> 
  <input name="phone" type="text" maxlength="100"><br>
  <label>Email:</label> 
  <input name="email" type="text" maxlength="100"><br><br>
  <button id="pay-button">Pay!</button>
</form>

The php code and html form are in 1 file. php 代码和 html 格式在 1 个文件中。

Your form needs 2 attributes (action, method) and your button needs 1 (type) :您的表单需要 2 个属性(操作、方法),您的按钮需要 1 个(类型):

<form action="#" method="GET">
  <label>Name:</label>      
  <input name="first_name" type="text" maxlength="50"><br>
  <label>Phone:</label> 
  <input name="phone" type="text" maxlength="100"><br>
  <label>Email:</label> 
  <input name="email" type="text" maxlength="100"><br><br>
  <button type="submit" id="pay-button">Pay!</button>
</form>

So the submit button will handle the form using the same page.所以提交按钮将使用同一页面处理表单。

You can check if the form is handled using this :您可以使用以下方法检查表单是否被处理:

if(isset($_GET['name'])) {
    //use your form
}

Then you have an existing array $_GET .然后你有一个现有的数组$_GET You can access your datas using the name of your input (ex : $_GET['first_name'] ).您可以使用输入的名称访问您的数据(例如: $_GET['first_name'] )。

If you want to use your $customer_array , then :如果你想使用你的$customer_array ,那么:

$customer_details = array(
  'first_name'    => $_GET['first_name'],
  'email'         => $_GET['email'],
  'phone'         => $_GET['phone']
);

UPDATED更新

The final result should be :最终结果应该是:

if(isset($_GET['name'])) {
    
    $customer_details = array(
      'first_name'    => $_GET['first_name'],
      'email'         => $_GET['email'],
      'phone'         => $_GET['phone']
    );

    //Any other treatment...

} else {

  <form action="#" method="GET">
    <label>Name:</label>      
    <input name="first_name" type="text" maxlength="50"><br>
    <label>Phone:</label> 
    <input name="phone" type="text" maxlength="100"><br>
    <label>Email:</label> 
    <input name="email" type="text" maxlength="100"><br><br>
    <button type="submit" id="pay-button">Pay!</button>
  </form>

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM