html表单与php问题


html form with php issue

我制作了一个html表单,用户可以在其中输入自己的姓名/电子邮件/电话等,还可以选择列出的产品数量。

这是产品代码,我想知道如何在我从表单中收到的电子邮件中包含用户想要的产品数量。

这是表单代码:

    <div class="col-sm-6 col-md-6 bottom-padding">
    <?php
        process_si_contact_form();
        if (isset($_SESSION['ctform']['error']) &&  $_SESSION['ctform']['error'] == true):
    ?>
        <div class="alert alert-danger">
            <strong>Oops!</strong> Something went wrong.
        </div>
    <?php
        elseif (isset($_SESSION['ctform']['success']) && $_SESSION['ctform']['success'] == true):
    ?>
        <div class="alert alert-success">
            <strong>Message sent!</strong> We'll get in touch asap.
        </div>
    <?php
        endif;
    ?>
    <form class="form-box register-form contact-form" method="POST" id="contact_form">
        <input type="hidden" name="do" value="contact" />
        <h3 class="title">Form</h3>
        <label>Name: <span class="required">*</span></label>
        <?php echo @$_SESSION['ctform']['f_name_error'] ?>
        <input class="form-control" type="text" name="ct_f_name" value="<?php echo htmlspecialchars(@$_SESSION['ctform']['ct_f_name']) ?>">
        <label>E-mail: <span class="required">*</span></label>
        <?php echo @$_SESSION['ctform']['f_email_error'] ?>
        <input class="form-control" type="email" name="ct_f_email" value="<?php echo htmlspecialchars(@$_SESSION['ctform']['ct_f_email']) ?>">
        <label>Phone: <span class="required">*</span></label>
        <?php echo @$_SESSION['ctform']['f_tel_error'] ?>
        <input class="form-control" type="text" name="ct_f_tel" value="<?php echo htmlspecialchars(@$_SESSION['ctform']['ct_f_tel']) ?>">
        <div class="panel-group" id="accordion">
            <div class="panel panel-info">
                <div class="panel-heading">
                    <h4 class="panel-title">
                        <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" class="collapsed">
                            Hardware
                        </a>
                    </h4>
                </div>
                <div id="collapseOne" class="panel-collapse collapse" style="height: 0px;">
                    <div class="panel-body">
                        <ul class="list-group checked-list-box">
                            <?php
                                $my_products = array(   
                                    '1' => 'Product 1',
                                    '2' => 'Product 2',
                                    '3' => 'Product 3'
                                );
                                foreach ($my_products as $key => $value) {
                                    echo "<div class='"col-xs-6'" style='"margin: 10px 0 5px 0;'">";
                                        echo "<li class='"list-group-item'" data-style='"button'">";
                                            echo $value;
                                            echo "<select class='"form-control'" name='"quantity[$key]>'"";
                                                for ($i = 0; $i <= 10; $i++) echo "<option value='"$i'">$i</option>";
                                            echo "</select>";
                                        echo "</li>";
                                    echo "</div>";
                                }
                            ?>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="panel panel-info">
                <div class="panel-heading">
                    <h4 class="panel-title">
                        <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" class="collapsed">
                            Software
                        </a>
                    </h4>
                </div>
                <div id="collapseTwo" class="panel-collapse collapse">
                    <div class="panel-body">
                        Empty.
                    </div>
                </div>
            </div>
        </div>
        <label>Message: <span class="required">*</span></label>
        <?php echo @$_SESSION['ctform']['message_error'] ?>
        <textarea class="form-control" name="ct_message"><?php echo htmlspecialchars(@$_SESSION['ctform']['ct_message']) ?></textarea>
        <div class="clearfix"></div>
        <div class="buttons-box clearfix">
            <input type="submit" class="btn btn-default" value="Send">
            <span class="required"><b>*</b> Required fields</span>
        </div>
    </form>
</div>

还有什么需要我发布的吗?我不知道这是否是最好的编码方式,但它确实有效。现在它不会让我保存这个问题,因为这篇文章中包含了很多代码,所以我写这篇文章是为了能够保存它。哈哈。

