简体   繁体   English

发送PHP表单数据

[英]Sending PHP Form Data

I'm trying to create an online order form using PHP. 我正在尝试使用PHP创建一个在线订购表单。

I've already got it working using PHP Checkboxes for each item. 我已经使用每个项目的PHP Checkboxs使它工作。

But now I want to remove the Checkboxes - replacing them with a Quantity box and a Message box for each item. 但是现在我要删除复选框-用每个项目的“数量”框和“消息”框代替它们。

Here is how I currently have my order form setup with checkboxes (I removed the checkbox from the 1st item "Mexican Tortas," and added the Quantity & Message boxes as an example). 这是我目前使用复选框设置订单表单的方式(我从第一项“墨西哥玉米饼”中删除了该复选框,并添加了“数量和消息”框作为示例)。

What is the best way to go about this, so that only the items that have data entered into the Quantity box (and Message box) will be sent to my email? 最好的解决方法是什么,以便只有将数据输入到“数量”框(和“消息”框)中的项目才会发送到我的电子邮件中?

HTML: HTML:

<div class ="item_left">
<img src="images/mexicantortas.jpg" border="2" width="200px" height="150px"><br>
Mexican Torta - $8.50<input name="item" type="hidden" value="Mexican Torta"/><br>
How Many? <input name="quantity" type="text" style="width: 20px; height: 12px;"/><br>
<input name="message" type="text" value="Enter special order instructions here..."   style="max-width: 200px; height: 30px;"/>
</div><!-- ITEM_LEFT -->

<div class ="item_center">
<img src="images/fishsandwich.jpg" border="2" width="200px" height="150px"><br/>
Fish Sandwich - $8.50<input name="item" type="hidden" value="Fish Sandwich"/><br>
<input type="checkbox" name="check[]" value="Fish Sandwich"><br/>
</div><!-- ITEM_CENTER -->

<div class ="item_right">
<img src="images/hamburgers.jpg" border="2" width="200px" height="150px"><br/>
Hamburger w/ Fries - $7.00
<input type="checkbox" name="check[]" value="Hamburger"><br/>
</div><!-- ITEM_RIGHT -->

PHP: PHP:

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

$to = "test@websitehere.com"; 
$subject = "Form Tutorial";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$option = $_POST['radio'];
$dropdown = $_POST['drop_down'];

foreach($_POST['check'] as $value) {
$check_msg .= "Checked: $value\n";
}

$body = "From: $name_field\n E-Mail: $email_field\n $check_msg
    Option:$option\n    Drop-Down: $dropdown\n Message:\n $message\n";

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

 } else {
echo "blarg!";
}
?>

First off, you have a bunch of things with the same name not allowing you to distinguish them on the server side. 首先,您有一堆同名的东西,不允许您在服务器端对其进行区分。 Second, are you wanting each email to send you just 1 item or more than 1 item if they choose more than 1 item? 其次,您是否希望每封电子邮件仅向您发送一项或如果他们选择一项以上,则向您发送一项以上?

Either way what you are wanting to do I think is to validate and check if the values are equal to "" or 0 depending on your select box. 无论哪种方式,我都想验证并检查值是否等于“”或0(取决于您的选择框)。 Second, take these variables and populate them into an array. 其次,获取这些变量并将其填充到数组中。 Then, you could loop through your array appending the items to your $message using html to separate them. 然后,您可以遍历数组,使用html将项目附加到$ message中,以将它们分开。

Mind you, this is just something quick I typed up in about 10 mins, so I'm sure there is a better way to do this, but, it gets the job done. 请注意,这只是我在大约10分钟内输入的快速内容,因此,我相信有更好的方法可以完成此操作,但是它可以完成工作。

PHP: PHP:

if(isset($_POST['submit'])) {
    $to = "test@websitehere.com"; 
    $subject = "Form Tutorial";
    $name_field = $_POST['name'];
    $email_field = $_POST['email'];

    $order = array();
    foreach($_POST['item'] as $item => $name) {
        if ($_POST['quantity_'.$name] > 0) {
            $add_order = array('pretty'=>$_POST['pretty-name_'.$name],'qty'=>$_POST['quantity_'.$name],'message'=>$_POST['message_'.$name]);
            array_push($order,$add_order);
        }
    }

    $body = "From: $name_field\nE-Mail: $email_field\n";

    $body .= "Their Order:\n";
    foreach ($order as $item){
        $body .= "--".$item['qty']."x ".$item['pretty']."\n
        Extra: ".$item['message']."\n\n";
    }

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

HTML: HTML:

<form action="test.php" method="post">
<div class ="item_left">

Mexican Torta - $8.50<input name="item[]" type="hidden" value="torta"/>
<input name="pretty-name_torta" type="hidden" value="Mexican Torta"/><br>
How Many? <input name="quantity_torta" type="text" /><br>
<input name="message_torta" type="text" value="Enter special order instructions here..." />
</div><!-- ITEM_LEFT -->
<br />
<div class ="item_center">

Fish Sandwich - $8.50<input name="item[]" type="hidden" value="fish"/>
<input name="pretty-name_fish" type="hidden" value="Fish Sandwhich"/><br>
How Many? <input name="quantity_fish" type="text" /><br>
<input name="message_fish" type="text" value="Enter special order instructions here..." />
</div><!-- ITEM_CENTER -->
<br />
<div class ="item_right">

Hamburger w/ Fries - $7.00<input name="item[]" type="hidden" value="hamburger"/>
<input name="pretty-name_hamburger" type="hidden" value="Hamburger"/><br>
How Many? <input name="quantity_hamburger" type="text" /><br>
<input name="message_hamburger" type="text" value="Enter special order instructions here..." />
</div><!-- ITEM_RIGHT -->
<br /><br />
<input type="submit" name="submit" value="Order" />
</form>

OUTPUT: (Similar to) 输出:(类似于)

Their Order:
--22x Mexican Torta
Extra: Enter special order instructions here...

--1x Fish Sandwhich
Extra: Lots of stuff

--2x Hamburger
Extra: Mmmm Fries

Jared is right. 贾里德是对的。 You need to have a way to distinguish between the different items. 您需要一种方法来区分不同的项目。 Hence why I added a extra hidden input in there. 因此,为什么要在其中添加额外的隐藏输入。

Long story short, Every item gets put into an array. 长话短说,每个项目都放在一个数组中。 PHP goes through this array and picks out what has a quantity value greater than 0. If it has, add that items info into a second array. PHP遍历此数组,并选择数量值大于0的值。如果有,则将该项目信息添加到第二个数组中。 The last half of this, goes through that new array, and makes it look all nice and pretty for you. 最后一部分将遍历该新数组,并使其看起来对您来说都很漂亮。

If you need me to clarify or fix anything, lemme know. 如果您需要我澄清或解决任何问题,请联系我。

EDIT: Went ahead and added it into the mail function (boredom). 编辑:继续前进,并将其添加到邮件功能(无聊)中。

PS: I noticed you were using unsanitized varables. PS:我注意到您使用的是未经消毒的变量。 I probably shouldn't need to, but I will say it anyways. 我可能不需要,但无论如何我都会说。 Make sure you are checking and cleaning user input before you do much with it. 在进行大量操作之前,请确保检查并清理用户输入。 Probably not that big of a deal here, but it could still turn bad in certain situations. 在这里可能没什么大不了的,但是在某些情况下它仍然可能变得糟糕。

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

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