I am using Php for the first time and trying to make a contact us form. I have made the form with inline php inside html code and saved the file as .php and it worked. Now that looked really ugly and my nodejs server doesn't serve my php files so I have tried to take out the php code into a separate file.
So now I have two files -
- index.html which has form
- mail.php
index.html
<h2>PHP FORM </h2>
<form action="mail.php" method="POST">
<label for="user">Name</label>
<input type="text" id="user" name="users-name"><br>
<label for="email">Email</label>
<input type="email" id="email" name="users-email"><br>
<label for="cars">Category</label>
<select name="users-cat" id="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select><br>
<label for="subject">Subject</label>
<input type="text" id="subject" name="users-subject"><br>
<label for="msg">Message</label>
<input type="text" id="msg" name="users-message"><br>
<input type="submit">
</form>
mail.php is like this -
<?php
if (isset($_POST['users-name']) ||
isset($_POST['users-email']) ||
isset($_POST['users-subject'])) {
$admin_email = "username@gmail.com";
$name = stripslashes($_POST['users-name']);
$subject = stripslashes($_POST['users-subject']);
$email = stripslashes($_POST['users-email']);
$category = stripslashes($_POST['users-cat']);
$message = stripslashes($_POST['users-message']);
//send email
$maiL_status = mail($admin_email, "$subject", "Contact Email: " . $email . "\n" . "Name: " . $name . "\n" . "Category: " . $category . "\n" . "Message: " . $message);
//Email response
echo '<h4 class="text-center">THANK YOU.</h4>';
echo '<h6 class="text-center">Your message is now being processed.</h6>';
echo '<h6 class="text-center">We will get back to you promptly.</h6>';
header("Location: index.html?message=ThankYou");
}else{
echo '<h4 class="text-center">Problem</h4>';
}
?>
So my question here is - how can I submit my form with from action using mail.php and redirect back to html page with a message. I am open use Jquery or javascript. I wanna send back the echo response to html page and show message in html page.
I have gone through a lot of stackvoerflow post but I couldn't figure out
Edit:
I have solved this with javascript using -
let msg = window.location.href;
msg = msg.split('=')[1];
console.log(msg);
if(msg!=null){
$('#form_id').hide();
$('#repos').show();
$('#repos').text(msg);
}