编辑:添加PHP表单代码。

 <?php
        function process_si_contact_form() {
            $_SESSION['ctform'] = array();
            if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['do'] == 'contact') {
            foreach($_POST as $key => $value) {
                if (!is_array($key)) {
                    if ($key != 'ct_message') $value = strip_tags($value);
                    $_POST[$key] = htmlspecialchars(stripslashes(trim($value)));
                }
            }
            $f_name = @$_POST['ct_f_name'];
            $f_tel = @$_POST['ct_f_tel'];
            $f_email = @$_POST['ct_f_email'];
            $message = @$_POST['ct_message'];
            $f_name = substr($f_name, 0, 64);
            $errors = array();
            if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
                if (strlen($f_name) < 3) {
                    $errors['f_name_error'] = 'Your name please.';
                }
                if (strlen($f_tel) < 10) {
                    $errors['f_tel_error'] = 'Your phone please.';
                } else if (!preg_match('/^([-+0-9()]+)$/', $f_tel)) {
                    $errors['f_tel_error'] = 'Thats not a phone number!';
                }
                if (strlen($f_email) == 0) {
                    $errors['f_email_error'] = 'Your e-mail please.';
                } else if (!preg_match('/^(?:['w'd]+'.?)+@(?:(?:['w'd]'-?)+'.)+'w{2,4}$/i', $f_email)) {
                    $errors['f_email_error'] = 'Thats not an e-mail!';
                }
                if (strlen($message) < 10) {
                    $errors['message_error'] = 'Your message must contain atleast 10 characters.';
                }
            }
            if (sizeof($errors) == 0) {
                $time = date('r');
                $message = "<strong>Name:</strong><br /><em>$f_name</em><br />"
                    . "<br />"
                    . "<strong>E-mail:</strong><br /><em>$f_email</em><br />"
                    . "<br />"
                    . "<strong>Phone:</strong><br /><em>$f_tel</em>"
                    . "<br /><br /><br />"
                    . "<strong>Message:</strong><br />"
                    . "<pre>$message</pre>"
                    . "<br /><br />"
                    . "<strong>IP:</strong><br /><em>{$_SERVER['REMOTE_ADDR']}</em><br />"
                    . "<br />"
                    . "<strong>Time:</strong><br /><em>$time</em><br />"
                    . "<br />"
                    . "<strong>Browser:</strong><br /><em>{$_SERVER['HTTP_USER_AGENT']}</em>";
                $message = wordwrap($message, 70);
                if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
                    mail($GLOBALS['ct_recipient'], $GLOBALS['ct_msg_subject'], $message, "From: {$GLOBALS['ct_recipient_no_reply']}'r'nReply-To: {$f_email}'r'nContent-type: text/html; charset=utf8'r'nMIME-Version: 1.0");
                }
                $_SESSION['ctform']['error'] = false;
                $_SESSION['ctform']['success'] = true;
            } else {
                $_SESSION['ctform']['ct_f_name'] = $f_name;
                $_SESSION['ctform']['ct_f_tel'] = $f_tel;
                $_SESSION['ctform']['ct_f_email'] = $f_email;
                $_SESSION['ctform']['ct_message'] = $message;
                foreach($errors as $key => $error) {
                    $_SESSION['ctform'][$key] = "<span class='"error'" style='"float: right; color: 00ff00;'">$error</span>";
                }
                $_SESSION['ctform']['error'] = true;
            }
        }
    }
    $_SESSION['ctform']['success'] = false;
    ?>
