【问题标题】:Hiding unuseful vars in PHP在 PHP 中隐藏无用的变量
【发布时间】:2014-04-09 09:38:21
【问题描述】:

在基本的 PHP 电子邮件处理器中,我有一些来自 checkbox 标记的 var,问题是 HTML Doc 中有大约 20 个 checkbox 标记。用户只需选择其中的几个。

我如何隐藏或消除 PHP vars,所以当我的客户通过电子邮件收到此 form 时,HTML 将显示客户选择的 checkbox,而不是仅包含其中几个的空白列表?还是我应该用 HTML 中的 JS 来做?

这是我的代码:

<?php

// ******* PROCESS EMAILS
$emailSubject = 'Solicitud de Servicio ERASE - '.$_POST['nombreNovia'];
$webMaster = '--@live.com';


// INFO. DE LA NOVIA
$nombreNovia = $_POST['nombreNovia'];
$domicilioNovia = $_POST['domicilioNovia'];
$ciudadNovia = $_POST['ciudadNovia'];
$emailNovia = $_POST['emailNovia'];
$celularNovia = $_POST['celularNovia'];
$telefonoNovia = $_POST['telefonoNovia'];
$lugarNacimientoNovia = $_POST['lugarNacimientoNovia'];
$fechaNacimientoNovia = $_POST['fechaNacimientoNovia'];
$religionNovia = $_POST['religionNovia'];
$profesionNovia = $_POST['profesionNovia'];
$anteriorNovia = $_POST['anteriorNovia'];


// INFO. DEL NOVIO
$nombreNovio = $_POST['nombreNovio'];
$domicilioNovio = $_POST['domicilioNovio'];
$ciudadNovio = $_POST['ciudadNovio'];
$emailNovio = $_POST['emailNovio'];
$celularNovio = $_POST['celularNovio'];
$telefonoNovio = $_POST['telefonoNovio'];
$lugarNacimientoNovio = $_POST['lugarNacimientoNovio'];
$fechaNacimientoNovio = $_POST['fechaNacimientoNovio'];
$religionNovio = $_POST['religionNovio'];
$profesionNovio = $_POST['profesionNovio'];
$anteriorNovio = $_POST['anteriorNovio'];


// INFO. DEL EVENTO
$tipoEvento = $_POST['tipoEvento'];
$lugarEvento = $_POST['lugarEvento'];
$fechaEvento = $_POST['fechaEvento'];
$numeroInvitados = $_POST['numeroInvitados'];
$porcentajeForaneos = $_POST['porcentajeForaneos'];

$tipoCeremonia = $_POST['tipoCeremonia'];
$numeroInvitadosCeremonia = $_POST['numeroInvitadosCeremonia'];
$numeroHorasCeremonia = $_POST['numeroHorasCeremonia'];
$desdeCeremonia = $_POST['desdeCeremonia'];
$hastaCeremonia = $_POST['hastaCeremonia'];

$tipoRecepcion = $_POST['tipoRecepcion'];
$numeroInvitadosRecepcion = $_POST['numeroInvitadosRecepcion'];
$numeroHorasRecepcion = $_POST['numeroHorasRecepcion'];
$desdeRecepcion = $_POST['desdeRecepcion'];
$hastaRecepcion = $_POST['hastaRecepcion'];

$diasEvento = $_POST['diasEvento'];
$pedida = $_POST['pedida'];
$despedida = $_POST['despedida'];
$ensayo = $_POST['ensayo'];
$tornaboda = $_POST['tornaboda'];

$hotelesSede = $_POST['hotelesSede'];
$plan = $_POST['plan'];


// DEFINIENDO EL EVENTO
$eventoFormal = $_POST['eventoFormal'];
$eventoSemiformal = $_POST['eventoSemiformal'];
$eventoCasual = $_POST['eventoCasual'];

$atmosferaElegante = $_POST['atmosferaElegante'];
$atmosferaTradicional = $_POST['atmosferaTradicional'];
$atmosferaTematica = $_POST['atmosferaTematica'];
$atmosferaDivertida = $_POST['atmosferaDivertida'];

$banqueteEmplatado = $_POST['banqueteEmplatado'];
$banqueteBuffet = $_POST['banqueteBuffet'];
$banqueteCoktail = $_POST['banqueteCoktail'];
$banqueteTiempos = $_POST['banqueteTiempos'];

