【发布时间】: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="«Regresar"></a> <a href="index.php"><input type="submit" class="boton2" name="ir_inicio" value="Ir a inicio »" ></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 的教程,你知道我可以使用的好的教程吗?我会很感激的
-
如果有帮助,别忘了将答案标记为完成...