Super noob to html/php, hopefully you go easy on me I think I learned just enough to be dangerous lol
Trying to cobble together a basic contact form on a small website project, found a html5 template and some basic code to validate the form variables, and use PHPMailer to send. I am trying to call what seems like a function in jquery, from the form elements to verify the variable entries. Using this as my project to wrap my head around working with scripting in html so far.
Here are the html form elements from index.php:
<form method="post" action="#">
<div class="fields">
<div class="field">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
</div>
<div class="field">
<label for="email">Email</label>
<div..............
</div>
<ul class="actions">
<li><input type="submit" onclick="sendEmail()" value="Send Message"/></li>
</ul>
</form>
<!-- Code to verify form input Credit: Senaid Bacinovic-->
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" >
<script type="text/javascript">
function sendEmail() {
var name = $("#name");
var email = $("#email");
var subject = $("#subject");
var body = $("#body");
if (isNotEmpty(name) && isNotEmpty(email) && isNotEmpty(subject) && isNotEmpty(body)) {
$.ajax({
url: 'sendEmail.php',
method: 'POST',
dataType: 'json',
data: {
name: name.val(),
email: email.val(),
subject: subject.val(),
body: body.val()
}, success: function (response) {
if (response.status == "success")
alert('Email Has Been Sent!');
else {
alert('Please Try Again!');
console.log(response);
}
}
});
It seems I have two action items,
<form method="post" action="#">
wants a script name,
and the other line:
<li><input type="submit" onclick="sendEmail()" value="Send Message"/></li>
Does nothing when clicked. The code works as I have it on my sandbox server, but it does not run the entry validation checks on the fields. and doesn’t handle the response, just reloads the index page.
Can anyone point me to examples when calling jquery functions from php/html forms? I am still learning syntax by example if it doesn’t show lol Any help would be greatly appreciated. I am working on much simpler example code, and everything works - or so I think I have my head wrapped around it, then I get lost as soon as I want to add a bit more logic.
Thanks!