$decoracionClasico = $_POST['decoracionClasico'];
$decoracionRomantico = $_POST['decoracionRomantico'];
$decoracionMinimalista = $_POST['decoracionMinimalista'];
$decoracionVintage = $_POST['decoracionVintage'];
$decoracionModerno = $_POST['decoracionModerno'];
$decoracionCaribeño = $_POST['decoracionCaribeño'];
$tematico = $_POST['tematico'];

$color1 = $_POST['color1'];
$color2 = $_POST['color2'];

$prioridades = $_POST['prioridades'];
$noDeseado = $_POST['noDeseado'];


// SERVICIOS A MANEJAR
$servicioBanquete = $_POST['servicioBanquete'];
$ServicioDecoracion = $_POST['ServicioDecoracion'];
$ServicioPirotecnia = $_POST['ServicioPirotecnia'];
$servicioBebidas = $_POST['servicioBebidas'];
$ServicioEdecanes = $_POST['ServicioEdecanes'];
$servicioPostres = $_POST['servicioPostres'];
$servicioImpresos = $_POST['servicioImpresos'];
$servicioPastel = $_POST['servicioPastel'];
$servicioFlores = $_POST['servicioFlores'];
$servicioCotillones = $_POST['servicioCotillones'];
$servicioAnimacion = $_POST['servicioAnimacion'];
$servicioMobiliario = $_POST['servicioMobiliario'];
$servicioFavors = $_POST['servicioFavors'];
$servicioIluminacion = $_POST['servicioIluminacion'];
$servicioDj = $_POST['servicioDj'];
$servicioGrupoMusical = $_POST['servicioGrupoMusical'];
$servicioPlantaLuz = $_POST['servicioPlantaLuz'];
$servicioMusicaAmbiental = $_POST['servicioMusicaAmbiental'];
$servicioMesaDulces = $_POST['servicioMesaDulces'];
$servicioFotoVideo = $_POST['servicioFotoVideo'];
$servicioRentaAutos = $_POST['servicioRentaAutos'];
$servicioValetParking = $_POST['servicioValetParking'];

// INVERSION
$presupuesto = $_POST['presupuesto'];
$otrasAgencias = $_POST['otrasAgencias'];
$notas = $_POST['notas'];


   ob_start();

?>

