I have an HTML form which I will tailor later as a personal feedback form, if someone submits the form, I want the responses to be sent to my email id. I googled about it and apparently, it can be done with PHP but I'm not familiar with the PHP script, can someone give me a headstart using the first few labels, I will try to write the rest on my own, this is the code
<form id="survey-form">
<div class="form-group">
<label id="name-label" for="name">Name</label>
<input
type="text"
name="name"
id="name"
class="form-control"
placeholder="Enter your name"
required
/>
</div>
<div class="form-group">
<label id="email-label" for="email">Email</label>
<input
type="email"
name="email"
id="email"
class="form-control"
placeholder="Enter your Email"
required
/>
</div>
<div class="form-group">
<label id="number-label" for="number"
>Age<span class="clue">(optional)</span></label
>
<input
type="number"
name="age"
id="number"
min="10"
max="99"
class="form-control"
placeholder="Age"
/>
</div>
<div class="form-group">
<p>Which option best describes your current role?</p>
<select id="dropdown" name="role" class="form-control" required>
<option disabled selected value>Select current role</option>
<option value="student">Student</option>
<option value="job">Full Time Job</option>
<option value="learner">Full Time Learner</option>
<option value="preferNo">Prefer not to say</option>
<option value="other">Other</option>
</select>
</div>
<div class="form-group">
<p>Would you recommend freeCodeCamp to a friend?</p>
<label>
<input
name="user-recommend"
value="definitely"
type="radio"
class="input-radio"
checked
/>Definitely</label
>
<label>
<input
name="user-recommend"
value="maybe"
type="radio"
class="input-radio"
/>Maybe</label
>
<label
><input
name="user-recommend"
value="not-sure"
type="radio"
class="input-radio"
/>Not sure</label
>
</div>
<div class="form-group">
<p>
What is your favorite feature of freeCodeCamp?
</p>
<select id="most-like" name="mostLike" class="form-control" required>
<option disabled selected value>Select an option</option>
<option value="challenges">Challenges</option>
<option value="projects">Projects</option>
<option value="community">Community</option>
<option value="openSource">Open Source</option>
</select>
</div>
<div class="form-group">
<p>
What would you like to see improved?
<span class="clue">(Check all that apply)</span>
</p>
<label
><input
name="prefer"
value="front-end-projects"
type="checkbox"
class="input-checkbox"
/>Front-end Projects</label
>
<label>
<input
name="prefer"
value="back-end-projects"
type="checkbox"
class="input-checkbox"
/>Back-end Projects</label
>
<label
><input
name="prefer"
value="data-visualization"
type="checkbox"
class="input-checkbox"
/>Data Visualization</label
>
<label
><input
name="prefer"
value="challenges"
type="checkbox"
class="input-checkbox"
/>Challenges</label
>
<label
><input
name="prefer"
value="open-source-community"
type="checkbox"
class="input-checkbox"
/>Open Source Community</label
>
<label
><input
name="prefer"
value="gitter-help-rooms"
type="checkbox"
class="input-checkbox"
/>Gitter help rooms</label
>
<label
><input
name="prefer"
value="videos"
type="checkbox"
class="input-checkbox"
/>Videos</label
>
<label
><input
name="prefer"
value="city-meetups"
type="checkbox"
class="input-checkbox"
/>City Meetups</label
>
<label
><input
name="prefer"
value="wiki"
type="checkbox"
class="input-checkbox"
/>Wiki</label
>
<label
><input
name="prefer"
value="forum"
type="checkbox"
class="input-checkbox"
/>Forum</label
>
<label
><input
name="prefer"
value="additional-courses"
type="checkbox"
class="input-checkbox"
/>Additional Courses</label
>
</div>
<div class="form-group">
<p>Any comments or suggestions?</p>
<textarea
id="comments"
class="input-textarea"
name="comment"
placeholder="Enter your comment here..."
></textarea>
</div>
<div class="form-group">
<button type="submit" id="submit" class="submit-button">
Submit
</button>
</div>
</form>
I have a page where the users can register accounts which looks like this:
The problem is, when the user doesn't or forgot to fill up gender, it just submits the form without an error even if I have a required function on the select tag. It's only the gender that I'm having problems with. The required function works with the other fields.
here's my form:
<div class="main">
<div class="one">
<div class="register">
<center><h3>Add Account</h3></center>
<form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
<div>
<label>ID</label>
<input type="text" name="id" required>
</div>
<div>
<label>First Name</label>
<input type="text" name="firstname" required>
</div>
<div>
<label>Last Name</label>
<input type="text" name="lastname" required>
</div>
<div>
<label>Email</label>
<input type="text" name="email" placeholder="user#teamspan.com" required>
</div>
<div>
<label>Username</label>
<input type="text" name="username" required>
</div>
<div>
<label>Password</label>
<input type="password" name="password" required>
</div>
<div>
<label>Street Address</label>
<input type="text" name="street" required>
</div>
<div>
<label>Town/Suburb</label>
<input type="text" name="town" required>
</div>
<div>
<label>City</label>
<input type="text" name="city" required>
</div>
<div>
<label>Phone No.</label>
<input type="text" name="phone">
</div>
<div>
<label>Landline No.</label>
<input type="text" name="landline">
</div>
<div>
<label>Gender</label>
<select name="gender" required>
<option disabled selected hidden>Select Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
<div>
<label>User Levels</label>
<select name="user_levels" required>
<option disabled selected hidden>Select Access Level</option>
<option value="0">Employee</option>
<option value="1">Administrator</option>
<option value="2">Manager</option>
<option value="1">HR</option>
</select>
</div>
<div>
<label>Date</label>
<input type="text" readonly="readonly" name="date" value="<?php echo date("Y/m/d");?>" required>
</div>
<div>
<label>Sick Leave</label>
<input type="text" name="sickleave" required>
</div>
<div>
<label>Vacation Leave</label>
<input type="text" name="vacationleave" required>
</div>
<div>
<label>Picture (Link)</label>
<input type="text" name="picture" value="img/emp/" required>
</div>
<div>
<label></label>
<input type="submit" name="submit" value="Add Account" class="button" style="color: white;" />
<a href="hr_panel.php"><input type="button" value="Back" class="button" style="color: white;" />
</div>
</form>
</div>
</div>
You need to give value="" on first option i.e.Select Gender. Try following code
<div class="main">
<div class="one">
<div class="register">
<center><h3>Add Account</h3></center>
<form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
<div>
<label>ID</label>
<input type="text" name="id" required>
</div>
<div>
<label>First Name</label>
<input type="text" name="firstname" required>
</div>
<div>
<label>Last Name</label>
<input type="text" name="lastname" required>
</div>
<div>
<label>Email</label>
<input type="text" name="email" placeholder="user#teamspan.com" required>
</div>
<div>
<label>Username</label>
<input type="text" name="username" required>
</div>
<div>
<label>Password</label>
<input type="password" name="password" required>
</div>
<div>
<label>Street Address</label>
<input type="text" name="street" required>
</div>
<div>
<label>Town/Suburb</label>
<input type="text" name="town" required>
</div>
<div>
<label>City</label>
<input type="text" name="city" required>
</div>
<div>
<label>Phone No.</label>
<input type="text" name="phone">
</div>
<div>
<label>Landline No.</label>
<input type="text" name="landline">
</div>
<div>
<label>Gender</label>
<select name="gender" required>
<option disabled selected value="">Select Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
<div>
<label>User Levels</label>
<select name="user_levels" required>
<option disabled selected hidden>Select Access Level</option>
<option value="0">Employee</option>
<option value="1">Administrator</option>
<option value="2">Manager</option>
<option value="1">HR</option>
</select>
</div>
<div>
<label>Date</label>
<input type="text" readonly="readonly" name="date" value="<?php echo date("Y/m/d");?>" required>
</div>
<div>
<label>Sick Leave</label>
<input type="text" name="sickleave" required>
</div>
<div>
<label>Vacation Leave</label>
<input type="text" name="vacationleave" required>
</div>
<div>
<label>Picture (Link)</label>
<input type="text" name="picture" value="img/emp/" required>
</div>
<div>
<label></label>
<input type="submit" name="submit" value="Add Account" class="button" style="color: white;" />
<a href="hr_panel.php"><input type="button" value="Back" class="button" style="color: white;" />
</div>
</form>
</div>
</div>
Because you havent put value="" in Option
Try By This
<option value="" disable hidden>Select Gender</option>
I am trying to insert a row in a table but its giving me syntax error.
Here is the HTML Code:
<form method="post" action="" >
<!--form fields-->
<div class="control-group">
<div class="customer_data">
<div class="controls" id="id_milesPerDay">
<label for="ICAO" class="left-text">ICAO:</label>
<input type="text" id="ICAO" style='text-transform:uppercase' maxlength="4" name="ICAO" placeholder="" class="input-xlarge" required>
</div>
<div class="controls">
<label for="IATA" class="left-text">IATA:</label>
<input type="text" name="IATA" maxlength="3" style='text-transform:uppercase' id="IATA" placeholder="" class="input-xlarge" required>
</div>
<div class="controls">
<label for="airportname" class="left-text">Airport Name:</label>
<input type="text" name="airport_name" maxlength="" id="Airport_Name" placeholder="" class="input-xlarge" required>
</div>
<div class="controls">
<label for="coordinates" class="left-text">Coordinates:</label>
<input type="text" name="coordinates" maxlength="" id="Coordinates" placeholder="" class="input-xlarge" required>
</div>
<div class="controls">
<label for="Time_zone_UTC" class="left-text">Time Zone UTC:</label>
<input type="text" name="Time_zone_UTC" maxlength="3" id="Time_zone_UTC" placeholder="" class="input-xlarge" required>
</div>
<div class="controls">
<label for="DST" class="left-text">DST:</label>
<input type="checkbox" name="dst" value="other" id="DST" placeholder="" class="input-xlarge">
</div>
<div class="controls text">
<label for="utc" class="left-text">UTC:</label>
<input type="text" name="utc" maxlength="3" id="UTC" placeholder="" class="input-xlarge">
</div>
<div class="controls text">
<label for="From_date" class="left-text">From Date:</label>
<input type="date" name="from_date" maxlength="" id="fromdate" placeholder="" class="input-xlarge">
</div>
<div class="controls text">
<label for="To_date" class="left-text">To Date:</label>
<input type="date" name="to_date" maxlength="" id="todate" placeholder="" class="input-xlarge">
</div>
<!-- Button -->
<div class="control-group">
<div class="controls">
<input type="submit" value="Submit" id="Submit" class="btn btn-success" name="Submit">
</div>
</div>
</div>
<div class="customer_aircraft">
<div class="controls">
<label for="country_code" class="left-text">Country Code:</label>
<input type="text" name="country_code" maxlength="2" style='text-transform:uppercase' id="countrycode" placeholder="" class="input-xlarge" required>
</div>
<div class="controls">
<label for="Remarks" class="left-text">Remarks:</label>
<textarea cols="25" rows="7" name="remarks" id="remarks" placeholder="" class="input-xlarge"></textarea>
</div>
<div class="controls">
<label for="Country" class="left-text">Country: </label>
<input type="text" name="country" maxlength="" id="country" placeholder="" class="input-xlarge" required>
</div>
</div>
</div>
</form>
</body>
</html>
PHP Code:
<?php
$link=require ("Connection.php");
error_reporting(E_ALL); ini_set('display_errors', 1);
if(isset($_POST['Submit']))
{
$ICAO= mysqli_real_escape_string($link,$_POST['ICAO']);
$IATA= mysqli_real_escape_string($link,$_POST['IATA']);
$Airport_Name= mysqli_real_escape_string($link,$_POST['airport_name']);
$coordinates= mysqli_real_escape_string($link,$_POST['coordinates']);
$Time_zone_UTC= mysqli_real_escape_string($link,$_POST['Time_zone_UTC']);
$DST= mysqli_real_escape_string($link,$_POST['dst']);
$UTC= mysqli_real_escape_string($link,$_POST['utc']);
$from_date = mysqli_real_escape_string($link,$_POST['from_date']);
$to_date = mysqli_real_escape_string($link,$_POST['to_date'];
$country_code= mysqli_real_escape_string($link,$_POST['country_code']);
$remarks= mysqli_real_escape_string($link,$_POST['remarks']);
$Country= mysqli_real_escape_string($link,$_POST['country']);
//inserting records//
$sql="INSERT INTO airport_data (ICAO,IATA,Airport_name,Coordinates,Time_zone_utc,DST,UTC,From,To,Country_code,Remarks,Country) VALUES ('$ICAO','$IATA','$Airport_Name','$coordinates','$Time_zone_UTC','$DST','$UTC',STR_TO_DATE('$from_date', '%m/%d/%y'),STR_TO_DATE('$to_date', '%m/%d/%y'),'$country_code','$remarks','$Country')";
if(mysqli_query($link, $sql))
{
echo "Records added successfully.";
}
else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
}
?>
Here is the error I receive:
ERROR: Could not able to execute INSERT INTO airport_data (ICAO,IATA,Airport_name,Coordinates,Time_zone_utc,DST,UTC,From,To,Country_code,Remarks,Country) VALUES ('oprn','op','islamabad airport','islam','+9','other','+8',STR_TO_DATE('2015-05-26', '%m/%d/%y'),STR_TO_DATE('2015-05-20', '%m/%d/%y'),'op','p','Pakistan'). You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'From,To,Country_code,Remarks,Country) VALUES ('oprn','op','islamabad airport','i' at line 1
Escape (at least) reserved words in column names.
... `From`, `To`, ...
Full list of MySQL reserved words is in http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html.
from and to are reserve keyword. Avoid to use them as column name. You need to change these column name.
You can find more details here
Actually the format of my date variables was wrong so i had to change it like
$from_date = new DateTime($_POST['from_date']);
$to_date = new DateTime($_POST['to_date']);
Insert the following into the database
$from_date = mysqli_real_escape_string($link, $from_date->format('Y-m-d'));
$to_date = mysqli_real_escape_string($link, $to_date->format('Y-m-d'));
And in query
some thing like
,'".$from_date."','".$to_date."',
I apologize in advance if this is an easy fix as I'm somewhat new to learning PHP. I found related questions and tried those answers, but none applied to my issue.
I am receiving an email from my contact form where some of the fields show up and some do not. On the Address field, I only receive the country, and for the Date Available field, I only receive the year. These are the current issues. I thought maybe it could be the input id, but it is only those two giving me a problem right now.
I sincerely appreciate any help you can offer.
PHP:
<?php
$myemail = "myemail#gmail.com";
$subject = "Employment Application Submission from Your Website";
/* Check all form inputs using check_input function */
$position = check_input($_POST['position']);
$refer = check_input($_POST['refer']);
$name = check_input($_POST['name'], "Enter your name");
$address = check_input($_POST['address']);
$phone = check_input($_POST['phone']);
$email = check_input($_POST['email']);
$dateavail = check_input($_POST['dateavail']);
$social = check_input($_POST['social']);
$dob = check_input($_POST['dob']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Position Applied For: $position
Referred by: $refer
Name: $name
Address: $address
Phone: $phone
E-mail: $email
Date available: $dateavail
SSN: $social
DOB: $dob
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
HTML:
<form id="form_884913" method="post" action="send_form_email1.php" class="appnitro">
<ul >
<li id="li_1" >
<label class="description" for="position">Position Applied For </label>
<div>
<input id="element_1" name="position" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li> <li id="li_2" >
<label class="description" for="refer">How were you referred to us? </label>
<div>
<input id="element_2" name="refer" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li> <li id="li_3" >
<label class="description" for="name">Full Name (first, middle, last) </label>
<div>
<input id="element_3" name="name" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li> <li id="li_4" >
<label class="description" for="address">Address </label>
<div>
<input id="element_4_1" name="address" class="element text large" value="" type="text">
<label for="address" style="font-size: 12px;">Street Address</label>
</div>
<div>
<input id="element_4_2" name="address" class="element text large" value="" type="text">
<label for="address" style="font-size: 12px;">Address Line 2</label>
</div>
<div class="left">
<input id="element_4_3" name="address" class="element text medium" value="" type="text">
<label for="address" style="font-size: 12px;">City</label>
</div>
<div class="right">
<input id="element_4_4" name="address" class="element text medium" value="" type="text">
<label for="address" style="font-size: 12px;">State / Province / Region</label>
</div>
<div class="left">
<input id="element_4_5" name="address" class="element text medium" maxlength="15" value="" type="text">
<label for="address" style="font-size: 12px;">Postal / Zip Code</label>
</div>
<div class="right">
<select class="element select medium" id="element_4_6" name="address">
<option value="" selected="selected"></option>
<option value="Afghanistan" >Afghanistan</option>
</select>
<label for="address" style="font-size: 12px;">Country</label>
</div>
</li> <li id="li_5" >
<label class="description" for="phone">Phone </label>
<span>
<input id="element_5_1" name="phone" class="element text" size="3" maxlength="3" value="" type="text"> -
<label for="element_5_1">(###)</label>
</span>
<span>
<input id="element_5_2" name="phone" class="element text" size="3" maxlength="3" value="" type="text"> -
<label for="element_5_2">###</label>
</span>
<span>
<input id="element_5_3" name="phone" class="element text" size="4" maxlength="4" value="" type="text">
<label for="element_5_3">####</label>
</span>
</li> <li id="li_6" >
<label class="description" for="email">E-Mail Address </label>
<div>
<input id="element_6" name="email" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li> <li id="li_7" >
<label class="description" for="dateavail">Date Available to Start </label>
<span>
<input id="element_7_1" name="dateavail" class="element text" size="2" maxlength="2" value="" type="text"> /
<label for="dateavail">MM</label>
</span>
<span>
<input id="element_7_2" name="dateavail" class="element text" size="2" maxlength="2" value="" type="text"> /
<label for="dateavail">DD</label>
</span>
<span>
<input id="element_7_3" name="dateavail" class="element text" size="4" maxlength="4" value="" type="text">
<label for="dateavail">YYYY</label>
</span>
<span id="calendar_7">
<img id="cal_img_7" class="datepicker" src="calendar.gif" alt="Pick a date.">
</span>
<script type="text/javascript">
Calendar.setup({
inputField : "element_7_3",
baseField : "element_7",
displayArea : "calendar_7",
button : "cal_img_7",
ifFormat : "%B %e, %Y",
onSelect : selectDate
});
</script>
</li> <li id="li_8" >
<label class="description" for="social">Social Security Number </label>
<div>
<input id="element_8" name="social" class="element text small" type="text" maxlength="255" value=""/>
</div>
(I did not display the entire form for length purposes)
You have several input's with the same name. You need to give each input element a unique name. If you have multiple elements with the same name only the last one will be sent to the server.
An easy solution:
Change your PHP:
$address = check_input($_POST['address']." ".$_POST['address2']." ".$_POST['address3']." ".$_POST['address4']." ".$_POST['address5']);
Change your HTML:
<div>
<input id="element_4_1" name="address" class="element text large" value="" type="text">
<label for="address" style="font-size: 12px;">Street Address</label>
</div>
<div>
<input id="element_4_2" name="address2" class="element text large" value="" type="text">
<label for="address2" style="font-size: 12px;">Address Line 2</label>
</div>
<div class="left">
<input id="element_4_3" name="address3" class="element text medium" value="" type="text">
<label for="address3" style="font-size: 12px;">City</label>
</div>
<div class="right">
<input id="element_4_4" name="address4" class="element text medium" value="" type="text">
<label for="address4" style="font-size: 12px;">State / Province / Region</label>
</div>
<div class="left">
<input id="element_4_5" name="address5" class="element text medium" maxlength="15" value="" type="text">
<label for="address5" style="font-size: 12px;">Postal / Zip Code</label>
</div>
You have multiple <input> with the same name:
<input id="element_4_1" name="address" class="element text large" value="" type="text">
<input id="element_4_2" name="address" class="element text large" value="" type="text">
Since the name is what appears as the key in $_POST, you're essentially overwriting all of your inputs with the LAST name="address" that appears in the form.
Each input must have a unique name. Which you already have in your $foo = $_POST[...], so why you don't have the matching names in your form is a mystery.
you are using same names for input fields .. give different names for name attributes in input fields.
<input id="element_4_1" name="streetaddress" class="element text medium" value="" type="text">
<input id="element_4_2" name="addressline2" class="element text medium" value="" type="text">
while receiving in php.. get one by one. example
$doorNo = $_POST['streetaddress'];
$streetName= $_POST['addressline2'];
like wise for all fields.. same applies to all other issues.
I'm working through some legacy code from a previous developer and I'm very new to php. Here is my challenge, the registration form works as is.
The problem is the drop down menu that allows a user to self select their usertype does not appear on the form/page. I need it to.
Currently this bit of code stores all new users in the db as user1.
How can I get the drop down to show? And alter it from auto saving as user1?
index.html.php
<body id="page-login" onload="document.registerForm.name.focus();">
'. getMsg() .'
<div class="login-container">
<div class="logo_login"><img src="'. $mainframe->cfg->site_url .'/images/logo.gif"></div>
<div class="login-box">
<form method="post" action="" name="registerForm" id="registerForm" autocomplete="off">
<div class="login-form">
<h2>'. LANG::_('register') .'</h2>
<div class="input-box input-left"><label for="name">'. LANG::_('name') .':</label><br/>
<input type="text" class="txt" id="name" name="name" value="" class="required-entry input-text" style="width:130px;"/>
</div>
<div class="input-box input-left"><label for="link">'. LANG::_('Add your profile link') .':</label><br/>
<input type="text" class="txt" id="link" name="link" value="" class="required-entry input-text" style="width:130px;"/>
</div>
<div class="input-box input-left"><label for="username">'. LANG::_('user_name') .':</label><br/>
<input type="text" class="txt" id="username" name="username" value="" class="required-entry input-text" style="width:130px;"/>
</div>
<div class="input-box input-left"><label for="email">'. LANG::_('email_address') .':</label><br/>
<input type="text" class="txt" id="email" name="email" value="" class="required-entry input-text" style="width:130px;"/>
</div>
<div class="input-box input-left"><label for="password">'. LANG::_('password') .':</label><br />
<input type="password" class="txt" id="password" name="password" class="required-entry input-text" value="" style="width:130px;"/>
</div>
'. ($mainframe->cfg->allow_only_coach_or_player ? '<div class="input-box input-left"><label for="usertype">'. LANG::_('register_me_as') .':</label><br />
<select name="usertype" class="required-entry select">
<option value="">--'. LANG::_('choose_one') .'--</option>
<option value="Coach">'. LANG::_('contest_holder') .'</option>
<option value="Player">'. LANG::_('contest_participant') .'</option>
</select>
</div>'
: '<input name="usertype" value="user1" />') .'
<div class="input-box input-left"><label for="password2">'. LANG::_('r_password') .':</label><br />
<input type="password" class="txt" id="password2" name="password2" class="required-entry input-text" value="" style="width:130px;"/>
</div>`