how to change php headers on respective website using ajax - php

contactfrm.php
<?php
// Server side validation
$name = $_POST['name'];
$surname = $_POST['surname'];
$bsubject = $_POST['subject'];
$email= $_POST['email'];
$message= $_POST['message'];
$to = "sample#email.com";
$subject = "Mail From sample 1";
$cc = "sample#email.com";
$txt ="";
$txt .="<html><body>";
$txt .= "<table style='border-collapse: collapse; width: 30%; background: #ddd7d769;'><tr>
<th style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>Name</th>";
$txt .= "<td style=' border: 1px solid #0000004f;text-align: left;padding: 8px;'>".$name."</td></tr>";
$txt .= "<tr><th style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>Surname</th>";
$txt .= "<td style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>".$surname."</td></tr>";
$txt .= "<tr><th style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>Subject</th>";
$txt .= "<td style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>".$bsubject."</td></tr>";
$txt .= "<tr style='background: lightgrey;'><th style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>Email</th>";
$txt .= "<td style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>".$email."</td></tr>";
$txt .= "<tr><th style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>Message</th>";
$txt .= "<td style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>".$message."</td></tr>";
$txt .= "</table>";
$txt .="</body></html>";
$headers = "From: Sample 1 . "\r\n" ;
$headers .= 'Cc: '.$cc . "\r\n";
$headers .= "MIME-Version: 1.0'.PHP_EOL'\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if(!empty($email))
{
mail($to,$subject,$txt,$headers);
$success_output = "Your message sent successfully, We will contact you soon. Thank You";
}
else
{
$error_output = "Something went wrong. Please try again later";
}
$output = array(
'error' => $error_output,
'success' => $success_output
);
// Output needs to be in JSON format
echo json_encode($output);
?>
Contact Form.html
<section id="contact" class="contact">
<div class="container" data-aos="fade-up">
<div class="section-title">
<h2>Contact</h2>
<p>Feel Free to contact</p>
</div>
<div class="row">
<div class="col-lg-5 d-flex align-items-stretch">
<div class="info">
<div class="address">
<a class="skype" href="skype:live:.cid.123456789?chat" target = "_blank"><i class="fa fa-skype" aria-hidden="true"></i></a>
<a class="skype" href="skype:live:.cid.123456789?chat" target = "_blank"><h4>Skype</h4></a>
<p>Click here to message us directly on Skype</p>
</div>
<div class="address">
<a class="whatsapp" href="https://wa.me/+123456789" target="_blank"><i class="fa fa-whatsapp" aria-hidden="true"></i></a>
<a class="whatsapp" href="https://wa.me/+123456789" target="_blank"><h4>WhatsApp</h4></a>
<p>Click here to message us directly on WhatsApp </p>
</div>
<div class="email">
<i class="fas fa-envelope"></i>
<h4>Email:</h4>
<p>sample#email.com</p>
</div>
<div class="phone">
<i class="fa fa-phone" aria-hidden="true"></i>
<h4>Call:</h4>
<p>+123456789</p>
</div>
</div>
</div>
<div class="col-lg-7 mt-5 mt-lg-0 d-flex align-items-stretch">
<form id="contactfrm" onsubmit="validateCaptcha()" method = "POST" role="form" class="php-email-form">
<div class="form-row">
<div class="form-group col-md-6">
<label for="name">First Name</label>
<input type="text" name="name" placeholder="Enter Your First Name" class="form-control" id="name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" required />
<div class="validate"></div>
</div>
<div class="form-group col-md-6">
<label for="name">Surname</label>
<input type="text" class="form-control" placeholder="Enter Your Surname" name="surname" id="surname" data-rule="minlen:4" data-msg="Please enter a valid email" />
<div class="validate"></div>
</div>
</div>
<div class="form-group">
<label for="name">Your Email</label>
<input type="email" class="form-control" name="email" placeholder="example#example.com" id="email" data-rule="email" data-msg="Please enter a valid email" />
<div class="validate"></div>
</div>
<div class="form-group">
<label for="name">Subject</label>
<input type="text" class="form-control" name="subject" id="subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
<div class="validate"></div>
</div>
<div class="form-group">
<label for="name">Message</label>
<textarea class="form-control" name="message" rows="10" data-rule="required" data-msg="Please write something for us" required></textarea>
<div class="validate"></div>
</div>
<div id="captcha">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Captcha" data-rule="required" data-msg="Please Enter the Captcha" id="cpatchaTextBox"/>
</div>
<div class="text-center" id="btnSend"><button type="submit">Send Message</button></div>
<div id="alert"></div>
</form>
</div>
</div>
</div>
</section><!-- End Contact Section -->
ajax script
<script async = "true">
var code;
function createCaptcha() {
document.getElementById('captcha').innerHTML = "";
var charsArray =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#";
var lengthOtp = 6;
var captcha = [];
for (var i = 0; i < lengthOtp; i++) {
var index = Math.floor(Math.random() * charsArray.length + 1);
if (captcha.indexOf(charsArray[index]) == -1)
captcha.push(charsArray[index]);
else i--;
}
var canv = document.createElement("canvas");
canv.id = "captcha";
canv.width = 100;
canv.height = 50;
var ctx = canv.getContext("2d");
ctx.font = "25px Georgia";
ctx.strokeText(captcha.join(""), 0, 30);
code = captcha.join("");
document.getElementById("captcha").appendChild(canv);
}
function validateCaptcha() {
event.preventDefault();
debugger
if (document.getElementById("cpatchaTextBox").value == code) {
// alert("Valid Captcha")
readCap();
}else{
alert("Invalid Captcha. try Again");
createCaptcha();
}
}
function readCap(){
$.ajax({
url: 'contactfrm.php',
type: 'post',
data: $('#contactfrm').serialize(),
dataType: 'json',
success: function( _response ){
var error = _response.error;
var success = _response.success;
console.log(success);
if(error != null) {
$('#alert').html(error);
}
else {
$('#alert').html(success);
$('#btnSend').remove();
}
},
error: function(jqXhr, json, errorThrown){
var error = jqXhr.responseText;
$('#alert').html(error);
}
});
}
</script>
I have 30 different websites with this same contact form at the bottom.
As shown in the image, When I receive an email, it says "Mail From (website Name)"
So, if I have to upload same php for 30 different websites, I'll have to make 30 different php forms with different titles and headers.
How can I add if else loop where I can target respective website and only its header will be displayed. Because right now I have created 10 different 'contactfrm.php' in which I have changed headers to respective website.