<body style="font-size: 14px; font-family: sans-serif; margin: 0px; padding: 0px; color: #4D4D4D; background-color: #FAFAFA; -moz-box-sizing:border-box; box-sizing:border-box;">
    <div style="width: 100%; padding-left: 24px; padding-top: 24px; padding-bottom: 24px; background-color: #4D4D4D; -moz-box-sizing:border-box; box-sizing:border-box;">
        <img src="http://www.erase.com.mx/erase/email/img/logoEmail.png" alt="Logo ERASE">
    </div>
    <div style="padding: 0px 24px; -moz-box-sizing:border-box; box-sizing:border-box;">
        <h1 style="text-align: center; font-size: 2.4em; line-height: 5em; font-weight: bold;">Solicitud de Servicio</h1>

        <div style="width: 100%; text-align: left; margin-bottom: 48px; -moz-box-sizing:border-box; box-sizing:border-box;">
            <h2 style="text-align: left; font-size: 1.3em; width: 100%; margin-bottom: 20px;">INFO. DE LA NOVIA</h2>
            <p style="margin-left: 12px;">
                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">NOMBRE:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$nombreNovia</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">DOMICILIO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$domicilioNovia</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">CIUDAD:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$ciudadNovia</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">EMAIL:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$emailNovia</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">CELULAR:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$celularNovia</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">TELEFONO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$telefonoNovia</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">LUGAR DE NACIMIENTO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$lugarNacimientoNovia</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">FECHA DE NACIMIENTO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$fechaNacimientoNovia</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">RELIGION:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$religionNovia</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">PROFESION:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$profesionNovia</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">EVENTO ANTERIOR:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$anteriorNovia</span><br/><br/>
            </p>
        </div>

        <div style="width: 100%; text-align: left; margin-bottom: 48px; -moz-box-sizing:border-box; box-sizing:border-box;">
            <h2 style="text-align: left; font-size: 1.3em; width: 100%; margin-bottom: 20px;">INFO. DEL NOVIO</h2>
            <p style="margin-left: 12px;">
                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">NOMBRE:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$nombreNovio</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">DOMICILIO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$domicilioNovio</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">CIUDAD:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$ciudadNovio</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">EMAIL:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$emailNovio</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">CELULAR:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$celularNovio</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">TELEFONO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$telefonoNovio</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">LUGAR DE NACIMIENTO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$lugarNacimientoNovio</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">FECHA DE NACIMIENTO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$fechaNacimientoNovio</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">RELIGION:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$religionNovio</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">PROFESION:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$profesionNovio</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">EVENTO ANTERIOR:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$anteriorNovio</span><br/><br/>
            </p>
        </div>

        <div style="width: 100%; text-align: left; margin-bottom: 48px; -moz-box-sizing:border-box; box-sizing:border-box;">
            <h2 style="text-align: left; font-size: 1.3em; width: 100%; margin-bottom: 20px;">INFO. DEL EVENTO</h2>
            <p style="margin-left: 12px;">
                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">TIPO DE EVENTO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$tipoEvento</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">LUGAR DEL EVENTO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$lugarEvento</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">FECHA DEL EVENTO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$fechaEvento</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">NUMERO DE INVITADOS:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$numeroInvitados</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">% DE FORANEOS:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$porcentajeForaneos</span><br/><br/>

            <span style="font-size: 1em; color: #888888; line-height: 3em;">CEREMONIA</span><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">TIPO DE CEREMONIA:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$tipoCeremonia</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">NUMERO DE INVITADOS:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$numeroInvitadosCeremonia</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">NUMERO DE HORAS:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$numeroHorasCeremonia</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">DESDE LAS:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$desdeCeremonia</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">HASTA LAS:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$hastaCeremonia</span><br/><br/>

            <span style="font-size: 1em; color: #888888; line-height: 3em;">RECEPCION</span><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">TIPO DE RECEPCION:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$tipoRecepcion</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">NUMERO DE INVITADOS:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$numeroInvitadosRecepcion</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">NUMERO DE HORAS:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$numeroHorasRecepcion</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">DESDE LAS:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$desdeRecepcion</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">HASTA LAS:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$hastaRecepcion</span><br/><br/>

            <span style="font-size: 1em; color: #888888; line-height: 3em;">EVENTO</span><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">DIAS DE EVENTO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$diasEvento</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">PEDIDA:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$pedida</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">DESPEDIDA:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$despedida</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">ENSAYO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$ensayo</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">TORNABODA:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$tornaboda</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">HOTELES SEDE:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$hotelesSede</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">PLAN:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$plan</span><br/><br/>
            </p>
        </div>

        <div style="width: 100%; text-align: left; margin-bottom: 48px; -moz-box-sizing:border-box; box-sizing:border-box;">
            <h2 style="text-align: left; font-size: 1.3em; width: 100%; margin-bottom: 20px;">DEFINIENDO EL EVENTO</h2>
            <p style="margin-left: 12px;">
                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">ESTILO DEL EVENTO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$eventoFormal</span><br/><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$eventoSemiformal</span><br/><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$eventoCasual</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">ATMOSFERA DESEADA:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$atmosferaElegante</span><br/><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$atmosferaTradicional</span><br/><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$atmosferaTematica</span><br/><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$atmosferaDivertida</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">TIPO DE BANQUETE:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$banqueteEmplatado</span><br/><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$banqueteBuffet</span><br/><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$banqueteCoktail</span><br/><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$banqueteTiempos tiempos</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">ESTILO DE DECORACION:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$decoracionClasico</span><br/><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$decoracionRomantico</span><br/><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$decoracionMinimalista</span><br/><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$decoracionVintage</span><br/><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$decoracionModerno</span><br/><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$decoracionCaribeño</span><br/><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$tematico</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">COLORES BASE:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$color1 y $color2</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">PRIORIDADES DEL EVENTO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$prioridades</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">NO DESEADO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$noDeseado</span><br/><br/>
            </p>
        </div>

        <div style="width: 100%; text-align: left; margin-bottom: 48px; -moz-box-sizing:border-box; box-sizing:border-box;">
            <h2 style="text-align: left; font-size: 1.3em; width: 100%; margin-bottom: 20px;">SERVICIOS A MANEJAR</h2>
            <p style="margin-left: 12px;">
                <?php
                    foreach ($_POST as $key => $value) {
                        if (strtolower(substr($key, 0, 8)) == 'servicio' && !empty($value)) {
                            ?>
                                <span class="answer" style="margin-left: 6px; line-height: 1.4em;"><?php echo $value; ?></span><br/><br/>
                            <?php
                        }
                    }
                ?>
            </p>
        </div>

        <div style="width: 100%; text-align: left; margin-bottom: 48px; -moz-box-sizing:border-box; box-sizing:border-box;">
            <h2 style="text-align: left; font-size: 1.3em; width: 100%; margin-bottom: 20px;">PRESUPUESTO</h2>
            <p style="margin-left: 12px;">
                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">PRESUPUESTO:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$presupuesto</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">YA HE COTIZADO CON:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$otrasAgencias</span><br/><br/>

                <span class="label" style="font-size: .8em; color: #888888; line-height: 1.2em;">NOTAS:</span><br/>
                <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$notas</span><br/><br/>
            </p>
        </div>

    </div>
