Concatenation email recipient address - php

This is a form that I get the user to enter data including their mobile number and mobile carrier. I am using concatenation of the mobile number and carrier to send a message to their phone. This works but I am having a problem with the concatenation for the email recipient. If I use a straight email address ("myemail#gmail.com"), it will deliver the content. It will not work using the concatenation of $phone and $carrier ($YourEmailAddress). I have tried several different methods but nothing is working. I have tried using "&" and "+". I need assistance in figuring out why the concatenation of the phone and carrier strings is not working. I am new to this site so I am not sure if I correctly posted this.
This is my php file:
<?php
if( count($_POST) )
{
$YourEmailSubject = "Form Submission From the Blog";
$name = stripslashes($_POST['name']);
$email = stripslashes($_POST['email']);
$comment = stripslashes($_POST['comment']);
$phone = ($_POST['phone']);
$selectOption = $_POST['carrier'];
$content = "$name\r\n$email\r\n$comment\r\n$selectOption\r\n";
$YourEmailAddress = $phone."#".$selectOption;
mail($YourEmailAddress,$YourEmailSubject,$content,"From: ABC Company");
header("Location:" . (isset($_POST['redirect']) ? $_POST['redirect'] : '/') );
exit;
}
?>
This is my form:
<form method="post" action="/simplecontact.php">
<input type="hidden" name="redirect" value="//www.google.com">
<p>
Name:<br>
<input type="text" name="name" style="width:200px;"></td>
</p>
<p>
Email:<br>
<input type="text" name="email" style="width:200px;"></td>
</p>
<p>
Comment:<br>
<textarea name="comment" style="width:200px; height:100px"></textarea></td>
</p>
<p>Phone Number:<br>
<input type="text" id="number" name="number" /></td>
</p>
Carrier: <br>
<select id="carrier" name="carrier">
<option value="tmomail.net">T-mobile</option>
<option value="vmobl.com">Virgin Mobile</option>
<option value="cingularme.com">Cingular</option>
<option value="messaging.sprintpcs.com">Sprint</option>
<option value="txt.att.net">AT&T</option>
<option value="vtext.com">Verizon</option>
<option value="messaging.nextel.com">Nextel</option>
<option value="email.uscc.net">US Cellular</option>
<option value="sms.mycricket.com">Cricket</option>
<option value="mymetropcs.com">Metro PCS</option>
<option value="myboostmobile.com">Boost Mobile</option>
</select>
<p>
<input type="submit" style="width:200px;" value="Submit Form"></td>
</p>
</form>

In your form the phone input name is "number", in the php code you are trying to get the phone number like the input name was "phone"
Just change the input name to "phone"
<input type="text" id="number" name="phone" /></td>

Related

PHP form doesn't validate, only gives blank page on submit