EDIT 2: ADDING NEW PHP MAIL CODE.
<?php
    function process_si_contact_form() {
        $_SESSION['ctform'] = array();
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['do'] == 'contact') {
        foreach($_POST as $key => $value) {
            if (!is_array($key)) {
                if ($key != 'ct_message') $value = strip_tags($value);
                $_POST[$key] = htmlspecialchars(stripslashes(trim($value)));
            }
        }
        $f_name = @$_POST['ct_f_name'];
        $f_tel = @$_POST['ct_f_tel'];
        $f_email = @$_POST['ct_f_email'];
        $message = @$_POST['ct_message'];
        $f_name = substr($f_name, 0, 64);
        $products = array(
            @$_POST['quantity[1]'], 
            @$_POST['quantity[2]'],
            @$_POST['quantity[3]'],
            @$_POST['quantity[4]'],
            @$_POST['quantity[5]'],
            @$_POST['quantity[6]'],
            @$_POST['quantity[7]'],
            @$_POST['quantity[8]'],
            @$_POST['quantity[9]'],
            @$_POST['quantity[10]']);
        $errors = array();
        if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
            if (strlen($f_name) < 3) {
                $errors['f_name_error'] = 'Fyll i ditt namn';
            }
            if (strlen($f_tel) < 10) {
                $errors['f_tel_error'] = 'Fyll i ditt tel.nr';
            } else if (!preg_match('/^([-+0-9()]+)$/', $f_tel)) {
                $errors['f_tel_error'] = 'Felaktigt tel.nr';
            }
            if (strlen($f_email) == 0) {
                $errors['f_email_error'] = 'Fyll i din e-postadress';
            } else if (!preg_match('/^(?:['w'd]+'.?)+@(?:(?:['w'd]'-?)+'.)+'w{2,4}$/i', $f_email)) {
                $errors['f_email_error'] = 'Felaktig e-postadress';
            }
            if (strlen($message) < 10) {
                $errors['message_error'] = 'Ditt meddelande måste bestå av minst 10 tecken';
            }
        }
        if (sizeof($errors) == 0) {
            $time = date('r');
            $message = "<strong>Namn:</strong><br /><em>$f_name</em><br />"
                . "<br />"
                . "<strong>E-postadress:</strong><br /><em>$f_email</em><br />"
                . "<br />"
                . "<strong>Telefon:</strong><br /><em>$f_tel</em>"
                . "<br /><br /><br />"
                . "<strong>Meddelande:</strong><br />"
                . "<pre>$message</pre>"
                . "<br />"
                . "<strong>IP:</strong><br /><em>{$_SERVER['REMOTE_ADDR']}</em><br />"
                . "<br /><strong>".$products[1]." - Ingenico IPP350</strong>"
                . "<br /><strong>".$products[2]." - Ingenico ICT250</strong>"
                . "<br /><strong>".$products[3]." - Yomani</strong>"
                . "<br /><strong>".$products[4]." - Ingenico IWL250 GPRS</strong>"
                . "<br /><strong>".$products[5]." - PosBank&reg; AnyShop II</strong>"
                . "<br /><strong>".$products[6]." - Ingenico IWL250 Wifi</strong>"
                . "<br /><strong>".$products[7]." - Ingenico IWL250 BT</strong>"
                . "<br /><strong>".$products[8]." - PosBank&reg AnyShop e2</strong>"
                . "<br /><strong>".$products[9]." - Ingenico IWL285 3G</strong>"
                . "<br /><strong>".$products[10]." - Ingenico iCMP</strong>"
                . "<br /><br /><strong>Tid:</strong><br /><em>$time</em><br />"
                . "<br />"
                . "<strong>Webbläsare:</strong><br /><em>{$_SERVER['HTTP_USER_AGENT']}</em>";
            $message = wordwrap($message, 70);
            if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
                mail($GLOBALS['ct_recipient'], $GLOBALS['ct_msg_subject'], $message, "From: {$GLOBALS['ct_recipient_no_reply']}'r'nReply-To: {$f_email}'r'nContent-type: text/html; charset=utf8'r'nMIME-Version: 1.0");
            }
            $_SESSION['ctform']['error'] = false;
            $_SESSION['ctform']['success'] = true;
        } else {
            $_SESSION['ctform']['ct_f_name'] = $f_name;
            $_SESSION['ctform']['ct_f_tel'] = $f_tel;
            $_SESSION['ctform']['ct_f_email'] = $f_email;
            $_SESSION['ctform']['ct_message'] = $message;
            foreach($errors as $key => $error) {
                $_SESSION['ctform'][$key] = "<span class='"error'" style='"float: right; color: 00ff00;'">$error</span>";
            }
            $_SESSION['ctform']['error'] = true;
        }
    }
}
$_SESSION['ctform']['success'] = false;
?>

这些元素的name将是:quantity[0]quantity[1]quantity[2],其中包含为每个产品给定的值。请注意,如果您想要quantity[0],则需要将其包含在$my_products数组中。Product 1的值将在quantity[1]中。信息来自这里的代码:

$my_products = array(   
    '0' => 'Product 0', /* Added Product 0 */
    '1' => 'Product 1',
    '2' => 'Product 2'
);
foreach ($my_products as $key => $value) {
    ...
            // $key is 0,1,2 through the loop so the name would be 
            // quantity[0] quantity[1], and quantity[2]
            echo "<select class='"form-control'" name='"quantity[$key]>'"";
                for ($i = 0; $i <= 10; $i++) echo "<option value='"$i'">$i</option>";
            echo "</select>";
    ...
}

所以你只需要像在PHP表单代码中获得其他POST数据一样获得名称:

$products = array(
    @$_POST['quantity[0]'], 
    @$_POST['quantity[1]'], 
    @$_POST['quantity[2]'] );

现在只需将这些值包含在$message:中即可

$message =
    /* Your normal message content here from the code */
    . "<strong>Product0:</strong><br /><em>".$products[0]."</em><br />"
    . "<br />"
    . "<strong>Product1:</strong><br /><em>".$products[1]".</em><br />"
    . "<br />"
    . "<strong>Product2:</strong><br /><em>".$products[2]."</em><br />"
    . "<br />";