Código HTML:
TypeError: data.error_msg is null<anonymous> mails.js:157
jQuery 4
Código:
mails.js<script src="js/mails.js"></script> <form action="#"> <div class="form-group left"> <label class="sr-only" for="name">Nombre</label> <input type="text" class="form-control" id="name" name="name" placeholder="Nombre" required /> </div> <div class="form-group"> <label class="sr-only" for="phone5">Teléfono</label> <input type="tel" pattern="^\d{3}\d{3}\d{3}$" class="form-control" id="phone4" name="phone" placeholder="Teléfono (123456789)" required /> </div> <div class="form-group"> <label class="sr-only" for="msg">Mensaje</label> <textarea class="form-control" id="msg" name="msg" placeholder="Mensaje" rows="4" required ></textarea> </div> <div class="form-group" style=" font-size:13px"> <input type="checkbox" required /> Acepto la <a href="#" target="_blank">política de privacidad.</a> </div> <button class="btn btn-warning" type="submit">Enviar</button> </form>
Código HTML:
$('#wsForm form').on('submit', function(){ $.ajax({ method: "POST", url: "msg.php", data: $(this).serialize(), dataType: 'json' }).done(function( data ) { if(!data.error) { $('#wsForm').html('<div class="form"><div class="text-center">Gracias, pronto le llamaremos!</div></div>') } else { $('<div class="alert alert-danger" role="alert">' + data.error_msg.message + '</div>').prependTo('#wsForm'); } }); return false; });
Código HTML:
<? $return = array(); $to = 'micorreo@micorreo'; $subject = 'the subject'; $headers = 'From: micorreo@micorreo' . "\r\n" . 'Content-type: text/html' . "\r\n" . 'Reply-To: micorreo@micorreo' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); $message = 'New message from the site<br/><br/>'; if($_REQUEST['name']) $message .= 'Name: '.$_REQUEST['name']."<br/>"; if($_REQUEST['phone']) $message .= 'Phone: '.$_REQUEST['phone']."<br/>"; if($_REQUEST['msg']) $message .= 'Message: '.$_REQUEST['msg'].'<br/>'; $success = mail($to, $subject, $message, $headers); if (!$success) { $return['error'] = true; $return['error_msg'] = error_get_last(); } die(json_encode($return));