I decided to do a little test by testing to see if the form would detect an empty input field, and it didn't work, I don't know what the problem is and I don't want to write the rest of the project if this one small thing doesn't work so here's the code, I've looked over it and I don't think I've missed anything.
Here's the HTML:
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<form method="POST" action="myform.php">
<fieldset>
<legend>Personal Info</legend>
First name <input name="name" type="text">
Middle name <input name="middlename" type="text">
Surname <input name="lastname" type="text">
Age <input name="age" type="number">
Date of birth <input name="dob" type="date">
</fieldset>
<fieldset>
<legend>Regional & location info</legend>
Continent
<select>
<option value="europe">Europe</option>
<option value="americas">America</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option>
<option value="australia">Australia</option>
<option value="eurasia">Eurasia</option>
</select>
Country <input type="text"> State <input type="text">
City <input type="text">
Street number <input type="number">
Street name <input type="text"> <br><br>
Suburb <input type="text"> Postcode <input type="number">
If none of these apply to your accommodations, enter a typed location here <input type="text">
</fieldset>
<fieldset>
<legend>Previous lifestyle accommodations</legend>
Previous &/or most recent job title <input name="job" type="text">
First time job seeker <input type="checkbox" name="check1" value="ftjb">
I'm a student <input type="checkbox" name="check2" value="ias">
Previous &/or most recent acedemic title <input name="school" type="text">
First time applying for a qualification <input type="checkbox" name="check3" value="ftafaq">
I have work experience <input type="checkbox" name="check4" value="ihwe">
</fieldset>
<fieldset>
<legend>Details of arrival</legend>
Reason for arrival of all parties <input name="reason" type="text">
Date of arrival <input name="arrival" type="date">
Amount of stay expectancy
<input type="checkbox" name="check3">Temporary
<input type="checkbox" name="check4">Longterm
</fieldset>
<fieldset>
<legend>Signiture</legend>
<input type="text">
</fieldset>
<input type="submit" value="Submit"><button type="reset">Reset</button>
</form>
</body>
</html>
And here's what I've done in PHP so far for the form:
<?php
$nameInvalid = "";
$middleInvalid = "";
$surnameInvalid = "";
$ageInvalid = "";
$dobInvalid = "";
$countryInvalid = "";
$cityInvalid = "";
$strtInvalid = "";
$strnameInvalid = "";
$suburbInvalid = "";
$postcodeInvalid = "";
$jobInvalid = "";
$ftjsInvalid = "";
$iasInvalid = "";
$schoolInvalid = "";
$check1Invalid = "";
$check2Invalid = "";
$checl3Invalid = "";
$check4Invalid = "";
$reasonInvalid = "";
$arrivalInvalid = "";
if (isset($_POST['submit'])) {
if (empty($_POST["name"])) {
$nameInvalid = "Name is required";
}
}
?>
Please add name="submit" attribute into the button.
<input type="submit" value="Submit" name="submit">
And also please add echo into the validation to print validation message like below.
if (isset($_POST['submit'])) {
if (empty($_POST["name"])) {
echo $nameInvalid = "Name is required";
}
}
Use this
<input type="submit" name="submit" value="Submit">
HTML
You need to use HTML5 validation to prevent the form from being sent if it doesn't conform to your rules.
From the official documentation:
The simplest HTML5 validation feature to use is the required attribute — if you want to make an input mandatory, you can mark the element using this attribute. When this attribute is set, the form won't submit (and will display an error message) when the input is empty (the input will also be considered invalid).
In your case, try adding the required tag to the fields you want to test, for instance the name field:
First name <input name="name" type="text" required>
To make it a little bit more fancy, add some CSS:
input:invalid {
border: 2px dashed red;
}
input:valid {
border: 2px solid black;
}
PHP
To validate PHP responses, all you need to do is the following:
if (!$_REQUEST["name"]) {
//If the name field is empty
die("Name is missing");
//Replace this with whatever logic fits your code
}
We're using $_REQUEST, which means both $_POST and $_GET, but you can stick to $_POST if you'd like. We're using if(!$val) to validate, it's the easiest way, but has caveats (see below), you can also use if(empty($val)) but for strings, the first is fine.
Get rid of the list of variables you define as empty in the beginning of your script. Instead look at setting an array if a value that shouldn't be empty, is empty. For instance:
# Repeat this structure for each form field you want
if (!$_REQUEST["name"]) {
//If the name field is empty
$empty_fields[] = "name";
//Fill the $empty_fields array with all the fields that are missing
}
# At the end, cycle thru the missing fields and tell the user
if(!empty($empty_fields)){
die("The following fields are missing: ".implode(", ", $empty_fields));
}
Caveat
Using a if(!$val) is a shortcut, and won't work if you allow values like " " (space) or "0" (zero), but works fine if you're expecting string values.
I checked code and its working properly. Just give name to submit button and echo validation message in php page.
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<form method="POST" action="test1.php">
<fieldset>
<legend>Personal Info</legend>
First name <input name="name" type="text">
Middle name <input name="middlename" type="text"> Surname <input name="lastname" type="text"> Age <input name="age" type="number"> Date of birth <input name="dob" type="date">
</fieldset>
<fieldset>
<legend>Regional & location info</legend>
Continent
<select>
<option value="europe">Europe</option>
<option value="americas">America</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option>
<option value="australia">Australia</option>
<option value="eurasia">Eurasia</option>
</select>
Country <input type="text"> State <input type="text"> City <input type="text">
Street number <input type="number"> Street name <input type="text"> <br><br>
Suburb <input type="text"> Postcode <input type="number"> If none of these apply to your accommodations, enter a typed location here <input type="text">
</fieldset>
<fieldset>
<legend>Previous lifestyle accommodations</legend>
Previous &/or most recent job title <input name="job" type="text"> First time job seeker <input type="checkbox" name="check1" value="ftjb"> I'm a student <input type="checkbox" name="check2" value="ias"> Previous &/or most recent acedemic title <input name="school" type="text"> First time applying for a qualification <input type="checkbox" name="check3" value="ftafaq"> I have work experience <input type="checkbox" name="check4" value="ihwe">
</fieldset>
<fieldset>
<legend>Details of arrival</legend>
Reason for arrival of all parties <input name="reason" type="text"> Date of arrival <input name="arrival" type="date"> Amount of stay expectancy <input type="checkbox" name="check3">Temporary <input type="checkbox" name="check4">Longterm
</fieldset>
<fieldset>
<legend>Signiture</legend>
<input type="text">
</fieldset>
<input type="submit" name="submit" value="Submit">
<button type="reset">Reset</button>
</form>
</body>
</html>
Display echo message in php page.
<?php
$nameInvalid = "";
$middleInvalid = "";
$surnameInvalid = "";
$ageInvalid = "";
$dobInvalid = "";
$countryInvalid = "";
$cityInvalid = "";
$strtInvalid = "";
$strnameInvalid = "";
$suburbInvalid = "";
$postcodeInvalid = "";
$jobInvalid = "";
$ftjsInvalid = "";
$iasInvalid = "";
$schoolInvalid = "";
$check1Invalid = "";
$check2Invalid = "";
$checl3Invalid = "";
$check4Invalid = "";
$reasonInvalid = "";
$arrivalInvalid = "";
if (isset($_POST['submit']))
{
if (empty($_POST["name"]))
{
echo $nameInvalid = "Name is required";
}
}
?>
Its done.

