通过PHP提交数据的最简单方法?

通过PHP提交数据的最简单方法?,php,html,Php,Html,我是PHP新手,花了10个小时试图解决这个问题 目标是获取此订单中输入的所有数据,并通过PHP发送到我的电子邮件 我有两个问题: 1.我可以让PHP从单个菜单项(例如:墨西哥玉米饼)发送数据,但如何让PHP从多个菜单项(例如:墨西哥玉米饼、鱼三明治和汉堡)发送数据 2.如何告诉PHP不要从没有填写“多少”或“自定义”文本字段的菜单项发送数据 如果你能提供一个超级简单的例子(或者一个学习资源的链接),我会非常感激 谢谢,, 亚比雅 PHP <?php if(isset($_POST['su

我是PHP新手,花了10个小时试图解决这个问题

目标是获取此订单中输入的所有数据,并通过PHP发送到我的电子邮件

我有两个问题:

1.我可以让PHP从单个菜单项(例如:墨西哥玉米饼)发送数据,但如何让PHP从多个菜单项(例如:墨西哥玉米饼、鱼三明治和汉堡)发送数据

2.如何告诉PHP不要从没有填写“多少”或“自定义”文本字段的菜单项发送数据

如果你能提供一个超级简单的例子(或者一个学习资源的链接),我会非常感激

谢谢,, 亚比雅


PHP

<?php
if(isset($_POST['submit'])) {

$to = "test@mywebsite.com"; 
$subject = "New Order";
$name_field = $_POST['name'];
$phone_field = $_POST['phone'];
$item = $_POST['item'];
$quantity = $_POST['quantity'];
$customize = $_POST['customize'];

}

$body = "Name: $name_field\nPhone: $phone_field\n\nItem: $item\nQuantity: $quantity\nCustomize: $customize";

echo "Data has been submitted to $to!";
mail($to, $subject, $body);

?>


HTML

<form action="neworder.php" method="POST">

<div class ="item">
<img style="float:left; margin-right:15px; border:1px Solid #000; width:200px; height:155px;" src="images/mexicantortas.jpg">
<h1>Mexican Torta - $8.50</h1>
<input name="item" type="hidden" value="Mexican Torta"/>
<h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2> 
<input name="quantity" type="text"/>
<h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3> 
<textarea name="customize"/></textarea>
</div><!-- ITEM_LEFT -->

<div class ="item">
<img style="float:left; margin-right:15px; border:1px Solid #000; width:200px; height:155px;" src="images/fishsandwich.jpg">
<h1>Fish Sandwich - $8.50</h1>
<input name="item" type="hidden" value="Fish Sandwich"/>
<h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2> 
<input name="quantity" type="text"/>
<h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3> 
<textarea name="customize"/></textarea>
</div><!-- ITEM_LEFT -->

<div class ="item">
<img style="float:left; margin-right:15px; border:1px Solid #000; width:200px; height:155px;" src="images/hamburgers.jpg">
<h1>Hamburger w/ Fries - $7.00</h1>
<input name="item" type="hidden" value="Fish Sandwich"/>
<h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2> 
<input name="quantity" type="text"/>
<h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3> 
<textarea name="customize"/></textarea>
</div><!-- ITEM_LEFT -->

<div class="horizontal_form">
<div class="form">
<h2>Place Your Order Now: <font size="3"><font color="#037B41">Fill in the form below, and we'll call you when your food is ready to be picked up...</font></font></h2>
<p class="name">
<input type="text" name="name" id="name" style="text-align:center;" onClick="this.value='';" value="Enter your name"/>
</p>
<p class="phone">  
<input type="text" name="phone" id="phone" style="text-align:center;" onClick="this.value='';" value="Enter your phone #"/>
</p>
<p class="submit">
<input type="submit" value="Place Order" name="submit"/>
</p>
</div><!-- FORM -->
</div><!-- HORIZONTAL_FORM -->

</form>

墨西哥玉米饼-8.50美元
有多少例:1,2,3。。。?
定制它?没有生菜,多加奶酪。。。
鱼三明治-8.50美元
有多少例:1,2,3。。。?
定制它?没有生菜,多加奶酪。。。
汉堡配薯条-7美元
有多少例:1,2,3。。。?
定制它?没有生菜,多加奶酪。。。
现在下订单:填写下表,当您的食物准备好要取时,我们会打电话给您。。。


您需要唯一的表单字段名。换句话说,表单中不能有重复项:

<input name="quantity" type="text"/>

相反,您需要唯一的名称,例如:

<input name="quantity_fish" type="text"/>


然后,您可以使用PHP解析$\u POST,并根据需要抛出空字段。

您需要唯一的表单字段名。换句话说,表单中不能有重复项:

<input name="quantity" type="text"/>

相反,您需要唯一的名称,例如:

<input name="quantity_fish" type="text"/>


然后,您可以使用PHP解析$\u POST,并根据需要抛出空字段。

我建议您使用“[]”,因为它将多个输入字段分组到一个数组中

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);

  $to = "test@mywebsite.com"; 
  $subject = "New Order";

  $order = array();
  $order['name']  = $_POST['name'];
  $order['phone'] = $_POST['phone'];

  $food = $_POST['food'];

  foreach ($food as $type => $value)
    if (strlen($value['quantity']) > 0) // assuming 'customize' is optional
      $order[$type] = $value;

  print_r($order);
}

?>

<html>
<head>
  <title>Order now!</title>
  <style>label,input,textarea {display:block}</style>
<body>
<?php // You need enctype for '[]' support' ?>
<form action="" method="post" enctype="multipart/form-data">
  <div class ="item">
    <label>Mexican Torta - $8.50</label>

    <b>How Many?</b> 
    <input name="food[mexican_torta][quantity]" type="text">

    <b>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></b> 
    <textarea name="food[mexican_torta][customize]"></textarea>
  </div>

  <div class ="item">
    <label>Fish - $3.50</label>

    <b>How Many?</b> 
    <input name="food[fish][quantity]" type="text">

    <b>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></b> 
    <textarea name="food[fish][customize]"></textarea>
  </div>

  <h2>Place Your Order Now:</h2>

  <em>Fill in the form below, and we'll call you when your food is ready to be picked up.</em>

  <label>Enter your name</label>
  <input type="text" name="name">

  <label>Enter your phone nr.</label>
  <input type="text" name="phone">

  <button>Submit</button>
</form>
</body>

使用“[]”进行一些操作,以获得您真正想要的阵列输出。印刷品会准确地显示你得到的东西。从这里可以很容易地在电子邮件中输入食物详细信息,我建议您使用“[]”,因为它将多个输入字段分组到一个数组中

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);

  $to = "test@mywebsite.com"; 
  $subject = "New Order";

  $order = array();
  $order['name']  = $_POST['name'];
  $order['phone'] = $_POST['phone'];

  $food = $_POST['food'];

  foreach ($food as $type => $value)
    if (strlen($value['quantity']) > 0) // assuming 'customize' is optional
      $order[$type] = $value;

  print_r($order);
}

