if anyone can just go through my code and find possible error, I've tried everything, but I just can't find mistake. My form validates just fine, but when it comes to submit and redirect to next page, it just reloads...
<?php
$your_email ='(i have removed e-mail)';
session_start();
$errors = '';
$name = '';
$visitor_email = '';
$user_message = '';
if(isset($_POST['submit']))
{
$name = $_POST['form-name'];
$visitor_email = $_POST['form-email'];
$subject_email = $_POST['form-subject'];
$user_message = $_POST['form-message'];
$user_id = $_POST['form-id'];
$telephone = $_POST['form-telephone'];
///------------Do Validations-------------
if(empty($name)||empty($visitor_email))
{
$errors .= "\n Morate popuniti polja ime i e-mail. ";
}
if(IsInjected($visitor_email))
{
$errors .= "\n Pogresno unet e-mail!";
}
if(empty($_SESSION['6_letters_code'] ) ||
strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
{
//Note: the captcha code is compared case insensitively.
//if you want case sensitive match, update the check above to
// strcmp()
$errors .= "\n Verifikacioni kod je pogresno unet!";
}
if(empty($errors))
{
//send the email
$to = $your_email;
$subject = "Nova poruka: $subject_email";
$from = $_POST['form-name'];
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$body = "Posetilac $name je poslao poruku sa web-sajta:\n".
"Ime: $name\n".
"Email: $visitor_email \n".
"Poruka: \n ".
"$user_message\n".
"Broj licne karte: $user_id\n".
"Broj telefona: $telephone\n".
"IP: $ip\n";
$headers = "From: $from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to, $subject, $body, $headers);
header('Location: slanje_uspesno.html');
}
}
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
<!DOCTYPE html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script language="JavaScript" src="js/gen_validatorv31.js" type="text/javascript"></script>
</head>
<body>
<div id="header">
<div id="container_header">
<div id="logo"></div>
</div>
</div>
<div id="container_kontakt">
<div id="kontakt_email">
<div id="kontakt_middle">
<div id="forma">
<div class="errors_kontakt">
<?php
if(!empty($errors)){
echo "<p class='err'>".nl2br($errors)."</p>";
}
?>
<div id='form_errorloc' class='err'></div>
</div>
<form id="form" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<ul id="form_list">
<li><label>Vaše ime:</label><input type="text" id="form-name" name="form-name" value='<?php echo htmlentities($name) ?>'/></li>
<li><label>Vaš e-mail:</label><input type="text" id="form-email" name="form-email" value='<?php echo htmlentities($visitor_email) ?>'/></li>
<li><label>Naziv poruke:</label><input type="text" id="form-subject" name="form-subject" /></li>
<li><label>Broj telefona:</label><input type="text" id="form-telephone" name="form-telephone" maxlength="12" /></li>
<li><label>Broj lične karte:</label><input type="text" id="form-id" name="form-id" maxlength="6" /></li>
<li><label>Vaša poruka:</label><textarea name="form-message"><?php echo htmlentities($user_message) ?></textarea></li>
<li><label for="6_letters_code">Verifikacioni broj:</label><img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' > <input id="6_letters_code" class="captcha_code" maxlength="6" name="6_letters_code" type="text" ></li>
<li><label> </label><input type="submit" id="submit" value="POŠALJI" class="submit"></li>
</ul>
</form>
<script language="JavaScript">
// Code for validating the form
// Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
// for details
var frmvalidator = new Validator("form");
//remove the following two lines if you like error message box popups
frmvalidator.EnableOnPageErrorDisplaySingleBox();
frmvalidator.EnableMsgsTogether();
frmvalidator.addValidation("form-name","req","Unesite Vaše ime");
frmvalidator.addValidation("form-email","req","Unesite Vašu e-mail adresu");
frmvalidator.addValidation("form-email","email","Unesite validnu e-mail adresu");
frmvalidator.addValidation("form-id","req","Unesite Vaš broj lične karte");
frmvalidator.addValidation("form-telephone","req","Unesite Vaš broj telefona");
frmvalidator.addValidation("6_letters_code","req","Verifikacioni kod je pogresno unet");
</script>
<script language='JavaScript' type='text/javascript'>
function refreshCaptcha()
{
var img = document.images['captchaimg'];
img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
}
</script>
</div>
</div>
<div id="footer">
</div>
</body>
</html>
You are checking if $_POST['submit'] is ever submitted. Looking at your code, it is never submitted as your submit button doesn't have a name attribute:
<input type="submit" id="submit" value="POŠALJI" class="submit">
It needs to be:
<input type="submit" name="submit" id="submit" value="POŠALJI" class="submit">
There is your problem:
<input type="submit" id="submit" value="POŠALJI" class="submit">
There is no input with the name attribute submit.
Change it to:
<input type="submit" id="submit" name="submit" value="POŠALJI" class="submit">
Related
I'm using bluehost as my web server.
I'm trying to implement reCAPTCHA into my email form for my website,
The mail is sent without passing through the recaptcha.
Below are some of the html codes.
<div class="letterbox">
<form method="post" name="enewsform" id="enewsform" action="enewsletter_ins.php">
<h3 class="tit2">Subscribe To Newsletter</h3>
<div style="margin-top:10px;">
<label class="tit2">Name
<input type="text" name="name" value="" /></label>
<label class="tit2">Email Address
<input type="text" name="email" value="" /></label></br>
<div class="g-recaptcha" data-sitekey="itmysitekey!!"></div>
<input type="button" value="subscribe" class="btn_letter" onclick="sendData(document.enewsform)" />
</div>
</form>
<script type="text/javascript">
<!--
function sendData(f){
if(f.name.value==""){
alert("Required to name.");
f.name.focus();
return false;
}
if(f.email.value==""){
alert("Required to email.");
f.email.focus();
return false;
}
if (f.email.value.search(/(\S+)#(\S+)\.(\S+)/) == -1 ) {
alert("Not Valid Email.");
f.email.focus();
return false;
}
f.submit();
}
//-->
</script>
Below are some of the php codes.
<?
include_once $_SERVER[DOCUMENT_ROOT]."/include/initiate.php";
if(trim($name)=="" or trim($email)==""){
alert("send errer!!");
}
$form_title=$name." 님의 Subscribe to eNewsletter 신청";
$user_name=$name;
$user_email=$email;
$body="
date : ".date("Y-m-d H:i:s")."
<hr>
Name : $name
<hr>
E-Mail : $email
";
$sql = "
insert into prd_form_data set
form_type = '3',
form_title = '".$form_title."',
user_name = '".$user_name."',
user_email = '".$user_email."',
user_content = '".$body."',
insdt = now()
";
mysql_query($sql) or error(mysql_error());
//메일발송
$comments = $body;
$mailTo = ADMIN_MAIL; // 받는 메일 주소;
$mailFrom = "inquiry#example.com"; //
$mailSubject = "Subscribe to eNewsletter";
$mailContent = $comments;
$mailHeader = "From: $mailFrom\r\n";
$mailHeader .= "MIME-Version: 1.0\r\n";
$mailHeader .= "Content-type: text/html; charset=utf-8\r\n";
$mailResult = mail ($mailTo, $mailSubject, $mailContent, $mailHeader,'-f'.$mailFrom);
?>
<form method="post" action="/" name="frm">
</form>
<script type="text/javascript">
<!--
alert("ok!!");
document.frm.submit();
//-->
</script>
function sendData(f){
if(f.name.value==""){
alert("Required to name.");
f.name.focus();
return false;
}
if(f.email.value==""){
alert("Required to email.");
f.email.focus();
return false;
}
if (f.email.value.search(/(\S+)#(\S+)\.(\S+)/) == -1 ) {
alert("Not Valid Email.");
f.email.focus();
return false;
}
if(document.forms["enewsform"]["g-recaptcha-response"].value==""){
alert("Please fill reCAPTCHA");
document.forms["enewsform"]["g-recaptcha-response"].focus();
return false;
}
f.submit();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js"></script>
<div class="letterbox">
<form method="post" name="enewsform" id="enewsform" action="enewsletter_ins.php">
<h3 class="tit2">Subscribe To Newsletter</h3>
<div style="margin-top:10px;">
<label class="tit2">Name
<input type="text" name="name" value="" />
</label>
<br />
<label class="tit2">Email Address
<input type="text" name="email" value="" />
</label>
<br />
<div class="g-recaptcha" data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"></div>
<input type="button" value="subscribe" class="btn_letter" onclick="sendData(document.enewsform)" />
</div>
</form>
</div>
I am trying to send emails to only the users which i am selecting using checkbox from same index.php page. i am trying something here but i don't know that how to transfer and hold checked emails to "Bcc" field. here, is my code please have a look !
Php code for email (index.php) :
<?php
if (isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$to = "";
$headers = "From:$name<$email>";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Bcc: $selectedemailsall\r\n";
$message = "Name: $name\n\n Email: $email \n\n Subject : $subject \n\n Message : $comments";
if(mail($to,$subject,$message,$headers))
{
echo "Email Send";
}
else
{
echo "Error : Please Try Again !";
}
}
?>
Code for form (index.php) :
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mail Document</title>
</head>
<body>
<form action="" method="post" >
<p>Name :<br>
<input type="text" name="name" id=""></p>
<p>Email :<br>
<input type="text" name="email" id=""></p>
<p>Subject :<br>
<input type="text" name="subject" id=""></p>
<p>Comments :<br>
<textarea name="comments" id="" cols="30" rows="10"></textarea></p>
<p><input type="submit" value="Send Email" name="SubmitEmail"></p>
</form>
<form action="#" method="post">
<?php
error_reporting(E_ERROR | E_PARSE);
$connection = mysqli_connect("localhost","root", "");
$db = mysqli_select_db("testdb", $connection);
$query = mysqli_query("select * from users", $connection);
while ($row = mysqli_fetch_array($query))
{
echo "
<input type='checkbox' name='check_list[]' value='{$row['email']}'>
<label>{$row['username']}</label><br/>
";
}
?>
<?php
if(isset($_POST['submituserchk']))
{
//to run PHP script on submit
if(!empty($_POST['check_list']))
{
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selectedemails)
{
$selectedemailsall = $selectedemails.",";
//echo $selectedemailsall;
}
}
}
?>
</div> <!-- End of RightUsersDivWithCheckBox -->
<input type="submit" name="submituserchk" style="margin-left: 87%; margin-top: 20px;" value="Done"/>
</form>
</body>
</html>
Any solution please how to do this ? right now when i click "Done" and submit emails nothing happens and i don't want to click "Done" button after selecting emails. I just select emails and they goes to "Bcc" field in a variable.
Don't use Bcc header for many users. Yo can make is:
Your form:
...
<input type="checkbox" name="email[]" value="foo#host.tld"> - foo#host.tld
<input type="checkbox" name="email[]" value="bar#host.tld"> - bar#host.tld
<input type="checkbox" name="email[]" value="baz#host.tld"> - baz#host.tld
...
Your backend code:
...
if (array_key_exists('email', $_POST) && is_array($_POST['email'])) {
foreach ($_POST['email'] as $to) {
mail($to, $subject, $message, $headers);
}
}
...
All emails sent separatelly for all recipients. This is flexible case for your application -- you can check for each send status.
Finally i got an answer to my own question. below is my code,
Display username, emails with checkbox from database :
$getemail = mysqli_query("SELECT * FROM users",$connection);
if(!$getemail) die('MsSQL Error: ' . mysqli_error());
echo '<div class="AllUserDiv" style="overflow-y:scroll;height:400px;"><table class="table table-bordered">';
echo "<thead>
<tr>
<th><input type='checkbox' onchange='checkedbox(this)' name='chk' /> </th>
<th>Username</th>
<th>Email</th>
</tr>
</thead>";
if(mysqli_num_rows($getemail) == 0)
{
echo "<tbody><tr><td colspan='3'> No Data Available</td></tr></tbody>";
}
while($row = mysqli_fetch_assoc($getemail))
{
echo "<tbody><tr><td><input value='".$row['email']."' type='checkbox' name='check[]' checked /> </td>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['email']."</td></tr></tbody>";
}
Email Form :
<form method="post" action="">
<p style="margin-top:30px;">Email Subject: <input type="text" name="subject" value="" class="form-control" /></p>
<p>Email Content: <textarea name="message" cols="40" rows="6" style="width:100%;"></textarea></p>
<center><input type="submit" name="submit" value="Send Email Now" class="btn btn-primary btn-block" />
</form>
JavaScript for making checkbox selection :
<script type="text/javascript" language="javascript">
function checkedbox(element)
{
var checkboxes = document.getElementById('input');
if(element.checked)
{
for (var i = 0; i < checkboxes.length; i++ )
{
if(checkboxes[i].type == 'checkbox')
{
checkboxes[i].checked = true;
}
}
}
else
{
for (var i = 0; i < checkboxes.length; i++)
{
console.log(i)
if(checkboxes[i].type == 'checkbox')
{
checkboxes[i].checked = false;
}
}
}
}
</script>
i wrote a code for a form in which i added a two validation first one is through email and second one is through email. when user enter's the email address which already exist in a data base it will show an error. the problem which i faced is that when user enter's a new email address and wrong captcha code than it will show an error but at the same time it will save that into the data base also. Here's the complete code is given
<?php
include('../config/connection.php');
//DATABASE INSERT QUERY
if(isset($_POST['submit']))
{
$finame = $_POST['finame'];
$email = $_POST['email'];
$user_message = $_POST['message'];
$b="SELECT * from form WHERE email='".$_POST['email']."'";
$res = mysql_query($b);
$tot = mysql_fetch_assoc($res);
if(empty($tot) || empty($_SESSION['6_letters_code'] ) ||
strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) == 0)
{
$sel = "insert into ".form." set finam='".$_POST['finam']."',lnam='".$_POST['lnam']."',dob='".$_POST['dob']."',cntn='".$_POST['cntn']."',fanam='".$_POST['fanam']."',str='".$_POST['str']."',email='".$_POST['email']."',passw='".$_POST['passw']."'";
mysql_query($sel);
}
//-------------------------------Captcha--------------------
$your_email ='yourname#your-website.com';// <<=== update to your email address
session_start();
$errors = '';
$finame = '';
$email = '';
$user_message = '';
///------------Do Validations-------------
if(!empty($tot))
{
$errors .= "\n Re-enter the captcha code...!!! ";
$msg .="Email adreess already exist";
}
if(IsInjected($email))
{
$errors .= "\n Bad email value!";
}
if(empty($_SESSION['6_letters_code'] ) ||
strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
{
// strcmp()
$errors .= "\n The captcha code does not match!";
}
if(empty($errors))
{
//send the email
$to = $your_email;
$subject="New form submission";
$from = $your_email;
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$body = "A user $name submitted the contact form:\n".
"Name: $finame\n".
"Email: $email \n".
"Message: \n ".
"$user_message\n".
"IP: $ip\n";
$headers = "From: $from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to, $subject, $body,$headers);
header('Location: ../admin/sign-in1.php');
}
}
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />
<script type="text/javascript" src="jsDatePick.min.1.3.js"></script>
<script type="text/javascript">
window.onload = function(){
new JsDatePick({
useMode:2,
target:"inputField",
dateFormat:"%d-%M-%Y"
});
};
</script>
<title>Form</title>
<link href="style/style.css" rel="stylesheet" type="text/css">
<link href='http://fonts.googleapis.com/css?family=Kaushan+Script' rel='stylesheet' type='text/css'>
<script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script>
</head>
<body>
<div style="width:100%; height:170px; margin:auto;">
<div class="abc">
<h1 style="margin:5% 0 0 5%; width:10%; color:#FFF;">Form</h1>
<!--</form>-->
<?php
$sel = "select * from home";
$a=mysql_query($sel);
$fetch = mysql_fetch_array($a);
?>
<div class="sample"> <?php echo $fetch['home4'];?></div>
<div class="main" style="margin:4% 0 0 0;">
Home
About us
Gallery
Contact us
</div>
</div>
</div>
<div style=" width:100%; margin:5% 0 0 0; height:auto;">
<div style="margin:auto; width:80%">
<form method="post" class="w3-container" onSubmit="alert('Thank you. You are registered now input your login id and passwprd to make changes on index and another pages...')" >
<div class="w3-group">
<input class="w3-input blue-l4" pattern="[A-Za-z]{3,}" title="only alphabets" value="<?php echo $_POST['finam'] ?>" type="text" name="finam"required>
<label class="w3-label">First-Name</label>
</div>
<div class="w3-group">
<input class="w3-input blue-l4" pattern="[A-Za-z]{3,}" title="only alphabets" value="<?php echo $_POST['lnam'] ?>" type="text" name="lnam"required>
<label class="w3-label">Last name</label>
</div>
<div class="w3-group">
<input class="w3-input blue-l4" type="text" size="12" readonly id="inputField" value="<?php echo $_POST['dob'] ?>" name="dob" placeholder="DD/MM/YY"required>
</div>
<div class="w3-group">
<input class="w3-input blue-l4" type="text" pattern="[A-Za-z]{3,}" title="only alphabets" value="<?php echo $_POST['fanam'] ?>" name="fanam"required>
<label class="w3-label">Father's name</label>
</div>
<div class="w3-group">
<?php
if(!empty($msg)){
echo "<p class='err'>".nl2br($msg)."</p>";
}
?>
<input class="w3-input blue-l4" value="<?php echo $_POST['email'] ?>" type="email" name="email"required>
<label class="w3-label">Email</label>
</div>
<div class="w3-group">
<input class="w3-input blue-l4" type="password" name="passw" required>
<label class="w3-label">Password</label>
</div>
<div class="w3-group">
<input class="w3-input blue-l4" pattern="[0-9]+" value="<?php echo $_POST['cntn'] ?>" title="only numeric value" type="text" name="cntn"required>
<label class="w3-label">Contact no.</label>
</div>
<div class="clear"></div>
<div class="w3-group">
<select name="str" class="w3-input blue-l4">
<option>--Select Stream--</option>
<?php
$sel = "select * from stream";
$a=mysql_query($sel);
while($fetch = mysql_fetch_array($a))
{
?>
<option><?php echo $fetch['str']?></option>
<?php
}
?>
</select>
</div>
<div class="w3-group">
<?php
if(!empty($errors)){
echo "<p class='err'>".nl2br($errors)."</p>";
}
?>
<img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br>
<label for='message'>Enter the code above here :</label><br>
<input class="w3-input blue-l4" id="6_letters_code" name="6_letters_code" type="text"><br>
<small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small> </div>
<button class="w3-btn blue-d1" name="submit" value="submit">Submit</button>
</form>
</div>
</div>
<script language='JavaScript' type='text/javascript'>
function refreshCaptcha()
{
var img = document.images['captchaimg'];
img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
}
</script>
<div class="foot" style="margin-top:4px;">
<div style="margin:2% 0 2% 86%;">
<img style="margin:0 0 12px 12px;" src="index.jpg" width="30" height="30"/>
<img style="margin:0 0 12px 12px;" src="images1.png" width="30" height="30"/>
<img src="googleplus.png" width="50" height="50"/>
</div>
</div>
</div>
</body>
</html>
Several comments here. First, and maybe the most important, you're using mysql_* functions, which are deprecated and is no longer maintained. You should seriously consider converting to MySQLi or PDO, which has prepared statements (that protects you against SQL-inection). mysql_* is bad practice.
Furthermore, you are mixing variable-names in your code. You're using both $_POST['finam'] and $_POST['finame'], which I assume are one and the same. Be careful with your names!
And you're inserting the password in plain text -- this is also a security issue! You should really hash your password, so that it's never stored in plain text (in case of a hacker accessing your database).
As for your question: You are inserting into the database with a series of or-operators in your if-statement. This means as long as one of them returns TRUE, it'll run the query and insert the email.
I'm not really sure how you do your CAPTCHA-validating, but I think this code will work more as intended.
<?php
session_start();
include('../config/connection.php');
//DATABASE INSERT QUERY
if (isset($_POST['submit'])) {
$finame = $_POST['finam'];
$lname = $_POST['lnam'];
$dob = $_POST['dob'];
$passw = $_POST['passw'];
$email = $_POST['email'];
$fanam = $_POST['fanam'];
$cntn = $_POST['cntn'];
$user_message = $_POST['message'];
$str = $_POST['str'];
$errors = '';
///------------Do Validations-------------
// Checking if the email exists in the database
$res = mysql_query("SELECT * FROM form WHERE email=$email");
// If the number of rows from the result is greater than 0, the email is already in our database
if (mysql_num_rows($res) > 0) {
$errors .= "\n Email exists!";
$emailAvailable = false;
} else {
$emailAvailable = true;
}
if (!$emailAvailable)) {
$errors .= "\n Re-enter the captcha code...!!! ";
$msg .= "Email adreess already exist";
}
if (IsInjected($email)) {
$errors .= "\n Bad email value!";
$badEmail = false;
} else {
$badEmail = true;
}
if (empty($_SESSION['6_letters_code'] ) || strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0) {
// strcmp()
$errors .= "\n The captcha code does not match!";
$captcha = false;
} else {
$captcha = true;
}
///------------If all is well, inserting the email-------------
if ($emailAvailable && $captcha && $badEmail) {
// $sel = "insert into ".form." set finam='".$_POST['finam']."',lnam='".$_POST['lnam']."',dob='".$_POST['dob']."',cntn='".$_POST['cntn']."',fanam='".$_POST['fanam']."',str='".$_POST['str']."',email='".$_POST['email']."',passw='".$_POST['passw']."'";
$sel = "INSERT INTO form (finam, lnam, dob, cntn, fanam, str, email, passw) VALUES ($finame, $lname, $dob, $cntn, $fanam, $str, $email, $passw)";
mysql_query($sel);
}
//-------------------------------Captcha--------------------
$your_email ='yourname#your-website.com';// <<=== update to your email address
if(empty($errors)) {
//send the email
$to = $your_email;
$subject = "New form submission";
$from = $your_email;
$ip = $_SERVER['REMOTE_ADDR'];
$body = "A user $name submitted the contact form:\n".
"Name: $finame\n".
"Email: $email \n".
"Message: \n ".
"$user_message\n".
"IP: $ip\n";
$headers = "From: $from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to, $subject, $body,$headers);
header('Location: ../admin/sign-in1.php');
}
}
// Function to validate against any email injection attempts
function IsInjected($str) {
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if (preg_match($inject,$str)) {
return true;
} else {
return false;
}
}
?>
As you can see, I also updated your INSERT-query, it was kind of messy.
After reading about every single forum post I still cannot figure out how to get my contact form to work correctly. I got the frontend part of it looking good but I am getting a 404 error every time I try to use it. Obviously, because of that none of the information submitted is getting through. Here is my code:
Here is my contact.php which has a
<script src="email/validation.js" type="text/javascript"></script>
at the top between the header
<div class="span12" id="divMain">
<div id="contact">
<h1>Contact Us</h1></div>
<h3 style="color:#FF6633;"><?php echo $_GET[msg];?></h3>
<hr>
<!--Start Contact form -->
<form name="enq" method="post" action="email/index.php" onsubmit="return
validation();">
<fieldset>
<input type="text" name="name" id="name" value="" class="input-block-level" placeholder="Name" />
<input type="text" name="email" id="email" value="" class="input-block-level" placeholder="Email" />
<textarea rows="9" name="message" id="message" class="input-block- levelplaceholder="Let's hear what you've got to say"> </textarea>
<div class="actions">
<input type="submit" value="Send" name="submit" id="submitButton" class="btn btn-success pull -right" title="Click here to submit your message!" />
</div>
</fieldset>
<hr>
</form>
<!--End Contact form -->
</div>
Next here is my validation.js
function validation()
{
var contactname=document.enq.name.value;
var name_exp=/^[A-Za-z\s]+$/;
if(contactname=='')
{
alert("Name Field Should Not Be Empty!");
document.enq.name.focus();
return false;
}
else if(!contactname.match(name_exp))
{
alert("Invalid Name field!");
document.enq.name.focus();
return false;
}
var email=document.enq.email.value;
//var email_exp=/^[A-Za-z0-9\.-_\$]+#[A-Za-z]+\.[a-z]{2,4}$/;
var email_exp=/^\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
if(email=='')
{
alert("Please Enter Email-Id!");
document.enq.email.focus();
return false;
}
else if(!email.match(email_exp))
{
alert("Invalid Email ID !");
document.enq.email.focus();
return false;
}
var message=document.enq.message.value;
if(message=='')
{
alert("Query Field Should Not Be Empty!");
document.enq.message.focus();
return false;
}
return true; }
Followed by my index.php
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$to="marketing#durangoconnections.com";
$subject="Enquiry!";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n";
$message="
Name:
$name
<br>
Email-Id:
$email
<br>
Message:
$query
";
if(mail($to,$subject,$message,$headers))
header("Location:../contact.php?msg=Successful Submission! Thankyou for contacting us.");
else
header("Location:../contact.php?msg=Error To send Email !");
//contact:-your-email#your-domain.com
}
?>
You dont have a contact.php file on your server, you mean contact_us.php
if(mail($to,$subject,$message,$headers))
header("Location:../contact_us.php?msg=Successful Submission! Thankyou for contacting us.");
else
header("Location:../contact_us.php?msg=Error To send Email !");
//contact:-your-email#your-domain.com
}
I am trying to use jQuery to post data to an emailus.php script.
Here is the script part:
<script>
jQuery(document).ready(function(){
jQuery('#submitt').click(function(){
jQuery.post("/emailus.php", jQuery("#mycontactform").serialize(), function(response) {
jQuery('#success').html(response);
});
return false;
});
});
</script>
and here is the HTML used:
<form action="" method="get" id="mycontactform" >
<label for="name">Your Name:</label><br />
<input type="text" name="name" class="cleann" /><br />
<label for="email">Your Email:</label><br />
<input type="text" name="email" class="cleann" /><br />
<label for="message">Your Message:</label><br />
<textarea name="message" class="cleann" rows="7"></textarea><br />
<input type="button" value="send" id="submitt" class="cleannsubmit" /><div id="success" style="color:green;"></div>
</form>
and here is the php script:
<?php
// Here we get all the information from the fields sent over by the form.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'nohanada#gmail.com';
$subject = 'Fortrove Contact';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message.'\n\nItem:'.$itemname;
print_r($_POST);
if($name && $email && $message){
if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
mail($to, $subject, $message);
echo "Your email was sent!";
}
else echo "<span style='color:red'>Invalid email format.</span>";
}
else echo "<span style='color:red'>Please fill all fields</span>";
?>
Problem is that it does not POST the actual fields to the php script. What am i doing wrong?
you have placed the mycontactform inside product_addtocart_form that is the reason which is not allowed so the browser seems to be remocing the mycontactform