PHP form not validating form field properly

I'm learning some more PHP and after PHP itself to work, I can't seem to get it to validate any form field correctly. My goal is to check if the firstname field is empty, and if it is, it will give a message in a red color. The message in the red works, but only because the echo script is being called by the form submission, not because it has detected any empty field, because when I made an else statement to say "wassup" if its not empty, I got the same message for when the field is empty. Also, is there a way to check off multiple input fields at once like you could with JavaScript? For example if input1 == '' || input2 == '' and so on. Here is my HTML:
<html>
<head>
<title>Welcome</title>
</head>
<body>
<form action="welcome.php" method="post">
<fieldset>
<legend>Personal Info</legend>
First name <input name="name" type="text">
Middle name <input name="middlename" type="text">
Surname <input name="lastname" type="text">
Age <input name="age" type="number">
Date of birth <input name="dob" type="date">
</fieldset>
<fieldset>
<legend>Regional & location info</legend>
Continent
<select>
<option value="europe">Europe</option>
<option value="americas">America</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option>
<option value="australia">Australia</option>
<option value="eurasia">Eurasia</option>
</select>
Country <input name="country" type="text"> State <input type="text">
City <input name="city" type="text">
Street number <input name="streetno" type="number">
Street name <input name="streetname" type="text"> <br><br>
Suburb <input name="suburb" type="text"> Postcode <input name="postcode" type="number">
If none of these apply to your accommodations, enter a typed location here <input type="text">
</fieldset>
<fieldset>
<legend>Previous lifestyle accommodations</legend>
Previous &/or most recent job title <input name="job" type="text">
First time job seeker <input type="checkbox" name="check1" value="ftjb">
I'm a student <input type="checkbox" name="check2" value="ias">
Previous &/or most recent acedemic title <input name="school" type="text">
First time applying for a qualification <input type="checkbox" name="check3" value="ftafaq">
I have work experience <input type="checkbox" name="check4" value="ihwe">
</fieldset>
<fieldset>
<legend>Details of arrival</legend>
Reason for arrival <input name="reason" type="text">
Date of arrival <input name="arrival" type="date">
Amount of stay expectancy
<input type="checkbox" name="check3">Temporary
<input type="checkbox" name="check4">Longterm
</fieldset>
<fieldset>
<legend>Signiture</legend>
<input name='signiture' type="text">
</fieldset>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Here is my PHP code:
<?php
$firstname = $_POST['name'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$dob = $_POST['dob'];
$country = $_POST['country'];
$city = $_POST['city'];
$suburb = $_POST['suburb'];
$postcode = $_POST['postcode'];
$streetno = $_POST['streetno'];
$streetname = $_POST['streetname'];
$suburb = $_POST['suburb'];
$job = $_POST['job'];
$school = $_POST['school'];
$reason = $_POST['reason'];
$arrival = $_POST['arrival'];
$signiture = $_POST['signiture'];
if (isset($_POST['submit'])) {
if (empty($_POST[$firstname])) {
echo '<p style="color: red; text-align: center">Your first name is required</p>';
} else {
echo "wassaup";
}
}
?>
In your if statement, you need to do this:
if (empty($_POST['name'])) { //Or replace $_POST['name'] with $firstname
echo '<p style="color: red; text-align: center">Your first name is required</p>';
} else {
echo "wassaup";
}
You have a wrong syntax if (empty($_POST[$firstname])). You should use ' ' single paranthesis in firstname and remove this sign $.
change this if (empty($_POST[$name])) { to if (empty($_POST['firstname'])) { and check again. your syntax is wrong that's why it is not working.
Change in your code
if (!isset($_POST['firstname'] || empty($_POST['firstname'])) {
echo '<p style="color: red; text-align: center">Your first name is required</p>';
} else {
echo "wassaup";
}
First you can create error array where you will put all yours errors
$error = array();
then when you check for multiple fields
if (empty($_POST['firstname'])) $error[] = "First name can't be empty";
if (empty($_POST['lastname'])) $error[] = "Last name can't be empty";
// and so on
after you do all of this make statement to check if error array is empty, if not display errors
if (empty($error)) {
// do something
} else {
// error exists you want to display all errors
foreach ($error as $value) {
echo '<ul><li>'.$value.'</li></ul>';
}
}
in the html form, you have to add the attribute required in html tag and without using the PHP processing
First name <input name="name" type="text" required="required">
and if the user doesn't enter his First name, he will get the error message (that the first name is required)

Contact submit button redirects to blank .PHP url?

I have a contact form that I would like to send to my email address at my domain. However, upon hitting the "Submit" button, the website gets redirected to the .PHP url where it gives me:
HTTP ERROR 500: This page isn’t working "mywebsite.com" is currently unable to handle this request.
No email is received and the header("Location: ") doesn't redirect. I am currently using Bluehost. You can find the form I'm working on at acromojo.com/contact. I haven't added security yet, just trying to get the form to work first.
What am I doing wrong?
HTML:
<form method="post" action="contactform.php">
<div><select name="projectType">
<option value="0">Start A Project</option>
<option value="1">General Inquiry</option>
<option value="2">Collaborating</option>
</select></div></br>
<input name="name" type="text" placeholder="Full Name" required>
<input name="company" type="text" placeholder="Company / Organization"></br>
<input name="phone" type="phone" placeholder="Phone Number" required>
<input name="email" type="email" placeholder="Email Address"></br>
<input name="location" type="text" placeholder="Location">
<select name="find">
<option value="0">How did you hear about us?</option>
<option value="1">Social Media</option>
<option value="2">Search Engine</option>
<option value="3">Referrals / Recommendations</option>
<option value="4">Other</option>
</select></br></br>
<textarea name="message" placeholder="Please tell us a little about your project, timeline, and budget" row="4" required></textarea></br>
<label>Sign me up for the latest news, events, and more
<input name="newsletter" type="checkbox" checked="checked">
<span class="checkmark"></span>
</label>
<input type="submit" name="submit" value="SUBMIT">
</form>
PHP:
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$subject = $_POST['projectType'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$location = $_POST['location'];
$find = $_POST['find'];
$message = $_POST['message'];
$newsletter = $_POST['newsletter'];
$headers = "From: $email";
$txt = "You have received an e-mail from ".$name."\n\n"
"Location: ".$location."\n"
"Contact: ".$phone.", ".$email."\n"
"Found from: ".$find."\n\n"
.$message;
mail("hello#acromojo.com", $subject, $txt, $headers);
header("Location: contact.html?mailsent");
}
The answer came to me after aaaaaa123456789 commented. I checked the error_log where it was returning:
PHP Parse error: syntax error, unexpected '"Location: "' (T_CONSTANT_ENCAPSED_STRING)
Fix: "\r\n""Location: "
To: "\r\n Location: "

php/html form subject line missing

Having problem in sending form input to email, using php code.
<?php
$emailTo="testing_testing#gmail.com";
$subject = $POST_['username'];
$content = $_POST['course'] . $_POST['message'] . $_POST['tel'];
$headers = "From: ".$_POST['email'];
mail($emailTo, $subject, $content, $headers);
?>
HTML Form Code:
<form method="post">
<input id="username" type="text" name="username" placeholder="Name" required> </br>
<input id="tel" type = "tel" name="tel" placeholder="Phone no." required></br>
<input id="email" type="email" name="email" placeholder="Email id" required></br>
<input list="course" name="course">
<datalist id="course">
<option value="IELTS">
<option value="English Speaking">
<option value="Computers">
<option value="Accounting">
<option value="Fashion Designing">
<option value="Hospitality and Tourism">
<option value="General Enquiry">
</datalist>
<textarea id="message" name="message" rows="10" cols="30" placeholder="Any Message !"></textarea>
<INPUT id="submit" type="submit" value="Submit">
</form>
OUTPUT:
This code does not set the subject field as it says - nosubject. Where as in the php code I am assigning the value of username as the subject field.
The problem is your declaration of the $subject variable:
$subject = $POST_['username'];
Should be
$subject = $_POST['username'];
Hope this helps :)

How can I use autoincrement without the help of database in php

I have a form which user submits and is sent directly to the e-mail. The problem is that I'm not storing these values in database and directly mailing them. It is like a complaint registering system. What I want to do is that when the user submits a complaint and is redirected to success page, a complaint number is generated which should be obviously incremental by 1 for the next submission. Also there is no separate user account as anyone visiting the website can submit complaints. I tried using a field option as unique id but it didn't really work.
The html form is,
<form method="post" action="handler.php">
<div>
<label for="first_name"><span class="labelname"><strong>First Name:</strong></span></label>
<input type="text" maxlength="50" size="50" name="first_name" id="first_name" value="" class="required" />
</div>
<div>
<label for="last_name"><span class="labelname"><strong>Last Name:</strong></span></label>
<input type="text" maxlength="50" size="50" name="last_name" id="last_name" value="" class="required" />
</div>
<div>
<label for="telephone"><span class="labelname"><strong>Telephone Number:</strong></span></label>
<input type="text" maxlength="20" size="50" name="telephone" id="telephone" value="" class="required" />
</div>
<div>
<label for="email"><span class="labelname"><strong>E-mail: (Optional)</strong></span></label>
<input type="email" maxlength="30" size="50" name="email" id="email" value="" class="" />
</div>
<div>
<label for="com_type"><span class="labelname"><strong>Complaint Type:</strong></span></label>
<select name="com_type" id="com_type" class="required">
<option value=""></option>
<option value="Electrician">Electrician</option>
<option value="Plumber">Plumber</option>
<option value="Mason">Mason</option>
<option value="Miscellaneous">Miscellaneous</option>
</select>
</div>
<div>
<label for="flat_no"><span class="labelname"><strong>Flat No.:</strong></span></label>
<input type="text" maxlength="10" size="50" name="flat_no" id="flat_no" value="" class="required" />
</div>
<div>
<label for="block_no"><span class="labelname"><strong>Block Number:</strong></span></label>
<select name="block_no" id="block_no" class="required">
<option value=""> </option>
<option value="A-1">A-1</option>
<option value="A-2">A-2</option>
<option value="A-3">A-3</option>
<option value="A-4">A-4</option>
<option value="A-5">A-5</option>
<option value="A-6">A-6</option>
<option value="A-7">A-7</option>
<option value="B-1">B-1</option>
<option value="B-2">B-2</option>
<option value="B-3">B-3</option>
<option value="B-4">B-4</option>
<option value="C-1">C-1</option>
<option value="C-2">C-2</option>
</select>
</div>
<div>
<label for="message"><span class="labelname"><strong>Describe your problem:</strong></span></label>
<textarea rows="10" cols="50" maxlength="2000" name="message" id="message" class="required"></textarea>
</div>
<button class="submit" type="submit" name="submit" value="Send Email">Submit Complaint</button> <button class="reset" type="reset">Reset</button>
php code,
<?php
if(!isset($_POST['submit']))
{
die("Error. You need to submit the form.");
}
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$telephone = $_POST['telephone'];
$visitor_email = $_POST['email'];
$com_type = $_POST['com_type'];
$flat_no = $_POST['flat_no'];
$block_no = $_POST['block_no'];
$message = $_POST['message'];
$email_from = $visitor_email;
$email_subject = "New Complaint";
$email_body = "message\n\n". "First Name: $first_name\n\n".
"Last Name: $last_name\n\n".
"Telephone: $telephone\n\n". "Complaint Type: $com_type\n\n".
"Flat No.: $flat_no\n\n". "Block No.: $block_no\n\n".
"Complaint: $message";
$to = "my email.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
try{
mail($to,$email_subject,$email_body,$headers);
//success, redirect to thank
header('Location: http://mywebsite.com/thank.php');
} catch(Exception $e){
//problem, redirect to fail
header('Location: http://mywebsite.com/fail.php');
}
?>
I just want a complaint number on the successful submission page and the complaint number should also go in the mail also with other details. Can I do it without using the database. Please help.
If you have the numbers stored as a primary key, then you can do something like this:
SELECT COUNT(*) FROM `table`;
Or if you have the auto_increment or PRIMARY KEY set, you can use:
SELECT MAX(`id`) FROM `table`;
And then add + 1 to the result and insert it as new. If you aren't using a database, then use a flat file named count.txt for it and put in the current number and increment:
<?php
$count = file_get_contents("count.txt");
$count++;
file_put_contents("count.txt", $count);
?>
But this option is not so good. So please use a mechanism to lock the file while updating the count.

Categories