Replace this variable
$subject = "Mail From sample 1";
Into this code you will get your solution .
$regexp = "/([\w]{0,}).([\w]{2,3}|[\w]{2,3}.[\w]{2,3})$/";
$url= $_SERVER['SERVER_NAME'];
$input = parse_url($url, PHP_URL_HOST);
preg_match($regexp, $input, $matches);
$subject = "Mail From ".$matches[1]; //out put domain name

Related

Unable to POST form data when I add name tag to a particular input tag?

I have been struggling to figure out the mystery behind this.
Unable to POST form data when I add name to a particular input field. I been starring at the screen for hours to figure out what went wrong.
My Code goes as,
Form Page:
<form action="upload.php" method="POST">
<div class="customer_records">
<div class="customer_records_sub">
<input type="text" list="o_id" name="o_id[]" class="mb-2 mr-sm-2 o_id_input" placeholder="Order ID"
style="width: auto;padding: 10px 50px;display: inline-block;font-size: 16px;
border: 1px solid lightgray;border-radius:5px;vertical-align: text-bottom;background: none;">
<datalist id="o_id">
<?php
$sql_o_id = "SELECT DISTINCT(o_id) FROM quotes";
$result_o_id = $conn->query($sql_o_id);
if ($result_o_id->num_rows > 0) {
while($row_o_id = $result_o_id->fetch_assoc()) {
$o_id = $row_o_id["o_id"];
?>
<option value="<?= $o_id; ?>">
<?php } } else { } ?>
</datalist>
<div class="asins_visible_input mb-2 mr-sm-2"
style="width: auto;padding: 10px 50px;display: inline-block;font-size: 16px;
border: 1px solid lightgray;border-radius:5px;vertical-align: text-bottom;
background: none;height: 45px;min-width: 300px;" >
</div>
<br/>
<div class="asins_list" style="display:none;height: auto;overflow: auto;width: auto;
border: 1px solid lightgray;padding: 2%;border-radius: 5px;
margin-left: 24%;min-width: 400px;">
<?php
$sql_asin_id = "SELECT * FROM quotespos";
$result_asin_id = $conn->query($sql_asin_id);
if ($result_asin_id->num_rows > 0) {
while($row_asin_id = $result_asin_id->fetch_assoc()) {
$asin_id = $row_asin_id["id"];
$asin_p_id = $row_asin_id["p_id"];
$asin_o_id = $row_asin_id["o_id"];
if($asin_p_id == '' || $asin_p_id == NULL) {
$asin_p_id = '(BLANK)';
} else {
$asin_p_id = $asin_p_id;
}
?>
<div class="asin_id indi_asin_<?= $asin_o_id; ?>" style="display:none;padding-left:10px;">
<input type="checkbox" class="asins_input_checkbox" data-id="<?= $asin_id; ?>" data-pid="<?= $asin_p_id; ?>" style="display:inline-block;width: 20px;height: 20px;" />
<p style="display:inline-block;padding-top: 2px;font-size: 16px;vertical-align: super;"><?= $asin_p_id; ?></p>
<div class="asins_div_percentile"
style="diplay:inline-block;float:right;display:none;">
<i class="fa fa-chevron-right" aria-hidden="true"
style="margin-top: 9px;margin-left: 8px;font-size: 14px;"></i>
<input class="asins_div_percentile_input" name="tada"
style="width: 50px;margin-left: 30px;vertical-align: top;
border: 1px solid gray;font-size: 16px;
border-radius:5px;border:1px solid gray;text-align:center;"
type="text" placeholder="%" />
</div>
<br/>
</div>
<?php $sl++; } } else { } ?>
<input type="hidden" name="asins[]" class="asins_hidden_input" />
</div>
</div>
</div>
<input type="hidden" name="bywhom" value="0" />
<input type="hidden" name="date" value="06/12/201" />
<button type="submit" name="submit" class="btn btn-primary mb-2 expense_form_btn"
style="margin-top: 0.8%;font-size:16px;">Submit</button>
</form>
Upload.php page:
$date = $_POST["date"];
$bywhom = $_POST["bywhom"];
echo $bywhom;
In the above form page, forget the array and all, they all work perfectly fine the moment i remove the name tag from the input tag with class asins_div_percentile_input, But the moment I keep something inside the name tag of that asins_div_percentile_input input, form gets submitted but none of the values gets posted. not even the normal input tag values like date mentioned above.
I don't understand what went wrong. Hope I was clear.
Any help is greatly appreciated.
After thinking a bit, I found a way around. Instead of forcing it to send all the empty fields and then take only the fields which have values, I removed the empty fields from DOM at the time of submission.
$(".form_submit_btn").click(function() {
$(".form_submit_btn").prop("disabled", true);
$('.main_parent').find('.asins_input_checkbox').each(function () {
if($(this).prop("checked")) { } else {
$(this).parent().remove();
}
});
$("#add_expense_form").submit();
});
Works fine. Thanks to all who literally taught me a lot through this query. Appreciate your time.

