Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Every time i click the submit button the page re-directs.
how can i set it up so that the page does not re-direct and it runs the server side code staying on the current page and echoing above my form?
HTML Code:
<form name="contact_form" method="post" action="mail.php">
<table border="0">
<tbody class="Form">
<tr>
<td>
<label for="NameInput">Your Name:<font color="#FF0000">*</font></label>
<input autofocus="autofocus" placeholder="John Doe" id="NameInput" name="NameInput" required="required" />
</td>
</tr>
<tr>
<td>
<label for="CompanyName">Your Company:<font color="#FF0000">*</font></label>
<input required="required" placeholder="Warner bros." id="CompanyName" name="CompanyName" />
</td>
</tr>
<tr>
<td>
<label id="PhoneNumber" name="PhoneNumber">Phone: </label>
<input placeholder="888-888-8888" id="PhoneNumber" name="PhoneNumber"/>
</td>
</tr>
<tr>
<td>
<label id="Email" name="Email">Email:<font color="#FF0000">*</font></label>
<input required="required" placeholder="johndoe#gmail.com" id="Email" name="Email" type="email" />
</td>
</tr>
<tr>
<td>
<label for="Website">Website: </label>
<input placeholder="https://" id="Website" name="Website" />
</td>
</tr>
</tbody>
</table>
<br/>
<br/>
<br/>
<br/>
<p><strong>What interests you, broadly speaking?</strong></p>
<br/>
<p><input id="WebDesignCheckbox" name="WebDesignCheckbox" type="checkbox">Web Design/Development</input></p>
<input id="SocialMediaCheckbox" name="SocialMediaCheckbox" type="checkbox">Social Media</input>
>
<input id="MobilePresence" name="MobilePresence" type="checkbox">Mobile Presence</input>
<input id="OnlineAdvertising" name="OnlineAdvertising" type="checkbox">Online Advertising</input>
<input id="SEOCheckbox" name="SEOCheckbox" type="checkbox">Search Engine Optamization</input>
<p><input id="ecommerceCheckbox" name="ecommerceCheckbox" type="checkbox" >eCommerce</input></p>
<br/>
<label for="comments">Your Ideas to Life?</label><br/>
<textarea placeholder="How can we help you?" id="comments" name="comments" style="margin: 2px; height: 137px; width: 437px;"></textarea></td></tr><br/>
<tr>
<td><input type="submit" id="Submit" name="Submit" value="Submit" align="middle" /></td>
</tr>
<p id="req-field-desc"><span class="required">*</span> indicates a required field</p>
</form>
PHP Script mail.php:
<?php
if( isset($_POST) ){
//form validation vars
$formok = true;
$errors = array();
//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');
//form data
$NameInput = $_POST['NameInput'];
$CompanyName = $_POST['CompanyName'];
$PhoneNumber = $_POST['PhoneNumber'];
$Email = $_POST['Email'];
$Website = $_POST['Website'];
$WebDesignCheckbox = $_POST['WebDesignCheckbox'];
$SocialMediaCheckbox = $_POST['SocialMediaCheckbox'];
$MobilePresence = $_POST['MobilePresence'];
$OnlineAdvertising = $_POST['OnlineAdvertising'];
$SEOCheckbox = $_POST['SEOCheckbox'];
$ecommerceCheckbox = $_POST['ecommerceCheckbox'];
$comments = $_POST['comments'];
//form validation
if(empty($NameInput)){
$formok - false;
$errors[] = "You have not entered a name.";
}
if(empty($CompanyName)){
$formok = false;
$errors[] = "You have not entered a company name.";
}
if(empty($Email)){
$formok = false;
$errors[] = "You have not entered an email address.";
}
//send email if checks out
if($formok){
ini_set("sendmail_from","keeano#doodleinc.co");
/*$headers="From: Contact Page: {$Email}" . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";*/
$emailbody = "
<p>Time: {$time} \n {$date}</p>
<p>You are recieving this from your websites contact form.</p>
<p><strong>Contacts Name: </strong> {$NameInput}</p>
<p><strong>Companys Name: </strong> {$CompanyName}</p>
<p><strong>Phone Number: </strong> {$PhoneNumber}</p>
<p><strong>Email: </strong> {$Email}</p>
<p><strong>Website: </strong> {$Website}</p>
<p><strong>Web Design Checkbox: </strong> {$WebDesignCheckbox}</p>
<p><strong>Social Media Checkbox: </strong> {$SocialMediaCheckbox}</p>
<p><strong>Mobile Presence: </strong> {$MobilePresence}</p>
<p><strong>Online Advertising: </strong> {$OnlineAdvertising}</p>
<p><strong>SEO Checkbox: </strong> {$SEOCheckbox}</p>
<p><strong>eCommerce Checkbox: </strong> {$ecommerceCheckbox}</p>
<p><strong>Comments: </strong> {$comments}</p>
<p><strong>Extra Data Obtained:</strong>\n Users IPAddress: {$ipaddress}</p>
";
mail("keeano#doodleinc.co", "Contacts Page Enquiry", $emailbody);
echo('<p style="font-weight: bold; color: #709900"><em>Thank you for contacting doodle Inc., someone will contact you within 48 hours.</em></p>');
}
}
I would simply like to get this to create a label at the top of the Form saying "Thank you, someone will be in touch with you within 48 hours."
If someone can help me that would be great as well as detail the explanation so i never run into this problem again.
The thing is you have mail.php as your action for the form tag.
you could just leave it blank, then the form will post to the same page (contact.php)
To get the functionality working on that page you could include your mail.php file before the form, so that the errors are displayed on that same page.
just a require("mail.php"); at the top of the code will do.
You could look at doing a javascript event on the submit button. Then use json and ajax to process your form etc, return a success/fail message, and insert into where the form was. This will not 'redirect' the user to a different page, the form will get replaced by a success message.
<form method="get" action="" onsubmit="jsquickcomp(this); return false;" > // or something like this for your form heading.
Then you js function....
function jsquicksub(po_form) {
if (gb_ajaxbusy)
return false;
gb_ajaxbusy = true;
var lo_request = {};
lo_request.n = $(po_form).find('#sub-firstname').val();
lo_request.e = $(po_form).find('#sub-email').val();
// etc etc
$(po_form).fadeTo(200, 0.3);
$.getJSON('/mail.php', lo_request, function(po_result) { // this is your mail.php which performs all the php side of the form stuff
gb_ajaxbusy = false;
$(po_form).fadeTo(0, 1);
if (!po_result.success) {
alert(po_result.content); // has an alert box for error message. Can change it for anything
return false;
}else{
setTimeout(function() {
$.modal.close();
}, 2500);
}
$(po_form).replaceWith(po_result.content); // replaces the form with the success message
});
}
Then in your mail.php
// function gets passed values after validation and returns a message to the user on screen
function jsonreturn($pb_success, $ps_content = null) {
$lo_result = (object) array('success'=>$pb_success, 'content'=>$ps_content);
ob_clean();
echo json_encode($lo_result);
exit;
} // end jsonreturn()
// use $_GET to get all the values you have send through from your js file
// validate the data and do whatever you want to do with it
// these return the message that will replace the form
jsonreturn(true, 'Success!'); // for success
jsonreturn(false, 'Fail'); // for fail
Hope that makes some sense?
Related
On a membership site that I am developing, once the user has logged in, on his profile page there are two simple forms.
The first is diplayed only if the user has his "district" field NULL in the database. If the "district" field contains any information, the first form is replaced by a table displaying data from that district. The purpose of the form is for the user to select his district from a list, and update his account so he can read local information from his district. After he hits submit, the "district" field in the databsase is updated and then he never sees this form again.
The second one is a basic contact form, thru which the user can easily send an email message to the support team if he needs any help. Once the message has been sent, instead of the form, a success message is displayed until the refresh of the page, when the text field comes back to normal and the user can send another message.
I have tested them and they both work fine separately, but the error I am encountering is that when a user with NULL in his "district" field logs in, or whenever his page is refreshed, the second form automatically sends a blank email to the support team, and is allways showing the success message instead of the text input for the message to be written in. I have setup an error when the second form is submitted empty, but despite this, while the first form is showing, with every refresh, a new blank email is being sent...
After the user has submitted the first form an updated his "district", the second form returns to normal and works just fine... I know that there is a conflict, or I forgot to setup some conditions in the code below. Please be kind, take a look and tell me where do you think the error might be. Any help is welcomed.
<? if($row_user['district'] == NULL ):
if(!isset($_POST['submit2']))
{
foreach($row_user as $field => $value)
{
$_POST[$field] = $value;
}
}
$error2 = 0;
if(isset($_POST['submit2'])){
if(isset($_POST['distr']) && ($_POST['distr']==""))
{
$error2 = 1;
$msg_distr="<br /><span class='error'>Select your district</span>";
}
if($error2 == 0)
{
$update = 'UPDATE users SET ';
if(isset($_POST['distr'])) $update .= 'district = '.GetSQLValueString($_POST['distr'], 'text').', ';
$update = substr_replace($update,"",-2);
$update .= 'WHERE id_user = '.$id_user;
mysql_query($update, $conn) or die(mysql_error());
}
}
?>
The HTML code for the first form is
<form action="" method="post" enctype="multipart/form-data" >
<table width="465px" border="0" cellpadding="5" cellspacing="5">
<tr>
<td width="160" valign="middle" align="left">
<span class="style20"><b>Choose District</b> *</span>
</td>
<td valign="top" align="left">
<select name="distr" onChange="sel_distr(this.value)" class="select" >
<option value="">Choose</option>
<option value="District1" <? if($_POST['distr']=="District1") echo "'selected'"?>>District1</option>
<option value="District2" <? if($_POST['distr']=="District2") echo "'selected'"?>>District2</option>
<option value="District3" <? if($_POST['distr']=="District3") echo "'selected'"?>>District3</option>
<option value="District4" <? if($_POST['distr']=="District4") echo "'selected'"?>>District4</option>
<option value="District5" <? if($_POST['distr']=="District5") echo "'selected'"?>>District5</option>
</select>
<?=$msg_distr?>
</td>
</tr>
<tr>
<td >
<input type="submit" name="submit2" class="submit" value="Insert" />
</td>
</tr>
</table>
</form>
<?endif; ?>
The other form is for sending email messages from this page, just by inserting the message and sending it. the user will not need to insert his name or email.
<?php
$error_message=0;
if ($_POST["email"]<>'') {
if(isset($_POST['comment']) && ($_POST['comment']=="")){
$error_message=1;
$er_msg="<br /><span class='error'>You can't send a blank message</span>";
}
if($error_message == 0){
$ToEmail = 'mail#domain';
$EmailSubject = 'New message from '.$row_user['name'].'';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "<b>Name:</b> ".$_POST["name"]."<br/>";
$MESSAGE_BODY .= "<b>Email:</b> ".$_POST["email"]."<br/><br/>";
$MESSAGE_BODY .= " ".nl2br($_POST["comment"])."<br/>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
<div class="success" style="height:60px;">
<b><?=$row_user['name']?>, Your message was sent!</b> <br/>
A member of our team will contact you soon.
</div>
<?php
}
if($error_message == 1){ ?>
<form action="me.html" method="post">
<input name="name" type="hidden" value="<?=$row_user['name']?>" id="name" size="32">
<input name="email" type="hidden" value="<?=$row_user['email']?>" id="email" size="32"> <br/>
<textarea name="comment" cols="45" rows="6" id="comment" class="input" style="width:90%"></textarea><br/>
<input class="submit" type="submit" name="Submit" value="Send">
</form>
<?=$er_msg?>
<?
}
} else {
?>
<form action="me.html" method="post">
<input name="name" type="hidden" value="<?=$row_user['name']?>" id="name" size="32">
<input name="email" type="hidden" value="<?=$row_user['email']?>" id="email" size="32"> <br/>
<textarea name="comment" cols="45" rows="6" id="comment" class="input" style="width:90%"></textarea><br/>
<input class="submit" type="submit" name="Submit" value="Send">
</form>
<?php
};
?>
Alright, I think this is a minor mistake and you are getting confused because of the too many error counter flags.
Try changing this:
if($error_message == 0){
to:
if($error_message == 0 && $row_user['district'] != NULL){
I'm very new to PHP coding.
I've done tons of research to try and help me. As you can imagine I've gotten tons of material for help. The problem is when I'm trying to put it all together.
Specifically here is my problem. I've come across:
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "e-Mail is Valid";
} else {
echo "Invalid e-Mail";
}
But I have no idea how to implement it. As it stands now the validator checks the fields before the user has time to imput them..... I'm desperate
I'm sure the solution is really simple, but I've spent hours on this and am really desperate for this problem to be solved already.
Here's a link to the page
Here is the code for the page:
<!DOCTYPE html>
<head>
<meta charset='utf-8'>
<title>AWalsh Photography - Contact Me</title>
<link href="style/main_page.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="email_container">
<h1 class="email_head"> Contact Andrew walsh Photography</h1>
<form id="email_form" name="email_form" method="post">
<table>
<tr>
<td><label for="fname">First Name:</label>
</td>
<td><input type="text" name="fname_input" id="fname_input" /><br>
</td>
</tr>
<tr>
<td><label for="lname">Last Name:</label>
</td>
<td><input type="text" name="lname_input" id="lname_input" /><br>
</td>
</tr>
<tr>
<td><label for="email_input">Your Email:</label>
</td>
<td><input type="text" name="email_input" id="email_input" /><br>
</td>
</tr><tr>
<td><label for="email_conf">Re-enter Email:</label>
</td>
<td><input type="text" name="email_conf" id="email_conf" /><br>
</td>
</tr><tr>
<td>
<label for="message_input">Message </label>
</td><td>
<textarea rows="8" cols="45" id="message_input" name="message_input"></textarea>
</td></tr><tr><td></td>
<td>
<input id="submit"type="submit" value="submit" name="submit"/>
</td></tr>
</table>
</form>
<?php
if($_POST['email_imput'] == $_POST['email_conf']){
//stuff to do on success
echo '<h1>Success!!</h1>';
} else {
//stuff to do on failure
echo '<h1>Sorry, The emails you entered do not match</h1>';
}
$email_imput = $_POST['email_imput'];
if (filter_var($email_imput, FILTER_VALIDATE_EMAIL)) {
echo $email_imput . ' is a valid email address.';
} else {
echo $email_imput . ' is not a valid email address.';
}
$message_imput = $_POST['message_imput'];
$msg = "Email address: $email_imput \n" . "Message: $message_imput";
$to = 'myemail#gmail.com ';
$subject = 'AWP_email';
if (filter_var($email_imput)){
mail($to, $subject, $msg, $email);
}
if (mail($to, $subject, $msg, $email)) {
echo("<p>Message successfully sent! Thanks for submitting your message. We will reply to you as soon as possible</p>");
} else {
echo("<h1>Sorry, There was an error in your imput. Please try again.</h1>");
}
?>
<span class="error"><?=$error;?></span>
<form method="post" action="">
<h1> There was an error with your post</h1>
</form>
</div>
</div>
</body>
</html>
Any input would be amazing. Thank you.
You could add a hidden field into the form and check it's value when it's time to send the email.
if (isset($_POST["some_hidden_field"])) {
// put form validation and sending email here
}
else {
// print the form
}
You should first check whether the page has been submitted or not. You might want to try if ($_SERVER['METHOD'] == 'POST') before making any validations
I am trying to make a register form for my website where it asks for a users password once, and when they click submit, a JavaScript box will pop up and ask it from he user again and checks it against the old one and if they are not the same it stays on the join page and tells them passwords don't match. How would I go about doing this?
Here is the HTML document I have:
<h2>New Account Registration.</h2>
<table width="600" cellpadding="1" style="text-align:center;">
<form action="join.php" method="get" >
<tr>
<td colspan="1"><font color="#FF0000"></font></td>
</tr>
<tr>
<td>Company Name:</td>
</tr>
<tr>
<td><input style="background-color: #FFF;" name="companys_name" type="text" placeholder="Google" required /></td>
</tr>
<tr>
<td>First Name:</td>
</tr>
<tr>
<td ><input style="background-color: #FFF;" name="admin_first_name" type="text" placeholder="John" required />
</td>
</tr>
<tr>
<td>Last Name: </td>
</tr>
<tr>
<td>
<input style="background-color: #FFF;" name="admin_last_name" type="text" placeholder="Doe" required />
</td>
</tr>
<tr>
<td>Phone Number:</td>
</tr>
<tr>
<td><input style="background-color: #FFF;" type="tel" name="admin_phone" pattern="[0-9]{3}-?[0-9]{3}-?[0-9]{4}" required placeholder="123-123-1231"></td>
</tr>
<tr>
<td>Website: </td>
</tr>
<tr>
<td><input style="background-color: #FFF;" type="url" id="orderWebsite" placeholder="http://domain.com" /></td>
</tr>
<tr>
<td>Email: </td>
</tr>
<tr>
<td><input style="background-color: #FFF;" type="email" id="orderEmail" placeholder="name#domain.com" required /></td>
</tr>
<tr>
<td> Password: </td>
</tr>
<tr>
<td> <input style="background-color: #FFF;" name="admin_password" type="password" required />
</td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Register!" onclick="register()" /></td>
</tr>
</form>
</table>
Here is the PHP page it connects to:
<?php
$errorMsg = "";
// First we check to see if the form has been submitted
if (isset($_POST['companys_name'])){
//Connect to the database through our include
include_once "connect_to_mysql.php";
// Filter the posted variables
$companys_name = preg_replace("[^A-Za-z0-9]", "", $_POST['companys_name']); // filter everything but numbers and letters
$admin_first_name = preg_replace("[^A-Za-z]", "", $_POST['admin_first_name']); // filter everything but letters
$admin_phone = preg_replace("[^0-9]", "", $_POST['admin_phone']);// Filters everything except numbers
$admin_email = preg_replace("[^A-Z a-z0-9]", "", $_POST['admin_email']); // filter everything but spaces, numbers, and letters
$admin_url = preg_replace("[^A-Z a-z0-9]", "", $_POST['admin_url']); // filter everything but spaces, numbers, and letters
$admin_last_name = preg_replace("[^A-Z a-z]", "", $_POST['admin_last_name']); // filter everything but spaces, numbers, and letters
$admin_email = stripslashes($_POST['admin_email']);
$admin_email = strip_tags($admin_email);
$admin_email = mysql_real_escape_string($admin_email);
$admin_password = preg_replace("[^A-Za-z0-9]", "", $_POST['admin_password']); // filter everything but numbers and letters
// Check to see if the user filled all fields with
// the "Required"(*) symbol next to them in the join form
// and print out to them what they have forgotten to put in
if((!$companys_name) || (!$admin_first_name) || (!$admin_phone) || (!$admin_last_name) || (!$admin_email) || (!$admin_password)){
$errorMsg = "You did not submit the following required information!<br /><br />";
if(!$companys_name){
$errorMsg .= "--- Company Name";
} else if(!$admin_first_name){
$errorMsg .= "--- Phone Number 10 Digits";
} else if(!$admin_phone){
$errorMsg .= "--- Phone Number 10 Digits";
} else if(!$admin_last_name){
$errorMsg .= "--- Last Name";
} else if(!$admin_email){
$errorMsg .= "--- Email Address";
} else if(!$admin_password){
$errorMsg .= "--- Password";
}
} else {
// Database duplicate Fields Check
$sql_companys_name_check = mysql_query("SELECT id FROM toc_companys WHERE companys_name='$companys_name' LIMIT 1");
$sql_admin_email_check = mysql_query("SELECT id FROM toc_companys WHERE admin_email='$admin_email' LIMIT 1");
$companys_name_check = mysql_num_rows($sql_companys_name_check);
$admin_email_check = mysql_num_rows($sql_admin_email_check);
if ($companys_name_check > 0){
$errorMsg = "<u>ERROR:</u><br />Your company name is already in use inside our system. Please try another.";
} else if ($admin_email_check > 0){
$errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside our system. Please try another.";
} else {
// Add MD5 Hash to the password variable
//$hashedPass = md5($password);
// Add user info into the database table, claim your fields then values
$sql = mysql_query("INSERT INTO toc_companys (companys_name, admin_first_name, admin_phone, admin_url, admin_last_name, admin_email, admin_password )
VALUES('$companys_name', '$admin_first_name', '$admin_phone','$admin_url','$admin_last_name', '$admin_email','$admin_password', now())") or die (mysql_error());
// Get the inserted ID here to use in the activation email
$id = mysql_insert_id();
// Create directory(folder) to hold each user files(pics, MP3s, etc.)
mkdir("memberFiles/$id", 0755);
// Start assembly of Email Member the activation link
$to = "$admin_email";
// Change this to your site admin email
$from = "admin#5mutny.com";
$subject = "Activate your account!";
//Begin HTML Email Message where you need to change the activation URL inside
$message = '<html>
<body bgcolor="#FFFFFF">
Hi ' . $admin_first_name . ' at ' . $companys_name . ',
<br /><br />
You must complete this step to activate your account with us.
<br /><br />
Please click here to activate now >>
<a href="http://www.testsite.com/activation.php?id=' . $id . '">
ACTIVATE NOW</a>
<br /><br />
Your Login Data is as follows:
<br /><br />
E-mail Address: ' . $admin_email . ' <br />
Password: ' . $admin_password . '
<br /><br />
Thanks!
</body>
</html>';
// end of message
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$to = "$to";
// Finally send the activation email to the member
mail($to, $subject, $message, $headers);
// Then print a message to the browser for the joiner
print "<br /><br /><br /><h4>OK $firstname, one last step to verify your email identity:</h4><br />
We just sent an Activation link to: $admin_email<br /><br />
<strong><font color=\"#990000\">Please check your email inbox in a moment</font></strong> to click on the Activation <br />
Link inside the message. After email activation you can log in.";
exit(); // Exit so the form and page does not display, just this success message
} // Close else after database duplicate field value checks
} // Close else after missing vars check
} //Close if $_POST
?>
My teacher wants me to make it so when someone clicks register, a JavaScript box will pop up asking the user to retype their password so that it will check vs. the inputbox one and if they are the same it will make their account if not it won't leave the page but it will tell them their passwords are not the same.
Pseudocode via JQuery:
$('#submit').click(function(){
$('#box').show();
});
$('#box > #confirm').click(function(){
var firstInput = $('#firstInput').val();
var secondInput = $('#secondInput').val();
if(!firstInput.equal(secondInput)) {
//do something if they are not the same
}
});
set id for your form, and add a box:
<form id="myform" action="join.php" method="get" >
...
</form>
<div id="box" style="display: none; width: 100px; height: 100px; backgournd-color: #FFFFFF;">
<input id="confirmPassword"/>
<span id="info"></span>
<button id="confirm">confirm</button>
</div
add script in the bottom of your .html
$('#myform').submit(function(){
$('#box').show();
return false; // cancel the submit action
});
$('confirm').click(function(){
if($('input[value="admin_password"]').val().equal($('#confirmPassword').val())) {
$('#myform').submit(); // to submit the form here
} else {
$('#info').text("passwords are not the same!").show().fadeOut(1000);
}
});
I'm working on a very simple, very easy contact form and when i did it on a separate page it worked perfectly, but when i added it to the current website it can't get the $_POST i don't know why. here are the codes
$to ="enter email here";
$name = $_POST["name"];
$email = $_POST["email"];
$header = "From " . $name;
$message = $_POST["message"];
$content = "From: ". $name ."<br /> Email: " . $email ."<br /> Message: " . $message;
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
echo "illegal email";
}
else
{
if (!empty($name) && !empty($message)){
mail($to, $header, $content);
echo"sent <br />";
echo $content;
}else
{
if(empty($email))
{
echo "your email is empty";
}
elseif(empty($name))
{
echo "please enter your name";
}
elseif(empty($message)){
echo "can't send empty messages";
}
}
}
html
<form method="post" action="mail.php">
<table>
<tr>
<td>
Name:
</td>
<td>
<input type="text" name="name" />
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<input type="text" name="email"/>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<input type="text" name="subject"/>
</td>
</tr>
<tr>
<td>
Message: <br /><br/><br/>
</td>
<td>
<textarea style="resize:vertical;" name="message"></textarea>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit"/>
</td>
</tr>
</table>
</form>
thanks in advance and sorry if its a repeat
Check that the PHP is actually executing by adding something like this to the top:
echo "Testing PHP...";
If you do not see that output after submitting a form, check that you are posting the form to the right file. For example, you might need to use:
<form method="post" action="/mail.php">
or
<form method="post" action="/php/mail.php">
...code depending on your website structure.
It's certainly not $_POST that's broken, so it must be something either server related or an error in your code.
Do you have any other PHP on the website your importing the form to? If so you need to make sure that it isn't affecting it in any way.
One more thing to check, it has been reported that a PHP update accidently changed the upload limit size from "8M" to "10MB". Have a scan through your php.ini file and make sure that their isn't any unwanted "MB" instead of "M" in your upload limit.
One final suggestion I can give if you still haven't found the cause after this, is try using:
<?php var_dump($_POST); ?>
which should reveal what's really there.
PHP contact form phone validation of the correct amount of numbers
Hello,
I have this php form that validates the content once submitted a sticky php form is what it is called. It keeps the users data in the input box when an error if found so the user dose not have to re-enter all the data again.
When the phone number is submitted I need it to validate that there are 3 characters/numbers in the first input box then 3 in the next then 4 in the last one.
The way it is now as long as you input numbers in the first input box it over looks the rest of the input boxes for the phone number. So I am looking to add a minimum character/number script in the validation process. I have the form validating that it is a number at this time. I also need it to validate that there is the correct amount of numbers in each input box for the phone as well. I believe this is just changing the elseif statements to just if inside another if but that did not work either. Any help would be very appreciated. The Art Institute only taught so much with PHP, and not this.
This is the particular area of the script that validates the phone number:
//validate the phone number
if(is_numeric($_POST['phone01'])) {
$phone = $_POST['phone01']. '-';
}elseif(is_numeric($_POST['phone02'])) {
$phone .= $_POST['phone02']. '-';
}elseif(is_numeric($_POST['phone03'])) {
$phone .= $_POST['phone03'];
}else{
print '<p class="error">Please enter your Phone Number as 10 Number.</p>';
$validate = FALSE;
}
This is a copy of the whole script for the form itself:
<?php
// This page receives the data from itself and validates as well
//error reporting!
ini_set ('display_errors', 1);
//Shows all possible problem!
error_reporting (E_ALL);
// validate email
function isValidEmail($email){
return eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$', $email);
}
//show form
function show_form($firstName='',$lastName='',$businessName='',$email='',$phone01='',$phone02='',$phone03='',$message=''){
?>
<!--The form starts here -->
<form action ="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contact form" target="_self" id="contact form" dir="ltr" >
<table bgcolor="#000000" width="525" border="0" align="center">
<tr>
<td width="25%" align="right">*First Name:</td>
<td colspan="2" align="left"><input name="firstName" type="text" id="firstName" tabindex="1" size="30" value="<?php if(isset($_POST['firstName'])) { print htmlspecialchars($_POST['firstName']); }?>"/></td>
</tr>
<tr>
<td align="right">*Last Name:</td>
<td colspan="2" align="left"><input name="lastName" type="text" id="lastName" tabindex="2" size="30" value="<?php if(isset($_POST['lastName'])) {print htmlspecialchars($_POST['lastName']); }?>"/></td>
</tr>
<tr>
<td align="right">Business Name:</td>
<td colspan="2" align="left"><input name="businessName" type="text" id="businessName" tabindex="3" size="35" value="<?php if(isset($_POST['businessName'])) {print htmlspecialchars($_POST['businessName']); }?>"/></td>
</tr>
<tr>
<td align="right">*Email: </td>
<td colspan="2" align="left"><input name="email" type="text" id="email" tabindex="4" size="35" value="<?php if(isset($_POST['email'])) {print htmlspecialchars($_POST['email']); }?>"/></td>
</tr>
<tr>
<td align="right">*Phone Number:</td>
<td colspan="2" align="left">
<input name="phone01" type="text" id="phone01" size="3" maxlength="3" tabindex="5"value="<?php if(isset($_POST['phone01'])) {print htmlspecialchars($_POST['phone01']); }?>"/>
- <input name="phone02" type="text" id="phone02" size="3" maxlength="3" tabindex="6"value="<?php if(isset($_POST['phone02'])) {print htmlspecialchars($_POST['phone02']); }?>"/>
- <input name="phone03" type="text" id="phone03" size="4" maxlength="4" tabindex="7" value="<?php if(isset($_POST['phone03'])) {print htmlspecialchars($_POST['phone03']); }?>"/></td>
</tr>
<tr align="center">
<td align="right">*Message:</td>
<td colspan="2" align="left"><textarea name="message" type="text" id="message" tabindex="8" cols="45" rows="4"><?php if(isset($_POST['message'])) {print htmlspecialchars($_POST['message']); }?></textarea>
</td>
</tr>
<tr align="center">
<td> </td>
<td><input name="submit" type="submit" tabindex="9" value="Email" /></td>
<td><input type="reset" name="reset" id="reset" value=" Reset " tabindex="10"/></td>
</tr>
</table>
</form>
<?php
} // end of show_form function
$validate = TRUE;
if($_SERVER['REQUEST_METHOD']!='POST') {
show_form();
} else {
//validate form fields
//validate the first name
if(empty($_POST['firstName'])) {
print '<p class="error">Please enter your First Name.</p>';
$validate = FALSE;
}
//validate the last name
if(empty($_POST['lastName'])) {
print '<p class="error">Please enter your Last Name.</p>';
$validate = FALSE;
}
//validate the enail with email arrary
if(!isValidEmail($_POST['email'])) {
print '<p class="error">Please enter your Email Address in the correct formate.</p>';
$validate = FALSE;
}
//validate the phone number
if(is_numeric($_POST['phone01'])) {
$phone = $_POST['phone01']. '-';
}elseif(is_numeric($_POST['phone02'])) {
$phone .= $_POST['phone02']. '-';
}elseif(is_numeric($_POST['phone03'])) {
$phone .= $_POST['phone03'];
}else{
print '<p class="error">Please enter your Phone Number as 10 Number.</p>';
$validate = FALSE;
}
//validate the message
if(empty($_POST['message'])) {
print '<p class="error">Please enter your Messagee.</p>';
$validate = FALSE;
}
if(!$validate){
print "<p>Please fill in all the fields with an asterisk * next to it and than please try again!</p>";
show_form($_POST['firstName'],$_POST['lastName'],$_POST['businessName'],$_POST['email'],$_POST['phone01'],$_POST['phone02'],$_POST['phone03'],$_POST['message']);
}else{
$phone01 = $_POST['phone01'];
$phone02 = $_POST['phone02'];
$phone03 = $_POST['phone03'];
$phone = $phone01.'-'.$phone02.'-'.$phone03;
//confirmation email to client includes all information provided
mail($_POST['email'], 'Contact Confirmation from www.Ozbar.net Web site', 'Thank You '.$_POST['firstName'].' '.$_POST['lastName'].' for your request for us to contact you.
Below is the information your provided us to contact you per your request.
First Name: '.$_POST['firstName'].'
Last Name: '.$_POST['lastName'].'
Business Name: '.$_POST['businessName'].'
Email Address: '.$_POST['email'].'
Phone Number: '.$_POST['phone01'].'-'.$_POST['phone02'].'-'.$_POST['phone01'].'
Message: '.$_POST['message'].' ','From:contact#steveoatman.me);
//notice of a new contact request
mail('contact#steveoatman.me, 'Contact Request from www.Steveoatman.me Web site', '
First Name: '.$_POST['firstName'].'
Last Word: '.$_POST['lastName'].'
Business Name: '.$_POST['businessName'].'
Email Address: '.$_POST['email'].'
Phone Number: '.$_POST['phone01'].'-'.$_POST['phone02'].'-'.$_POST['phone01'].'
Message: '.$_POST['message'].' ','From:contact#steveoatman.me);
print '<p align="center">Thank You For Your Request!</p>'?><br /><?php
print '<p align="center">We will contact you back with in 24-48 hours.</p>'
?>
<br /><br /> <!-- if all validated a thank you statement -->
<?php
}
} //end of IF submit
// end of all php
?>
<!-- end of #ref form -->
Use strlen to validate the field lengths. Do not use if/elseif as you want to verify all three inputs. Set a flag to keep track of the validity of the phone number.
$invalid_phone = false;
if((strlen($_POST['phone01']) == 3) && is_numeric($_POST['phone01'])) {
$phone = $_POST['phone01']. '-';
}else{
$invalid_phone = true;
}
if((strlen($_POST['phone02']) == 3) && is_numeric($_POST['phone02'])) {
$phone .= $_POST['phone02']. '-';
}else{
$invalid_phone = true;
}
if((strlen($_POST['phone03']) == 4) && is_numeric($_POST['phone03'])) {
$phone .= $_POST['phone03'];
}else{
$invalid_phone = true;
}
if($invalid_phone){
print '<p class="error">Please enter your Phone Number as 10 Number.</p>';
$validate = FALSE;
}
The code above is just checking whether any of the 3 fields have a number in them, rather than all of them.
To achieve what you are going for above, something like this would do it:
if (is_numeric($_POST['phone01']) && is_numeric($_POST['phone02']) && is_numeric($_POST['phone03']))
{
$phone = $_POST['phone01']."-".$_POST['phone02']."-".$_POST['phone03'];
}
else
{
print '<p class="error">Please enter your Phone Number as 10 Number.</p>';
$validate = FALSE;
}
However, the above code does not do any other kind of validation, such as checking to see that the required number of digits have been put in each form field.
You might also want to use the 'ctype_digit()' function to make sure that only digits are entered, rather than a numric string such as 1.3.
So you could do something like
if (!ctype_digit($_POST['phone01']) || strlen($_POST['phone01']) != 4)
{
$validate = FALSE;
}