?>

<html>
<head>
  <title>Order now!</title>
  <style>label,input,textarea {display:block}</style>
<body>
<?php // You need enctype for '[]' support' ?>
<form action="" method="post" enctype="multipart/form-data">
  <div class ="item">
    <label>Mexican Torta - $8.50</label>

    <b>How Many?</b> 
    <input name="food[mexican_torta][quantity]" type="text">

    <b>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></b> 
    <textarea name="food[mexican_torta][customize]"></textarea>
  </div>

  <div class ="item">
    <label>Fish - $3.50</label>

    <b>How Many?</b> 
    <input name="food[fish][quantity]" type="text">

    <b>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></b> 
    <textarea name="food[fish][customize]"></textarea>
  </div>

  <h2>Place Your Order Now:</h2>

  <em>Fill in the form below, and we'll call you when your food is ready to be picked up.</em>

  <label>Enter your name</label>
  <input type="text" name="name">

  <label>Enter your phone nr.</label>
  <input type="text" name="phone">

  <button>Submit</button>
</form>
</body>

使用“[]”进行一些操作,以获得您真正想要的阵列输出。印刷品会准确地显示你得到的东西。从这里可以很容易地将食物详细信息放入电子邮件中

您是否要求PHP只将数据提交到来自表单中填写字段的电子邮件中?如果是这样的话,为什么不编写php脚本来获取所有输入字符串的长度,如果输入字符串为null,则不包括它?您是否要求php只将数据提交到来自表单中填写字段的电子邮件中?如果是这样的话,为什么不编写php脚本来获取所有输入字符串的长度,如果它们为null,则不包括它?您能否提供一个php文件应如何设置的参考示例?我修改了我给出的示例,你可以复制+粘贴它,它应该给你一个基本的理解,我猜。你可以提供一个参考例子,说明如何设置php文件吗?我修改了我给出的例子,你可以复制+粘贴它,它应该给你一个基本的理解,我猜。