I'm not able to login using PHP (pdo) & AJAX

I'm using framework 7, I'm trying to login using PHP (PDO) and Ajax however, every time it comes up with "login failed" even if the correct data was inserted in the email and password inputs, also no error is coming up, I was able to register using PHP (PDO) and everything is going just fine, but not able to login at all, please advise what's wrong with my code
index.php
<div class="pages navbar-through" class="login-screen">
<div data-page="index" class="page no-navbar no-toolbar">
<div class="page-content" style="background:url(img/bg1.png) ;background-
size: 100%">
<div class="content-block-title center">
</div>
<span style="float: right;color: white;margin:20px;font-size: 20px">
<b>عربي</b></span>
<div class="list-block" style="margin:100px 70px auto 70px;">
<img src="img/logo2.png" style="height: 55px ;width: 100%;margin-bottom:
60px;text-align: center;">
<div class="item-input" style="border-bottom: 1px solid gray">
<input type="text" placeholder="Email" id="email" name="email" style="font-
size: 15px;color: white;" required>
</div>
<div class="item-input" style="border-bottom: 1px solid gray;margin-top:
10px">
<input type="password" name="password" placeholder="password" id="password"
style="font-size: 15px;color: white"; required>
</div>
<div style="text-align: center ;margin-top: 30px">
<a href="#" style="color: #00bcd4 ;font-size: 15px">Forgot your password ?
</a>
<span id="login_message"><img src="img/ajax-loader.gif" style="display:
none;"></span>
<center><button style="margin-top: 20px; background-color: #00bcd4;border-
radius: 0px;height: 45px; width: 240px; color: white; text-align: center;"
name="login-btn" class="list-button" id="login-btn" name="login-btn">Login</button></center>
<p style="color: white;font-size: 13px;margin-top: 40px"> You don't have
account? </p>
<a href="registeration.php" style="color: #ffeb3b ;font-size:
13px">REGISTER</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
my-App.js
$(document).ready(function() {
$('#login-btn').click(function () {
var email = $('#email').val();
var password = $('#password').val();
if($.trim(email).length > 0 && $.trim(password).length > 0)
{
$.ajax({
url:"login.php",
method: "POST",
data: {email:email, password:password},
cache: false,
beforeSend:function()
{
$('#login-btn').val("connecting...");
},
success:function(data){
if(data){
myApp.alert('Login successful');
}
else{
myApp.alert('Login failed');
}
}
});
}
});
});
login.php
<?php
require_once 'config.php';
session_start();
if(isset($_POST['login-btn'])){
$email = $_POST['email'];
$password = $_POST['password'];
$select = $db_con->prepare("SELECT * FROM user WHERE email='$email' and
password='$password'");
$select->setFetchMode(PDO::FETCH_ASSOC);
$select->execute();
$data=$select->fetch();
if($data['email']!=$email and $data['password']!=$pass)
{
echo "invalid email or password";
}
elseif($data['email']==$email and $data['pass']==$pass)
{
$_SESSION['email']=$data['email'];
echo "successful";
}
}
?>
Your login.php file has this check before attempting to log in:
if(isset($_POST['login-btn'])){
However, your AJAX script does not send a value called "login-btn", only a value called "email" and a value called "password":
data: {email:email, password:password},
If you change your if statement to check for $_POST['email'] instead, it should work, assuming there are no other problems.
Please also note that your query is vulnerable to SQL injection. Please use bound parameters instead of using your variables directly in your query:
$select = $db_con->prepare("SELECT * FROM user WHERE email=:email and password=:password");
$select->setFetchMode(PDO::FETCH_ASSOC);
$select->execute(array(':email' => $email, ':password' => $password);

Can't send checklist to email from html

I need to do a simple script with two parts. First one is sending just name, phone number etc. and works perfectly already:
But after it i need to send a checklist to the same email, but still is nothing there.
My html:
<form action="contact.php" method="post" id="form" class="contact-form">
<table style="color: DEB441; font-size: 19px; margin-left: 60px; margin-top: 40px; float: left;">
<tr>
<td><font color="FFFFFF">Imie:</td>
<td style="padding:0px">
<textarea style="outline-color: DEB441; margin-left: 23px; margin-bottom: 5px; overflow:auto;width:269px;height:15px;border:1px solid #DEB441;background:#fff;padding:3px 5px;color:#3D1E09;font:14px;font-style:italic;margin:0" name="cf_imie" rows="2" cols="20"></textarea>
</td>
</tr>
<tr>
<td><font color="FFFFFF">Nazwisko:</td>
<td style="padding:0px">
<textarea style="outline-color: DEB441; margin-left: 23px; margin-bottom: 5px; overflow:auto;width:269px;height:15px;border:1px solid #DEB441;background:#fff;padding:3px 5px;color:#3D1E09;font:14px;font-style:italic;margin:0" name="cf_nazwisko" rows="2" cols="20"></textarea>
</td>
</tr>
<tr>
<td><font color="FFFFFF">Adres:</td>
<td style="padding:0px">
<textarea style="outline-color: DEB441; margin-left: 23px; margin-bottom: 5px; overflow:auto;width:269px;height:15px;border:1px solid #DEB441;background:#fff;padding:3px 5px;color:#3D1E09;font:14px;font-style:italic;margin:0" name="cf_adres" rows="2" cols="20"></textarea>
</td>
</tr>
<tr>
<td><font color="FFFFFF">Telefon</td>
<td style="padding:0px">
<textarea style="outline-color: DEB441; margin-left: 23px; margin-bottom: 5px; overflow:auto;width:269px;height:15px;border:1px solid #DEB441;background:#fff;padding:3px 5px;color:#3D1E09;font:14px;font-style:italic;margin:0" name="cf_telefon" rows="2" cols="20"></textarea>
</td>
</tr>
<tr>
<td><font color="FFFFFF">E-mail:</td>
<td style="padding:0px">
<textarea style="outline-color: FFFFFF; margin-left: 23px; margin-bottom: 5px; overflow:auto;width:269px;height:15px;border:1px solid #DEB441;background:#fff;padding:3px 5px;color:#3D1E09;font:14px;font-style:italic;margin:0" name="cf_email" rows="2" cols="20"></textarea>
</td>
</tr>
<tr>
<td style="width: 150px;">
<p style="align: left; margin-top: 2px; margin-right: 10px;"><br>
<font style="font-size: 16px;" color="#FFFFFF" align="left">
Wybierz produkty:<br><br>
<font style="font-size: 12px;" color="#FFFFFF" align="left">
Herbatniki w pudelkach <br>
<hr align="left" width="110px"><bt>
<input type="checkbox" name="thenderese" value="thenderese" />Thenderese<br>
<input type="checkbox" name="petipa" value="petipa" />Petipa<br>
<input type="checkbox" name="biscripts" value="biscripts" />Biscripts<br>
</p>
</td>
<td style="width: 150px;">
<p style="width: 250px; align: left;"><br>
<font style="font-size: 12px;" color="#FFFFFF" align="left">
Herbatniki Pakowane pojedynczo<br>
<hr align="left" width="160px">
<input type="checkbox" name="6g" value="g6g" />6g
<input type="checkbox" name="150g" value="g150g" />150g
<input type="checkbox" name="300g" value="g300g" />300g
</p>
</td>
</tr>
<tr>
<td style="width: 200px; margin-right: 250px;">
<p align="left" style="align: left; margin-top:0px;"><br>
<font style="font-size: 12px;" color="#FFFFFF" align="left">
Inne
<hr align="left" width="150px">
<input type="checkbox" name="folia" value="folia">Biscripts opakowane w folie<br>
<input type="checkbox" name="speculoos1" value="speculoos1" />1,5 kg (proszek speculoos)<br>
<input type="checkbox" name="speculoos2" value="speculoos2" />1,1 kg (pokruszony speculoos)<br>
<input type="checkbox" name="krem" value="krem biscripts" />krem biscripts<br>
</p>
</td>
</tr>
</table>
</form>
and my PHP
<?php
$field_imie = $_POST['cf_imie'];
$field_nazwisko = $_POST['cf_nazwisko'];
$field_adres = $_POST['cf_adres'];
$field_telefon = $_POST['cf_telefon'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'serwis#reskomp.pl';
$subject = 'nowe zamowienie '.$field_imie;
$body_message = 'Od: '.$field_imie."\n";
$body_message .= 'Nazwisko: '.$field_nazwisko."\n";
$body_message .= 'Adres: '.$field_adres."\n";
$body_message .= 'Telefon: '.$field_telefon."\n";
$body_message .= 'Email: '.$field_email."\n";
$body_message .= 'Szczegoly zamowienia: '.$field_message;
$thenderese = $_POST['thenderese'];
if ($thenderese != 'Yes') {
$thenderese = 'No';
}
$petipa = $_POST['petipa'];
if ($petipa != 'Yes') {
$petipa = 'No';
}
$biscripts = $_POST['biscripts'];
if ($biscripts != 'Yes') {
$biscripts = 'No';
}
$g6g = $_POST['6g'];
if ($g6g != 'Yes') {
$g6g = 'No';
}
$g150g = $_POST['150g'];
if ($g150g != 'Yes') {
$g150g = 'No';
}
$g300g = $_POST['300g'];
if ($g300g != 'Yes') {
$g300g = 'No';
}
$folia = $_POST['folia'];
if ($folia != 'Yes') {
$folia = 'No';
}
$speculoos1 = $_POST['speculoos1'];
if ($speculoos1 != 'Yes') {
$speculoos1 = 'No';
}
$speculoos2 = $_POST['speculoos2'];
if ($speculoos2 != 'Yes') {
$speculoos2 = 'No';
}
$krem = $_POST['krem'];
if ($krem != 'Yes') {
$krem = 'No';
}
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Zamowienie zostalo przyjete do realizacji.');
window.location = '../herbatniki/index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Blad wysylania wiadomosci. Prosimy o kontakt na adres: coo.maan#gmail.com');
window.location = 'Contacts.html';
</script>
<?php
}
?>
If somebody could help me...
I know that i should use more css, but this is not the reason.
It looks like you have no way to submit your form.
Further, to add your checkbox data to your email message, you can use something like the following.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
var_dump($_POST);
$message = '';
$message .= sprintf("Name: %s\n", isset($_POST['name']) ? $_POST['name'] : '');
$message .= sprintf("Foo wanted: %s\n", isset($_POST['foo']) ? 'Yes' : 'No');
$message .= sprintf("Bar wanted: %s\n", isset($_POST['bar']) ? 'Yes' : 'No');
var_dump($message);
}
?>
<form method="POST">
Your Name:<input type="text" name="name" /><br />
<input type="checkbox" name="foo" />You want Foo?<br />
<input type="checkbox" name="bar" />You want Bar?<br />
<input type="submit">
</form>
However please research 'email injection in php'.
You need to add the content to $body_message.
Change
$thenderese = $_POST['thenderese'];
if ($thenderese != 'Yes') {
$thenderese = 'No';
}
to
$thenderese = $_POST['thenderese'];
$body_message .= 'thenderese';
if ($thenderese != 'Yes') {
$body_message .= 'No';
}
else {
$body_message .= 'Yes';
}
$body_message .= '/n';
and continue for the rest of your if statements.

css classes are showing before a php form is submitted

I am working on a contact form at the minute which will spit out some errors if certain fields are left blank, and if the form submits successfully the user will see a successful message.
At the moment I have an errors class and a success class - these are below:
.errors {
font-family: 'Open Sans', sans-serif;
font-size: 13px;
padding: 7px 5px;
color: #A94442;
font-weight: 400;
width: 100%;
border-radius: 2px;
border: 1px solid #EBCCD1;
background-color: #F2DEDE;
margin: 0 0 10px 0;
}
.success {
font-family: 'Open Sans', sans-serif;
font-size: 13px;
padding: 7px 5px;
color: #3C763D;
font-weight: 400;
width: 100%;
border-radius: 2px;
border: 1px solid #D6E9C6;
background-color: #DFF0D8;
margin: 0 0 10px 0;
}
As far as the php validation goes, the errors/success messages are working as expected, however the problem I have is that the background-color and borders of these classes are showing blank when the users initially goes to the contact page.
You can get to the page here Contact Page by viewing the page you will see the issue straight away.
Please could you give me some advice/suggestions on how to hide these until they are needed? I could remove the background-color and borders from the classes and just have the text, but ideally I would like to keep the styling as it is.
Here is the php to go with this and also the html form
$errors = array();
$success = "";
$name = "";
$email = "";
$website = "";
$budget = "";
$message = "";
if (isset($_POST['contact_submit'])) {
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
}
if (isset($_POST['website'])) {
$website = $_POST['website'];
}
if (isset($_POST['budget'])) {
$budget = $_POST['budget'];
}
if (isset($_POST['message'])) {
$message = $_POST['message'];
}
if (empty($name)) {
$errors[] = "Please enter your name.";
}
if (empty($email)) {
$errors[] = "Please enter a valid email address.";
}
elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "Please enter a valid email address.";
}
if (empty($message)) {
$errors[] = "Don't forget your message!";
}
if (count($errors) == 0) {
$headers = "From: email here...\r\n";
$headers .= "Content-type: text/html\r\n";
$myaddress = "email here...";
$subject = "Website Enquiry";
$emailmessage = "<b><u>Name:</u></b><br>$name<br><br><b><u>Email:<br></u></b>$email<br><br><b><u>Message:<br></u></b> $message";
if (!mail($myaddress, $subject, $emailmessage, $headers)) {
$errors[] = "A problem occurred sending your email.";
}
else {
$success = "Your message was sent successfully!";
$name = "";
$email = "";
$message = "";
}
}
}
<form method="POST" action="">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php echo "<div class=\"errors\">" . implode("<br>", $errors) . "</div>"; ?>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php echo "<div class=\"success\">" . $success . "</div>"; ?>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<input type="text" name="name" placeholder="Your Name *" value="<?php echo htmlentities($name); ?>">
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<input type="text" name="email" placeholder="Your Email *" value="<?php echo htmlentities($email); ?>">
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<input type="text" name="website" placeholder="Website (optional)" value="<?php echo htmlentities($website); ?>">
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<input type="text" name="budget" placeholder="Budget (optional)" value="<?php echo htmlentities($budget); ?>">
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<textarea type="text" name="message" placeholder="Your Message *"><?php echo htmlentities($message); ?></textarea>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<button type="submit" name="contact_submit" class="btn-one">Send</button>
</div>
</div>
</form>
Thanks in advance!!
You have to do some thing like this
<?php
if(!empty($errors))
{
?>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php echo "<div class=\"errors\">" . implode("<br>", $errors) . "</div>"; ?>
</div>
</div>
<?php
}
else
{
}
if(!empty($success))
{
?>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php echo "<div class=\"success\">" . $success . "</div>"; ?>
</div>
</div>
<?php
}
else
{
}
?>
It would be helpful if you could share your PHP code, but without that I would guess that you're doing something like:
<div class="errors">
<?php
foreach ($errors as $error) {
echo $error . '<br>';
}
?>
</div>
When viewing the HTML for your website, the <div class="errors"></div> HTML is already present.
You should wrap that <div> with an isset() statement, so the HTML is only output when there is something to be shown.
For example:
<?php if (isset($errors) && !empty($errors)): ?>
<div class="errors">
<?php
foreach ($errors as $error) {
echo $error . '<br>';
}
?>
</div>
<?php endif; ?>
Simplest way to do this :
// your php code goes here
<?php
$http_post = ('POST' == $_SERVER['REQUEST_METHOD']);
if($http_post)
{
if(empty($error))
$class = 'error';
else
$class = 'success';
}
?>
// your html code
<div class="<?php echo $class?>"></div>

