• Hi,

    I have a problem, I have a form in mi front-end with 3 fields and a submit button. This submit button has to send me an email.
    I dont want to use more plugins, this is a very simple form.
    I only need send this form without reloading the page, so i need JS and AJAX, but, it doesn′t work…

    I have this in my index.php:

    <form method="post" name="fcontacto" id="fcontacto" action="<?php the_permalink(); ?>" >
    <div class="fcontacto">
                                                                                    <fieldset class="contact-form">
                                                                                        <p class="form-group">
                                                                                            <label id="lblName" for="txtName">
                                                                                                Nombre
                                                                                            </label>
                                                                                            <input type="text" tabindex="1" id="txtName" name="txtName">
                                                                                        </p>
                                                                                        <p class="form-group">
                                                                                            <label id="Email" for="txtEmail">
                                                                                                Email
                                                                                            </label>
                                                                                            <input type="text" tabindex="2" id="txtEmail" name="txtEmail">
                                                                                        </p>
                                                                                        <p class="form-group">
                                                                                            <label id="lblPostcode" for="txtPostcode">
                                                                                                Código Postal
                                                                                            </label>
                                                                                            <input type="text" tabindex="3" id="txtPostcode" name="txtPostcode">
                                                                                        </p>
                                                                                        <input type="submit" class="btn btn-default" tabindex="7" id="submit" value="Enviar" name="submit">
                                                                                        <div id="respuesta" style="display:none;"></div>
    
                                                                                    </fieldset>
                                                                                </div>
    </form>

    This in my custom.js:

    $(document).ready(function(){
    	$("#fcontacto").submit(function( event ){
    		event.preventDefault();
    		$.ajax({
    			type: 'POST',
    			url: '../send.php',
    			data: $(this).serialize(),
    			success: function(data){
    				$("#respuesta").slideDown();
    				$("#respuesta").html(data);
    			}
    		});
    		return false;
    	});
    
    });

    And this in my send.php:

    <?php
    require '../../../class.phpmailer.php';
    $mail = new PHPMailer;
    $to = "[email protected]"; // Nuestro correo de contacto
    // recogeremos los datos del formulario
    $nombre = $_POST['txtName'];
    $email = $_POST['txtEmail'];
    $asunto = $_POST['txtPostcode'];
    if($nombre == "" || $email == "" || $asunto == ""):
    echo '<div class="alert alert-danger">Todos los campos son requeridos para el envio</div>';
    else:
    $mail->From = $email;
    $mail->addAddress($to);
    $mail->Subject = $asunto;
    $mail->isHtml(true);
    $mail->Body = '<strong>'.$nombre.'</strong> le ha contactado desde su web, y le ha enviado el siguiente mensaje: <br><p></p>';
    $mail->CharSet = 'UTF-8';
    $mail->send();
    endif;
    ?>

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Send mail without Plugins and with Javascript’ is closed to new replies.