У меня есть сценарий php веб-почты на одну страницу.
Код скрипта PHP ниже:
<?php
function validEmail($email){
if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) {
return false;
}
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!preg_match("/^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/", $local_array[$i])) {
return false;
}
}
if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) {
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false;
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
return false;
}
}
}
return true;
}
if (isset($_POST["submit"])) {
$ip=$_SERVER['REMOTE_ADDR'];
$name = $_POST['name'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$company = $_POST['company'];
$address = $_POST['address'];
$state = $_POST['state'];
$city = $_POST['city'];
$pincode = $_POST['pincode'];
$package = $_POST['package'];
$logo = $_POST['logo'];
$writeup = $_POST['writeup'];
$heared = $_POST['heared'];
$call = $_POST['call'];
$message = $_POST['message'];
$mailheader = "From: $email \r\n";
$formcontent .= "IP Address: $ip \n\n Full Name: $name \n E-mail: $email \n Mobile: $mobile \n Company: $company \n Address: $address \n state: $state \n City: $city \n Pin Code: $pincode \n Package: $package \n Logo: $logo \n Write-Ups: $writeup \n Heared: $heared \n Call Back: $call \n\n Message: $message";
$recipient = "***@***.com";
$subject = "Package Enquiry Form";
if (!$_POST['name']) {
$errName = 'Please enter your full name';
}
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
if (!$_POST['mobile']) {
$errMobile = 'Please enter your mobile number';
}
if (!$_POST['company']) {
$errCompany = 'Please enter your company name';
}
if (!$_POST['address']) {
$errAddress = 'Please enter your Address';
}
if (!$_POST['state']) {
$errState = 'Please enter your state';
}
if (!$_POST['city']) {
$errCity = 'Please enter your city';
}
if (!$_POST['pincode']) {
$errPinCode = 'Please enter your pin code';
}
if (!$_POST['package']) {
$errPackage = 'Please enter your package';
}
if (!$_POST['logo']) {
$errLogo = 'Do you want logo';
}
if (!$_POST['writeup']) {
$errWriteup = 'Do you need writeup';
}
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($recipient, $subject, $formcontent, $mailheader) or die("Error!")) {
$result='<div class="alert alert-success"><button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>Thank You! for placing your order with us. We will revert back to you shortly.</div>';
} else {
$result='<div class="alert alert-danger"><button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>Warning! There was a problem with your network connection. Please try again.</div>';
}
}
}
?>
и HTML-код формы ниже:
<div class="form-group">
<div class="col-lg-12 col-sm-12">
<strong><?php echo $result; ?></strong>
</div>
</div>
<form id="registerform" method="post" name="registerform" action="">
<div class="form-group">
<div class="col-lg-3 col-sm-6">
<label>Full Name</label>
<input type="text" class="form-control" name="name" placeholder="Full Name *">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-lg-3 col-sm-6">
<label>E-Mail Address</label>
<input type="email" class="form-control" name="email" placeholder="Email *">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-lg-3 col-sm-6">
<label>Contact Number</label>
<input type="number" class="form-control" name="mobile" placeholder="Mobile Number *">
<?php echo "<p class='text-danger'>$errMobile</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-lg-3 col-sm-6">
<label>Company Name</label>
<input type="text" class="form-control" name="company" placeholder="Company Name *">
<?php echo "<p class='text-danger'>$errCompany</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-lg-3 col-sm-6">
<label>Address</label>
<input type="text" class="form-control" name="address" placeholder="Address *">
<?php echo "<p class='text-danger'>$errAddress</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-lg-3 col-sm-6">
<label>City</label>
<input type="text" class="form-control cities" name="city" placeholder="City *">
<?php echo "<p class='text-danger'>$errCity</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-lg-3 col-sm-6">
<label>State</label>
<input type="text" class="form-control" name="state" placeholder="State *">
<?php echo "<p class='text-danger'>$errState</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-lg-3 col-sm-6">
<label>Pin Code</label>
<input type="number" class="form-control" name="pincode" placeholder="Pin Code *">
<?php echo "<p class='text-danger'>$errPinCode</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-lg-3 col-sm-6">
<label>Package</label>
<select class="form-control" name="package" size="1">
<option value="" selected="true" disabled="disabled">Choose Your Package *</option>
<option value="Basic Package">Basic Package</option>
</select>
<?php echo "<p class='text-danger'>$errPackage</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-lg-3 col-sm-6">
<label>Logo</label>
<select class="form-control" name="logo" size="1">
<option value="" selected="true" disabled="disabled">Do you have Logo *</option>
<option value="No">No</option>
<option value="Yes">Yes</option>
<option value="Required">Required</option>
</select>
<?php echo "<p class='text-danger'>$errLogo</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-lg-3 col-sm-6">
<label>Write-up Text</label>
<select class="form-control" name="writeup" size="1">
<option value="" selected="true" disabled="disabled">Do you have Write-ups *</option>
<option value="No">No</option>
<option value="Yes">Yes</option>
<option value="Required">Required</option>
</select>
<?php echo "<p class='text-danger'>$errWriteup</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-lg-3 col-sm-6">
<label>For our records</label>
<select class="form-control" name="heared" size="1">
<option value="" selected="true" disabled="disabled">Where you heared about us?</option>
<option value="Google Search">Google Search</option>
<option value="Others">Others</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-lg-12 ">
<label>Write your Message</label>
<textarea id="message" class="form-control" name="message" rows="5" placeholder="Describe your details enquiry *"></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-lg-6 ">
Request a phone call from a Company representative -
Yes: <input type="radio" value="Yes" name="call">
No: <input type="radio" value="No" name="call">
</div>
</div>
<div class="form-group">
<div class="col-lg-6 text-left">
<input type="submit" id="submit" name="submit" value="Submit" class="btn btn-default btn-lg button">
<input type="reset" class="btn btn-default btn-lg button" value="Clear">
</div>
</div>
</form>
</div>
</div>
Этот код выше на той же странице. Теперь я хочу добавить капчу здесь, и код капчи будет: captcha.php
<?php
session_start();
$code=rand(1000,9999);
$_SESSION["code"]=$code;
$im = imagecreatetruecolor(50, 24);
$bg = imagecolorallocate($im, 22, 86, 165);
$fg = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $bg);
imagestring($im, 5, 5, 5, $code, $fg);
header("Cache-Control: no-cache, must-revalidate");
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
и проверять код будет: validate.php
<?php
session_start();
if(isset($_POST["captcha"])&&$_POST["captcha"]!=""&&$_SESSION["code"]==$_POST["captcha"])
{
echo "Correct Code Entered";
//Do you stuff
}
else
{
die("Wrong Code Entered");
}
?>
и HTML-код CAPTCHA будет:
<form action="validate.php" method="post">
Enter Image Text
<input name="captcha" type="text">
<img src="captcha.php" /><br>
<input name="submit" type="submit" value="Submit">
</form>
Мой вопрос: я интегрирую весь этот код на странице выше, но после проверки страницы она переходит на другую страницу, и я хочу, чтобы она оставалась на той же странице после успешной отправки почты.
Задача ещё не решена.
Других решений пока нет …