Php Redirection To Parent Page

I have an Html site.In that there have form for sending mail in Each page.The form action is done in another page that is process.php page.I want to redirect from php page to html page after sending mail.
This is my html code
<form name="mail" id="mail" method="post" action="process.php" onSubmit="return valid_form();">
<div class="form_bg11">
<div class="free_call_back">Free Callback Request</div>
<input name="txtname" id="txtname" type="text" class="free_cal_field" value="Your Name" onClick="(this.value='')" >
<input name="txtphone" id="txtphone" type="text" class="free_cal_field" value="Your Phone Number" onClick="(this.value='')">
<input type="submit" name="Submit" style="background-image: url('images/submit.png'); border: 0px none; height: 19px; margin-left: 85px; width: 56px; margin-top:5px;" value=" ">
</div>
</form>
Thi is my process.php page
<?php
if(isset($_POST['Submit']))
{
$name = $_POST['txtname'];
$phone = $_POST['txtphone'];
$to = "mariyadavis90#gmail.com";
$subject = "New Request Come !";
$message = '<div style="background-color: #EEEEEE;border-bottom: 1px solid #DCDCDC;padding: 10px 0 20px;width: 400px;">
<div style="width:400px;">
<div style="width:400px;background-color:#eeeeee;padding: 10px 0 20px;border-bottom:1px solid #dcdcdc;">
<div style="width:300px; text-align:center;color:#666666;margin-left:20px;font-size:12px;font-weight:bold;">
Now you got a new request from :
</div>
</div> <!--end of div_form_main-->
<div style="width:400px;background-color:#eeeeee;padding: 10px 0 20px;border-bottom:1px solid #dcdcdc;">
<div style="float:left;width:150px;color:#666666;margin-left:20px;font-size:12px;font-weight:bold;"> Name:</div>
<div style="float:left;width:auto;color:#000000;font-size:12px;font-weight:bold;">'.$name.'</div>
</div> <!--end of div_form_main-->
<!--end of div_form_main-->
<div style="width:400px;background-color:#eeeeee;padding: 10px 0 20px;border-bottom:1px solid #dcdcdc;">
<div style="float:left;width:150px;color:#666666;margin-left:20px;font-size:12px;font-weight:bold;"> Phone Number:</div>
<div style="float:left;width:auto;color:#000000;font-size:12px;font-weight:bold;">'.$phone.'</div></div> <!--end of div_form_main-->
</div> <!--end of div_password_main-->';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .="Content-Transfer-Encoding: 8bit";
if(!mail($to, $subject, $message, $headers))
{
$msg = "Sending mail is failed !";
}
else
{
$msg = "Mail send Successfully !";
}
print "<script type=\"text/javascript\">";
print "alert('".$msg."')";
print "</script>";
}
?>
how can i do this?
I am a bit confused by your wording. Why can't you just use the PHP header() function?
header('Location: http://www.example.com/');
you can modify your procss.php (in your javascript block)
print "<script type=\"text/javascript\">";
print "alert('".$msg."')";
print "window.location.href='http://www.yourdomain/yourhtmlpage.html'";
print "</script>";
A quick write up
if(!mail($to, $subject, $message, $headers))
{
$msg = "Sending mail is failed !";
}
else
{
$msg = "Mail send Successfully !";
print "<script type=\"text/javascript\">";
print "window.location.href= 'www.example.com' ";
print "</script>";
}
print "<script type=\"text/javascript\">";
print "alert('".$msg."')";
print "</script>";

Categories