Answer the question
In order to leave comments, you need to log in
Why doesn't phpmailer send email with ajax on button click?
Hello!
I can't figure out where the error is. Help, please, to understand. I'm trying to send an email when a button is clicked, but it gives an error, the email is not sent. There is no error text itself, I managed to write it in js so that the word "Error" appears next to the button. The main thing is, if you do not specify if(isset($_POST['#request']))
, then the letter is sent, but every time you reload the page, but I don’t need this ...
Don’t scold me if I made a mistake somewhere, out of ignorance or inattention :)
Thank you in advance.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title></title>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-6 offset-3">
<div id="app">
<form method="POST" id="request">
<h1>{{ msg }}</h1>
<div class="form-group">
<label for="exampleInputEmail1">Номер магазина: </label>
<select v-model="selectedUser" class="form-control">
<option v-for="site in sites" v-bind:value="site">{{site.name}}</option>
</select>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Адрес магазина: </label>
<span v-if="selectedUser!==null" class="address form-control">{{selectedUser.address}}</span>
</div>
<div class="form-group">
<label for="">Заявитель (инициатор заявки)</label>
<input type="text" class="form-control"><span></span>
</div>
<div class="form-group">
<label for="">Телефон заявителя</label>
<input type="text" class="form-control"><span></span>
</div>
<button type="submit" id="submit" class="btn btn-primary">Submit</button><span></span>
</form>
</div>
</div>
</div>
</div>
<?php
if(isset($_POST['#request'])){
require 'phpmailer/class.phpmailer.php';
require 'phpmailer/class.smtp.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // логин от вашей почты
$mail->Password = 'test'; // пароль от почтового ящика
$mail->SMTPSecure = 'ssl';
$mail->Port = '465';
$mail->CharSet = 'UTF-8';
$mail->setFrom = '[email protected]'; // адрес почты, с которой идет отправка
$mail->FromName = 'Александр'; // имя отправителя
$mail->addAddress('[email protected]', 'Александр');
// $mail->SMTPDebug = 1;
$mail->isHTML(true);
$mail->Subject = 'Тема письма';
$mail->Body = 'Привет, мир! <p>Это строка <b>HTML кода</b></p>';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'ok';
}
}
?>
<script src="https://unpkg.com/vue"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
var app = new Vue({
el: '#app',
data: {
sites:[
{name:'203', address:'test'},
{name:'452', address:'test'},
],
selectedUser: 'Выбери номер магазина',
msg: 'test'
},
});
$(function(){
'use strict';
$('#request').on('submit', function(e){
e.preventDefault();
var fd = new FormData (this);
$.ajax({
url:'index.php',
type: 'POST',
contentType: false,
processData:false,
data:fd,
success: function(msg){
if(msg == 'ok'){
$('#sumbit').next().text('Отправлено!');
}else{
$('#submit').next().text('Ошибка! ');
}
}
})
})
})
</script>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
Add the output print_r($_POST, true) to the PHP code and see that there is no array element named '#request' or 'request'. There even data from <input> will be, will not be, I doubt it, because they have no name=xxx names assigned.
if(isset($_POST['#request'])){
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question