PHP fetch the value from the select html page - php

i have below html code
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" class="contact">
<label id="name">
<h4>name*</h4>
<input type="text" name="name" placeholder="full name" autofocus/>
</label>
<label id="city">
<h4>city</h4>
<select name="city">
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
</select>
</label>
PHP code
if($_POST["name"] == null)
{
echo "<p class='wrong'><strong>wrong: </strong> name is empty</p>";
}
if($_POST["city"] == null)
{
echo "<p class='wrong'><strong>wrong: </strong> city is empty</p>";
}
this to check the null value and always gives the city is empty that means no value selected

Cannot comment yet.
Maybe try and close the label before the select.
<label id="city">
<h4>city</h4>
</label>
<select name="city">
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
</select>
and maybe do
<?php echo "<pre>".print_r($_POST,true)."<pre>";
to see what values are set.
And as others have suggested check of the value is empty()
if(!isset($_POST['city'])||empty($_POST['city']))
This returns true if city is empty, has no value or true if it doesn't exist.
Hope this helps!
Edit 1:
Also since the php code is probably on the same page, could just omit the action attribute.
<form method="POST" class="contact">
<label for="city">
<h4>city</h4>
</label>
<select id="city" name="city">
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
</select>
</form>
also use the for attribute with the ID of the input field you want it to represent

Related

Creating a dynamic form in php

I am trying to create a dynamic HTML form where the fields displayed are based on inputs in the field above it. So far, I have only found resources that work in JS but I was wondering if there was a pure PHP way to do it.
To give some context: I would like to create a form for a user to set their goal. The 'How Much' and 'programme duration' fields are dependent on the 'goal type' field. Ie. if They choose a muscle building goal then it will only show 5% etc. Here is my code so far...
Much appreciated in advanced!
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
session_start();
if (isset($_SESSION['Username'])) {
?>
<html>
<head>
</head>
<body>
<h1>Please set your goal and programme type</h1>
<form method="POST" action="Set_Goal_process.php" >
<label for="Sex"><b>Sex: </b></label>
<select name="Sex" id="Sex" required>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<label for="Age"><b>Age: </b></label>
<input type="number" name="Age" id="Age" value="Age" required>
<label for="Height"><b>Height: </b></label>
<input type="number" name="Height" id="Height" required>
<label for="Current Weight"><b>Current Weight: </b></label>
<input type="number" name="Initial_weight" id="Initial Weight" required>
<br><br><br><br><br><br><br><br><br>
<label for="Activity level"><b>Activity level: </b></label>
<select name="Activity_level" id="Activity level" required>
<option value=1>Sedentary</option>
<option value=2>Mostly sedentary</option>
<option value=3>Lightly active</option>
<option value=4>Highly active</option>
</select>
<label for="Goal type"><b>Goal type: </b></label>
<select name="Goal_type" id="Goal type" required>
<option value=1>Fat Loss</option>
<option value=2>Maintenance</option>
<option value=3>Muscle Building</option>
</select>
<label for="How much"><b>How much: </b></label>
<select name="How_much" id="How much" default="0" required>
<option value=-10>-10%</option>
<option value=-7.5>-7.5%</option>
<option value=-5>-5%</option>
<option value=-2.5>-2.5%</option>
<option value=0 selected>0%</option>
<option value=2.5>2.5%</option>
<option value=5>5%</option>
</select>
<!-- comment make both How much and program duration conditional -->
<label for="Program Duration"><b>Program Duration: </b></label>
<select name="Program_duration" id="Program Duration" required>
<option value=4>4 weeks</option>
<option value=8>8 weeks</option>
<option value=12 selected>12 weeks</option>
</select>
<label for="Experience_level"><b>Experience level: </b></label>
<select name="Expereince_level" id="Expereince level" required>
<option value=1 selected>Beginner</option>
<option value=2>Novice</option>
<option value=3 >Intermediate</option>
<option value=4 >Advanced</option>
</select>
<label for="Sessions per week">Sessions per week:</label>
<input type="number" name="Sessions_per_week" id="Sessions per week" min="1" max="7" required>
<label for="Start date">Start date:</label>
<input type="date" name="Start date"id="Start date" >
<br><br>
<input type="submit" name="Submit" value="Get Started!"required>
</form>
</body>
</html>
<?php
}else {
session_destroy();
header("location: index.php");
}

