【问题标题】:Replace a DIV inside a contact form with another DIV in PHP用 PHP 中的另一个 DIV 替换联系表单中的 DIV
【发布时间】:2014-07-02 01:01:26
【问题描述】:

我在 Stackoverflow 中尝试了不同的解决方案,但找不到适用于我拥有的代码的解决方案。

我正在处理一个联系表单,其中包含文本字段和标签的 DIV 应替换为另一个包含感谢信息的 DIV。

到目前为止,我已经收到了感谢信息,但包含联系人数据的 DIV 并没有消失。我为包含字段和感谢消息的 DIV 的名称使用了相同的名称。

所有内容都在同一个名为“pregunta.php”的文件中

如果可能的话,我希望 DIV 消失,而不是完整的表单。

我对 PHP 或 Javascript 代码了解不多。

这是PHP中的部分:

<?php
    if(isset($_POST['send_message'])){
    $errors = array(); // declaramos un array para almacenar los errores
        if($_POST['sender_name'] == ''){
            $errors[1] = '<span class="error">*Ingrese su nombre</span>';
            $mail_sent = false;
        }else if($_POST['sender_email'] == ''){
            $errors[2] = '<span class="error">*Ingrese un email correcto</span>';
            $mail_sent = false;
        }else if($_POST['sender_phone'] == ''){
            $errors[3] = '<span class="error">*Ingrese un teléfono</span>';
            $mail_sent = false;
        }else if($_POST['sender_empresa'] == ''){
            $errors[4] = '<span class="error">*Nombre de la empresa para la que trabaja</span>';
            $mail_sent = false;
        }else if($_POST['sender_message'] == ''){
            $errors[5] = '<span class="error">*Escriba un mensaje</span>';
            $mail_sent = false;
        }
        else{//si todo esta ok envia email

            $name = $_POST['sender_name'];
            $mail_from = $_POST['sender_email'];
            $phone = $_POST['sender_phone'];
            $empresa = $_POST['sender_empresa'];
            $message = $_POST['sender_message'];

            // Encabezados del mensaje
            $headers = 'From: ' . $mail_from . "\r\n";

            // Construct email body
            $body_message = '///////////DATOS PERSONALES'."\r\n";
            $body_message .= "\r\n";
            $body_message .= 'De: ' . $name . "\r\n";
            $body_message .= 'Correo: ' . $mail_from . "\r\n";
            $body_message .= 'Teléfono: ' . $phone . "\r\n";
            $body_message .= 'Empresa: ' . $empresa . "\r\n";
            $body_message .= 'Mensaje: ' . $message . "\r\n";
            $body_message .= "\r\n";


        if(mail("giselle@intex-mkt.com",'Mensaje de página',$body_message,$headers))
        {           
          $result = // This is the DIV with the Thank You Message
         '<div id="datos">
           <center><img src="img/avion.png" alt="Mensaje enviado" style="text-align:center;"/></center>
            <p style="text-align:center; font-size:12px; color:#FFF;">¡Gracias!<br/> Nos comunicaremos contigo en cuanto tengamos los resultados de tu búsqueda</p>

            <div class="botones">
                <a href="cotiza.php"><input type="submit" class="boton1" name="regresa_cotiza" value="&laquo;Regresar"></a> <a href="index.php"><input type="submit" class="boton2" name="ir_inicio" value="Ir a inicio &raquo;" ></a>
            </div><!-- Fin botones -->
          </div><!-- Fin datos-->';

          $_POST['sender_name'] = '';
          $_POST['sender_email'] = '';
          $_POST['sender_phone'] = '';
          $_POST['sender_empresa'] = '';
          $_POST['sender_message'] = '';
        } 
        else 
        { $result = '<div class="result_fail">Hubo un error al enviar el mensaje</div>'; }
        }
    }
        ?> 

这是联系表格:

<form class="contacto" action="pregunta.php" method="POST">
<?php echo $result ?>
                <div id="datos">
                <table width="auto" align="center" >
                <tr>
                <td>
                <form class="contacto" action="pregunta.php" method="POST">
                    <p style="padding-left:0px; font-size:11px;"><i>*Campos obligatorios</i></p>
                     <br/>
                    <div class="campo" style="margin-left:-10px;"><label for="field_name"><p>Nombre* </label><input type="text" id="field_name" class="textfield" name="sender_name" value='<?php echo $_POST['sender_name']; ?>'></p></div><!-- Fin class campo --><?php echo $errors[1]; ?>
                    <br/>    
                    <div class="campo" style="margin-left:-10px;"><label for="field_email"><p>Correo* </label>
                    <input type="text" id="field_email" class="textfield" name="sender_email" value='<?php echo $_POST['sender_email']; ?>'></p></div><!-- Fin class campo --><?php echo $errors[2]; ?>
                    <br/>
                    <div class="campo" style="margin-left:-10px;"><label for="field_phone"><p>Teléfono* </label>
                    <input type="text" id="field_phone" class="textfield" name="sender_phone" value='<?php echo $_POST['sender_phone']; ?>' style="margin-top:-12px;"></p></div><!-- Fin class campo --><?php echo $errors[3]; ?>
                    <br/>    
                    <div class="campo" style="margin-left:-10px;"><label for="field_phone"><p>Empresa* </label>
                    <input type="text" id="field_empresa" class="textfield" name="sender_empresa" value='<?php echo $_POST['sender_empresa']; ?>' style="margin-top:-12px;"></p></div><!-- Fin class campo --><?php echo $errors[4]; ?>
                </td>
                <td>
                    <div class="campo"><label for="field_message"><p style="margin-left:10px;">Mensaje* </label><br /><br />
                    <textarea id="field_message" rows="6" class="field_mensaje" name="sender_message"><?php echo $_POST['sender_message']; ?></textarea></p></div><!-- Fin class campo --><?php echo $errors[5]; ?>   
                </td>
                <tr/>
                </table>
                    <input type="submit" class="boton" name="send_message" value="Enviar">
                </form>

            </div><!-- Fin datos -->

【问题讨论】:

  • 您想要达到的效果最好使用 AJAX 或在完成的表单提交到感谢页面后进行重定向。为什么你不能这样做?
  • 嗯,实际上是我做不到的,客户希望表单以这种方式工作。你认为没有办法让它与这段代码一起工作?
  • 好的,我正在寻找 AJAX 的教程,你知道我可以使用的好的教程吗?我会很感激的
  • 如果有帮助,别忘了将答案标记为完成...

标签: php forms contact


【解决方案1】:

您需要做什么并没有任何意义....但是您可以做的事情是合并 Javascript/jQuery 以通过 AJAX 提交表单,并在成功发送后,将 HTML 替换为您的感谢信息.但同样,纯 PHP 无法做到这一点,因为它是一种服务器端语言。

您也可以在提交表单后尝试设置唯一的 Cookie,并在刷新页面时检查 cookie。如果存在,请在文本字段的位置显示您的感谢信息...不过,这是非常草率且不专业的技术。

更新Check out this article。它完全可以使用 AJAX、PHP 和 jQuery 完成您想做的事情。

【讨论】:

    【解决方案2】:

    您完全过于复杂,并使用了本应通过 AJAX/Javascript 编码的过多和不必要的代码。

    我的 [强烈] 建议是花一两个小时,到 YouTube 上搜索“ajax 教程”,然后从第 1 课开始)...刷新您的 JS 技能,并使用行业最佳实践对其进行编码,意思是 JS/AJAX),而不是 PHP。

    我还建议通过 jQuery 学习 AJAX 作为执行任何 AJAX 调用的方法 - 它更简单。

    相信我...

    【讨论】:

    • 感谢您的推荐。我将开始学习我的 JS 技能。
    猜你喜欢
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多