<body>
<?php

    $body = ob_get_clean();

$headers = "From: $emailNovia\r\n";
$headers .= "Content-type:  text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);


// ******* PROCESS EMAILS
//$message = "Gracias por contactarnos. Nos pondremos en contacto con usted cuanto antes<br />Atte:<br />(Lic. Cristina Madrid)";
//$subject = "¡Gracias por escribirnos!";

//$headers2 = "From: $webmaster\r\n";
//$headers2 .= "Content-type:  text/html\r\n";
//mail($leademail, $subject, $message, $headers2);


// ******* REDIRECTION
header('Location: http://www.erase.com.mx/index.html')
?>

【问题讨论】:

  • 如果它们是空的,不包括它们?
  • 你确实需要使用循环
  • 是的@Doon,我怎么不包括他们?这正是我的问题

标签: php forms email variables checkbox


【解决方案1】:

您可以简单地遍历 $_POST 以仅显示不为空的值(我还添加了检查以确保 $_POST必须servicio 开头,只是以防您有其他 POST 值)。

这里我们使用混合输出缓冲区(ob_startob_get_clean - 它们确保我们可以将其全部放入变量中,而不是直接输出),!empty() 确保我们的字符串是'不是空的。

<?php
    // ******* PROCESS EMAILS
    $emailSubject = 'Solicitud de Servicio ERASE - '.$_POST['nombreNovia'];
    $webMaster = '@.com';

    ob_start();

    ?>
        <body style="font-size: 14px; font-family: sans-serif; margin: 0px; padding: 0px; color: #4D4D4D; background-color: #FAFAFA; -moz-box-sizing:border-box; box-sizing:border-box;">
            <div style="width: 100%; padding-left: 24px; padding-top: 24px; padding-bottom: 24px; background-color: #4D4D4D; -moz-box-sizing:border-box; box-sizing:border-box;">
                <img src="http://www.erase.com.mx/erase/email/img/logoEmail.png" alt="Logo ERASE">
            </div>
            <div style="padding: 0px 24px; -moz-box-sizing:border-box; box-sizing:border-box;">
                <h1 style="text-align: center; font-size: 2.4em; line-height: 5em; font-weight: bold;">Solicitud de Servicio</h1>

                <div style="width: 100%; text-align: left; margin-bottom: 48px; -moz-box-sizing:border-box; box-sizing:border-box;">
                    <h2 style="text-align: left; font-size: 1.3em; width: 100%; margin-bottom: 20px;">INFO. DE LA NOVIA</h2>
                    <div style="width: 100%; text-align: left; margin-bottom: 48px; -moz-box-sizing:border-box; box-sizing:border-box;">

                        <h2 style="text-align: left; font-size: 1.3em; width: 100%; margin-bottom: 20px;">SERVICIOS A MANEJAR</h2>
                        <p style="margin-left: 12px;">
                            <?php
                                foreach ($_POST as $key => $value) {
                                    if (strtolower(substr($key, 0, 8)) == 'servicio' && !empty($value)) {
                                        ?>
                                            <span class="answer" style="margin-left: 6px; line-height: 1.4em;"><?php echo $value; ?></span><br/><br/>
                                        <?php
                                    }
                                }
                            ?>
                        </p>
                    </div>
                </div>
            </div>
        </body>
    <?php

    $body = ob_get_clean();

    $headers = "From: $emailNovia\r\n";
    $headers .= "Content-type:  text/html\r\n";
    $success = mail($webMaster, $emailSubject, $body, $headers);


    // ******* PROCESS EMAILS
    //$message = "Gracias por contactarnos. Nos pondremos en contacto con usted cuanto antes<br />Atte:<br />(Lic. Cristina Madrid)";
    //$subject = "¡Gracias por escribirnos!";

    //$headers2 = "From: $webmaster\r\n";
    //$headers2 .= "Content-type:  text/html\r\n";
    //mail($leademail, $subject, $message, $headers2);


    // ******* REDIRECTION
    header('Location: http://www.---.html')
