Hello.
A couple of weeks ago I concluded a couple of front end development courses and we only touched upon php coding. Since we didn't do much of it I used w3schools.com to learn some basics. I used the tutorial to make a contact form but I have a little problem. Currently the form is set (or rather was until I removed some code) to show anything that was entered in to the form on the site (basically like in this tutorial: https://www.w3schools.com/php/php_forms.asp). What I want the form to do is send the entered information to a predefined email address. But that's not all. It's going to be used on a singlepage website layout. I want the form to also show a prompt box that warns the user that either something went wrong or that the message was sent and reload the page back to the same position (I realise I need to use index#pageposition to do this but I don't know how to implement the link).
Currently this only warns the user of what he did wrong (but not in a prompt box that something wrong happened) and reloads the page to the top. I'm a PHP noob (big noob), and I would appreciate if someone can help me out with this. Thank you :).
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Vpiši ime!";
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Dovoljene so samo črke in presledki!";
}
}
if (empty($_POST["email"])) {
$emailErr = "Vpiši email naslov!";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Vpišite veljaven email naslov!";
}
}
if (empty($_POST["subject"])) {
$subjectErr = "Vpiši naslov sporočila!";
} else {
$subject = test_input($_POST["subject"]);
}
if (empty($_POST["message"])) {
$messageErr = "Vpiši sporočilo!";
} else {
$message = test_input($_POST["message"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="style.css" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet">
<meta name="kontakt">
<head>
</head>
<body>
<div class="col-md-12">
<div class="form-area">
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<br style="clear:both">
<h3>Piši nam</h3>
<div class="form-group">
<input type="text" class="form-control" name="name" placeholder="Ime">
<span class="error">* <?php echo $nameErr;?></span>
</div>
<div class="form-group">
<input type="text" class="form-control" name="email" placeholder="Email">
<span class="error">* <?php echo $emailErr;?></span>
</div>
<div class="form-group">
<input type="text" class="form-control" name="subject" placeholder="Naslov">
<span class="error">* <?php echo $subjectErr;?></span>
</div>
<div class="form-group">
<textarea class="form-control" type="textarea" placeholder="Sporočilo" name=message rows="8"></textarea>
<span class="error">* <?php echo $messageErr;?></span>
</div>
<input type="submit" class="btn btn-primary" name="submit" value="Submit">
<!--<button type="button" name="submit" class="btn btn-primary pull-right" value="Submit">Pošlji</button>-->
</form>
</div>
</div>
</body>