My PhP file is not picking up the variables from my HTML form [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
i have created a form with a PHP file, this is for a booking request and the aim is to get the variables sent to an email address. But for some reason the variables.
Below is the code for my html arm:
<form action="form.php" method="post">
<fieldset>
<legend>Book your Journey</legend>
<label for ="name">Name:</label>
<input type="text" id="name" name="name">
<label for="number">Phone Number</label>
<input type="number" id="number" name="number">
</fieldset>
<fieldset>
<label for="email">Email:</label>
<input type="email" id="email" name="user_email">
<label for="pickup">Pick Me Up From</label>
<input type="text" id="pickup" name="pickup">
<label for="date">Date</label>
<input type="text" id="datepicker" name="date">
<label>Time:</label>
<select>
<option>--HR--</option>
<option value="1">01</option>
<option value="2">02</option>
<option value="3">03</option>
<option value="4">04</option>
<option value="5">05</option>
<option value="6">06</option>
<option value="7">07</option>
<option value="8">08</option>
<option value="9">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<label>:</label>
<select>
<option>--MIN--</option>
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
</select>
<select>
<option value="am">am</option>
<option value="pm">pm</option>
</select>
</fieldset>
<fieldset>
<label for="destination"> Destination Postal Code </label>
<input type="text" id="destination" name="destination">
</fieldset>
<button>Submit</button>
</form>
and now my php code for this form is as follows
<?php
$to = 'test#gmail.com';
$subject = 'Booking';
$name = $_POST['name'];
$email = $_POST['email'];
$pickup = $_POST['pickup'];
$destination = $_POST['destination'];
$date = $_POST['date'];
$message = <<<Eod
Hi, please pick up $name from $pickup and drop to $destination
Eod;
$header = $email;
mail($to, $subject, $message, $header);
echo "Message Sent";
?>
Now, when i receive the email i can see only $destination gets picked up. Have i done something wrong in my html, it looks fine to me. Also, how do i get my select elements as a variable.
If destination already gets through, you do most likely have some mistake in the naming of the name attribute. Make sure it matches 1:1 the names you try to get in your PHP.
Secondary, regarding your select, you can get the value via the name attribute the same way you get all the text elements. Inputs, in whatever form (except of files), are just strings which do get through the protocol. It does not make any difference for PHP if the UI is different.
The form fields you want to capture the value of must all have name attributes and they should match what you use in PHP. For example $_POST['user_email'] not $_POST['email']. Note that none of your <select> elements have the name attribute.
HTML
You omitted the name attributes in the select options. I added hour, min and periods. Also you had user_email value in the name attribute for email input field.
In the PHP code, i've assigned concatenated and assigned the POST
values for the time and updated the message
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<fieldset>
<legend>Book your Journey</legend>
<label for ="name">Name:</label>
<input type="text" id="name" name="name">
<label for="number">Phone Number</label>
<input type="number" id="number" name="number">
</fieldset>
<fieldset>
<label for="email">Email:</label>
<input type="email" id="email" name="user_email">
<label for="pickup">Pick Me Up From</label>
<input type="text" id="pickup" name="pickup">
<label for="date">Date</label>
<input type="text" id="datepicker" name="date">
<label>Time:</label>
<select name="hour">
<option>--HR--</option>
<option value="1">01</option>
<option value="2">02</option>
<option value="3">03</option>
<option value="4">04</option>
<option value="5">05</option>
<option value="6">06</option>
<option value="7">07</option>
<option value="8">08</option>
<option value="9">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<label>:</label>
<select name="min">
<option>--MIN--</option>
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
</select>
<select name="period">
<option value="am">am</option>
<option value="pm">pm</option>
</select>
</fieldset>
<fieldset>
<label for="destination"> Destination Postal Code </label>
<input type="text" id="destination" name="destination">
</fieldset>
<button>Submit</button>
</form>
PHP
<?php
$to = 'test#gmail.com';
$subject = 'Booking';
$name = $_POST['name'];
$phone = $_POST['number'];
$email = $_POST['user_email'];
$pickup = $_POST['pickup'];
$destination = $_POST['destination'];
$date = $_POST['date'];
$time = $_POST['hour'].':'.$_POST['min'].''.$_POST['period'];
echo "Message Sent";
?>
Hope this helps
<select class="form-control" name="hour">
<option>--HR--</option>
<option value="1">01</option>
<option value="2">02</option>
<option value="3">03</option>
</select>
<label>:</label>
<select class="form-control" name="minutes">
<option>--MIN--</option>
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
</select>
<select class="form-control" name="time_of_day">
<option value="am">am</option>
<option value="pm">pm</option>
</select>
in php:
//grab the value the user select for hour from the $_POST, which is an array which stores all values submitted via the post method
$hour = $_POST['hour'];
$minutes = $_POST['minutes'];
$time_of_day = $_POST['time_of_day'];
//concate your variable to output what the user selected so it would out put something like 4:30 pm
$time = $hour.':'.$minutes.''.$time_of_day;
Essentially, you are adding the name attribute to the select statement like what you did for you inputs and you are accessing them the same way via php. if you do die(var_dump($_POST)); you will see have thing that has been submitted. If you do not see it in the dump then you either know you need to revisit the code

validation for data (select option) from database

im trying to make the validation for the select option. tried with using disabled but the first data is displayed as the first choice. how can i used validation here but not with the data as the first choice but "Select Type". for input i just used class required="form-control". how about select option?
<div class="form-group">
<label class="control-label col-sm-4" for="vehicleType">Vehicle Type:</label>
<div class="col-sm-5">
<select class="form-control required" name="vehicleType" >
<option value="" disabled="disabled">Select Type</option>
<?php
$res=mysqli_query($link,"Select * from vehicletype where status_vehicleType='1'");
while ($row=mysqli_fetch_array($res)) {
?>
<option value=<?php echo $row['id_vehicleType'];?>><?php echo $row['vehicle_Type'];?></option>
<?php
}
?>
</select>
</div>
disabled skip the "select type" option and display the first data.
You can add selected attribute to first <option> tag inside while loop.
<div class="form-group">
<label class="control-label col-sm-4" for="vehicleType">Vehicle Type:
</label>
<div class="col-sm-5">
<select class="form-control" name="vehicleType" required >
<option value="">Select Type</option>
<?php
$res=mysqli_query($link,"Select * from vehicletype where status_vehicleType='1'");
while ($row=mysqli_fetch_array($res)) {
?>
<option value=<?php echo $row['id_vehicleType'];?>><?php echo $row['vehicle_Type'];?></option>
<?php
}
?>
</select>
this worked for me. i just get rid the disabled and only put "required" in the attributes element without any "=" " ". didnt know required also works with this one.

Only show list option based on earlier form choice

Here's part of my form:
<label for="User">Select your account type:</label>
<select name="usertype" for="usertype" id="usertype" required>
<option value="1">Employee</option>
<option value="2">Manager</option>
</select>
<label for="User">Select the company you work for:</label>
<select name="company" for="company" id="company" required>
<?php
include ('processors/generate-company-list.php');
?>
<option value="NULL">I'll create my own company later</option>";
</select>
I only want <option value="NULL">I'll create my own company later</option> to be available if they choose to be a manager earlier in the form. Is this possible?
<script type="text/javascript">
function check(){
var usertype=document.getElementById("usertype").value;
if(usertype==2){
document.getElementById("company").selectedIndex = "0";
document.getElementById("company").disabled=true;
}else{
document.getElementById("company").disabled=false;
}
}
</script>
<label for="User">Select your account type:</label>
<select name="usertype" for="usertype" id="usertype" required onchange='check();'>
<option value="1">Employee</option>
<option value="2">Manager</option>
</select>
<label for="User">Select the company you work for:</label>
<select name="company" for="company" id="company" required>
<?php
include ('processors/generate-company-list.php');
?>
<option value="NULL">I'll create my own company later</option>";
<option value="1"> listitem1 </option>";
<option value="1"> listitem2</option>";
</select>
I used javascript function to select 0 index whenever manager is selected and disabled second select box.

Access value of selected option in <select> element with PHP

I am trying to create a simple form that will send data to a PHP script upon submitting the form. I am able to access all of the textbox values, and have successfully implemented variables for each of them. However, when I try to access the value of a combo box on my form, I get this error in the PHP:
Notice: Undefined index: cmbProvince in C:\inetpub\wwwroot\krAssignment4\scripts\index.php on line 15
HTML
<form id="orderForm" action="scripts/index.php" method="post" onsubmit="return validateForm()">
<fieldset id="customerInfo">
<legend>Customer Information</legend>
<label for="txtFirstName">Name:</label><br>
<input type="text" id="txtFirstName" name="txtFirstName"
placeholder="First Name" autofocus="true"
oninput="formatFirstName()">
<input type="text" id="txtLastName" name="txtLastName"
placeholder="Last Name" oninput="formatLastName()"><br><br>
<label for="txtAddress">Address:</label><br>
<input type="text" id="txtAddress" name="txtAddress" placeholder="Address"
oninput="formatAddress()"><br>
<input type="text" id="txtCity" name="txtCity" placeholder="City"
oninput="formatCity()">
<select id="cmbProvince" name="cmbProvince">
<option value="on">ON</option>
<option value="qc">QC</option>
<option value="mb">MB</option>
<option value="sk">SK</option>
<option value="ab">AB</option>
<option value="bc">BC</option>
<option value="nb">NB</option>
<option value="nl">NL</option>
<option value="ns">NS</option>
<option value="nu">NU</option>
<option value="pe">PE</option>
<option value="nt">NT</option>
<option value="yt">YT</option>
</select><br>
<input type="text" id="txtPostalCode" name="txtPostalCode"
placeholder="Postal Code" oninput="displayPostalCode()">
<br><br>
<!-- Button that will display the order form -->
<input type="button" id="btnOrder" value="Order"
onclick="showOrderInformation()">
</fieldset><br>
<fieldset id="orderInfo">
<legend>Order Information</legend>
<label for="cmbProduct">Product:</label><br>
<select id="cmbProduct">
<option> -- Select -- </option>
<option>Blackberries</option>
</select>
 # 
<input type="number" id="txtQuantity" name="txtQuantity" value="1"
step="1" min="1" max="50" oninput="displayTotalTax()"><br><br>
</fieldset>
<br><br>
<!-- Button to submit the form to PHP after validation -->
<input type="submit" id="btnSubmit" value="Submit">
</form>
PHP (lines 10 - 19)
/* Capture the user input from the HTML form */
$strFirstName = $_POST['txtFirstName'];
$strLastName = $_POST['txtLastName'];
$strAddress = $_POST['txtAddress'];
$strCity = $_POST['txtCity'];
$strProvince = $_POST['cmbProvince'];
$strPostalCode = $_POST['txtPostalCode'];
Am I typing something wrong? Or missing something? Everything except the combo box is working.
Let me know if I need to include anything else. Thanks!
EDIT
Here is what happens when I try var_dump($_POST);:
Notice: Undefined index: cmbProvince in C:\inetpub\wwwroot\krAssignment4\scripts\index.php on line 15
Notice: Undefined index: cmbProduct in C:\inetpub\wwwroot\krAssignment4\scripts\index.php on line 18
array(6) { ["txtFirstName"]=> string(7) "Kendall"
["txtLastName"]=> string(4) "Roth"
["txtAddress"]=> string(19) "22 Resolution Drive"
["txtCity"]=> string(8) "Millbank"
["txtPostalCode"]=> string(7) "K2K 2K2"
["txtQuantity"]=> string(1) "2" }
Your code works for me with a little bit changes, try this:
<form id="orderForm" action="scripts/index.php" method="post" onsubmit="return validateForm()">
<fieldset id="customerInfo">
<legend>Customer Information</legend>
<label for="txtFirstName">Name:</label><br>
<input type="text" id="txtFirstName" name="txtFirstName"
placeholder="First Name" autofocus="true"
oninput="formatFirstName()">
<input type="text" id="txtLastName" name="txtLastName"
placeholder="Last Name" oninput="formatLastName()"><br><br>
<label for="txtAddress">Address:</label><br>
<input type="text" id="txtAddress" name="txtAddress" placeholder="Address"
oninput="formatAddress()"><br>
<input type="text" id="txtCity" name="txtCity" placeholder="City"
oninput="formatCity()">
<select id="cmbProvince" name="cmbProvince">
<option value="on">ON</option>
<option value="qc">QC</option>
<option value="mb">MB</option>
<option value="sk">SK</option>
<option value="ab">AB</option>
<option value="bc">BC</option>
<option value="nb">NB</option>
<option value="nl">NL</option>
<option value="ns">NS</option>
<option value="nu">NU</option>
<option value="pe">PE</option>
<option value="nt">NT</option>
<option value="yt">YT</option>
</select><br>
<input type="text" id="txtPostalCode" name="txtPostalCode"
placeholder="Postal Code" oninput="displayPostalCode()">
<br><br>
<!-- Button that will display the order form -->
<input type="button" id="btnOrder" value="Order"
onclick="showOrderInformation()">
</fieldset><br>
<fieldset id="orderInfo">
<legend>Order Information</legend>
<label for="cmbProduct">Product:</label><br>
<select id="cmbProduct" name="cmbProduct"> //changes is in this code
<option> -- Select -- </option>
<option value="Blackberries">Blackberries</option>
</select>
 # 
<input type="number" id="txtQuantity" name="txtQuantity" value="1"
step="1" min="1" max="50" oninput="displayTotalTax()"><br><br>
</fieldset>
<br><br>
<!-- Button to submit the form to PHP after validation -->
<input type="submit" id="btnSubmit" value="Submit">
</form>
In your index.php write:
<?php
if(isset($_POST) && !empty($_POST)){
echo "<pre/>";print_r($_POST);die;
}
?>
Output:-
Array
(
[txtFirstName] => a
[txtLastName] => b
[txtAddress] => c
[txtCity] => d
[cmbProvince] => qc
[txtPostalCode] => e
[cmbProduct] => Blackberries
[txtQuantity] => 1
)
Kendall, you were:
missing name for your <select id="cmbProduct">, and
missing value for your cmbProduct options as well
Putting in 'disabled selected' is a good practice as well as people won't be able to select the 'select' option :)
Your code
<select id="cmbProduct">
<option> -- Select -- </option>
<option>Blackberries</option>
</select>
Correct code
<select id="cmbProduct" name="cmbProduct>
<option value="select" disabled selected> -- Select -- </option>
<option value="blackberries">Blackberries</option>
</select>

Categories