?>

请注意,您尚未在代码中的任何位置定义$emailNovia(用于From: $emailNovia)。另请注意,如果您从用户那里获得此值 ($emailNovia),您需要 以确保其安全,否则有人可能会注入一堆标头并使用您的服务器向所有人发送垃圾邮件他们想要。

【讨论】:

  • 我知道,其实代码要长很多,把重要的代码贴在这里,让它更短
  • 可以了,但是现在剩下的代码已经不行了,我把整个代码贴在原消息里
  • @Kunoxs “代码不再工作”是什么意思。什么不起作用?
  • 这是配方电子邮件的副本: TIPO DE BANQUETE: $banqueteEmplatado $banqueteBuffet $banqueteCoktail $banqueteTiempos tiempos ESTILO DECORACION: $decoracionClasico $decoracionRomantico $decoracionMinimalista $decoracionVintage $decoracionModerno $decoracionCaribeño $te : $color1 y $color2 PRIORIDADES DEL EVENTO: $prioridades NO DESEADO: $noDeseado SERVICIOS MANEJAR Banquete Decoracion DJ Gpo。音乐代客泊车 PRESUPUESTO PRESUPUESTO:$presupuesto YA HE COTIZADO CON:$otrasAgencias NOTAS:$notas
  • @Kunoxs 因为我们在 PHP 标记之外,您需要将 &lt;element&gt;$variable&lt;/element&gt; 更改为 &lt;element&gt;&lt;?php echo $variable; ?&gt;&lt;/element&gt;
【解决方案2】:

首先你需要使用一个数组:

$services = array(
    $_POST['servicioBanquete'],
    $_POST['ServicioDecoracion'],
    $_POST['ServicioPirotecnia'],
    $_POST['servicioBebidas'],
    ...
);

然后动态创建你的表:

<?php foreach($services as $service) : ?>
    <?php if(!empty($service)) : ?>
        <span class="answer" style="margin-left: 6px; line-height: 1.4em;">$servicioBanquete</span><br/><br/>
    <?php endif; ?>
<?php endforeach; ?>

【讨论】:

    【解决方案3】:

    考虑从这里更改您的复选框输入:

    <input type='checkbox' name='servicioBanquete'>
    <input type='checkbox' name='servicioDecoracion'>
    <input type='checkbox' name='servicioPirotecnia'>
    

    ...进入这个:

    <input type='checkbox' name='servicio[Banquete]'>
    <input type='checkbox' name='servicio[Decoracion]'>
    <input type='checkbox' name='servicio[Pirotecnia]'>
    

    这样,当表单发布时,您将获得一个$_POST['servicio'] 数组。此外,它是一个关联数组,仅包含检查值,仅包含检查值,因此不需要empty() 检查。

    为了概念证明,试试这个:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Checkboxes Array</title>
      </head>
      <body>
        <pre><?php print_r($_POST); ?></pre>
        <form method='post'>
          <p><input type='checkbox' name='servicio[Banquete]'>Banquete</p>
          <p><input type='checkbox' name='servicio[Decoracion]'>Decoracion</p>
          <p><input type='checkbox' name='servicio[Pirotecnia]'>Pirotecnia</p>
          <p><input type='submit' value='Go'></p>
        </form>
      </body>
    </html>
    

    【讨论】:

      【解决方案4】:

      您应该创建一个数组并将 $_POST 直接分配给该数组,这样键将被自动创建。然后遍历数组并仅打印非空键的值。

      【讨论】:

      • 为什么将$_POST 分配给另一个数组而不是循环遍历$_POST
      • @geomagas 可能有不需要的 POST 变量。
      • 所以您不应该只是将 $_POST 直接分配给数组,对吧?
      • @geomagas 如果您只将所需的 POST 值添加到另一个数组(我认为他的意思是),例如 $values = array($_POST['foo'], $_POST['bar']);,则在迭代 @ 时不必检查密钥987654324@,因为您只需遍历$values。也就是说,您仍然需要在迭代时检查!empty()。另一方面,我只是保存想要的 keys 然后遍历该数组并检查 $_POST[$key] 是否不为空以节省内存。
      • 同意。但这不仅仅是将 $_POST *直接*分配给数组。这就是我要说的!
      猜你喜欢
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 2012-11-05
      • 1970-01-01
      • 1970-01-01
      • 2012-04-19
      相关资源
      最近更新 更多