Creating a dynamic form in php - 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");
}

Related

Is there any way to write PHP code inside .twig file in WordPress? I am trying to send a mail by using the form values from a .twig template file

Is there any way to write PHP code inside .twig file in WordPress? I am trying to send a mail by using the form values from a .twig template file.
page-pledgeform.twig
{% extends "base.twig" %}
{% block content %}
<body class="{{body_class}}">
<div class="wrapper">
<section id="content" role="main" class="content-wrapper">
{% block body %}
<!-- strat_body_content -->
<div class="body_content">
<div class="space_height"></div>
<div class="form_section">
<div class="container">
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<!-- Top Navigation -->
<div class="main clearfix">
<form id="nl-form" method="POST" action="" class="nl-form">
I am <input type="text" name="name" id="user_name" value="" placeholder="Name" data-subline="For example: <em>Mr. K. Roy</em>"/>
It is a long <input type="text" value="" placeholder="Country name" data-subline="For example:
<em>India</em>"/>
Gender
<select>
<option value="1" selected>Male</option>
<option value="2">Female</option>
<option value="3">Other</option>
</select>
Lorem
<select>
<option value="1" selected>O</option>
<option value="2">B</option>
<option value="3">B+</option>
<option value="4">B-</option>
<option value="2">A-</option>
</select>
.
There are many <input type="text" value="" placeholder="Address" data-subline="For example: <em>Los Angeles</em> or <em>New York</em>"/>
passages of
<select>
<option value="1" selected>Mumbai</option>
<option value="2">Delhi</option>
<option value="3">Bangalore</option>
<option value="4">Hyderabad</option>
<option value="5">Ahmedabad</option>
<option value="6">Kolkata</option>
</select>
classical Latin
<select>
<option value="1" selected>Gujarat</option>
<option value="2">Andhra Pradesh</option>
<option value="3">Tamil Nadu</option>
<option value="4">Maharashtra</option>
<option value="5">West Bengal</option>
</select>
first true
<select>
<option value="1" selected>Vijayawada</option>
<option value="2">Kanyakumari</option>
<option value="3">Rewalsar</option>
<option value="4">Dehradun</option>
<option value="2">24 parganas</option>
</select>
There are many <input type="text" value="" placeholder="Pin code" data-subline="For example: <em>700049</em>"/>
There are <input type="email" value="" placeholder="Email ID" data-subline="For example: <em>mac#gmail.com</em>"/>
Phone <input type="number" value="" placeholder="Phone Number" data-subline="For example: <em>000-123-000</em>"/>
Mobile number <input type="number" value="" placeholder="Mobile Number" data-subline="For example: <em>000-123-000</em>"/>
My information may be shared with the
<select>
<option value="1" selected>Yes</option>
<option value="2">No</option>
</select>
<div class="nl-overlay"></div>
<br><br><br>
<h4>Emergency Contact Person Details :-</h4>
I am <input type="text" name="name" value="" placeholder="Name" data-subline="For example: <em>Mr. k. Roy</em>"/>
It is a long <input type="text" value="" placeholder="Country name" data-subline="For example: <em>India</em>"/>
Gender
<select>
<option value="1" selected>Male</option>
<option value="2">Female</option>
<option value="3">Other</option>
</select>
Lorem
<select>
<option value="1" selected>O</option>
<option value="2">B</option>
<option value="3">B+</option>
<option value="4">B-</option>
<option value="2">A-</option>
</select>
.
There are many <input type="text" value="" placeholder="Address" data-subline="For example: <em>India</em>"/>
passages of
<select>
<option value="1" selected>Mumbai</option>
<option value="2">Delhi</option>
<option value="3">Bangalore</option>
<option value="4">Hyderabad</option>
<option value="5">Ahmedabad</option>
<option value="6">Kolkata</option>
</select>
classical Latin
<select>
<option value="1" selected>Gujarat</option>
<option value="2">Andhra Pradesh</option>
<option value="3">Tamil Nadu</option>
<option value="4">Maharashtra</option>
<option value="5">West Bengal</option>
</select>
first true
<select>
<option value="1" selected>Vijayawada</option>
<option value="2">Kanyakumari</option>
<option value="3">Rewalsar</option>
<option value="4">Dehradun</option>
<option value="2">24 parganas</option>
</select>
There are many <input type="text" value="" placeholder="Pin code" data-subline="For example: <em>700049</em>"/>
There are <input type="email" value="" placeholder="Email ID" data-subline="For example: <em>mac#gmail.com</em>"/>
Phone <input type="number" value="" placeholder="Phone Number" data-subline="For example: <em>000-123-000</em>"/>
Mobile number <input type="number" value="" placeholder="Mobile Number" data-subline="For example: <em>000-123-000</em>"/>
My information may be shared with the
<select>
<option value="1" selected>Yes</option>
<option value="2">No</option>
</select>
<div class="clearfix"></div>
<div class="nl-submit-wrap" style="float: right;">
<button class="nl-submit" name="submit" id="btnSubmit" type="submit">Agree & Submit</button>
</div>
<div class="clearfix"></div>
<div class="nl-overlay"></div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block extrajs %}
<script src="{{theme.link}}/js/nlform.js"></script>
<script>
var nlform = new NLForm( document.getElementById( 'nl-form' ) );
</script>
<script>
var emargency = new NLForm( document.getElementById( 'emargency' ) );
</script>
{% endblock %}
</div>
</body>
</html>
{% endblock %}
twig is not like blade, you can't write php code inside.
However, you can write your own twig extensions:
https://symfony.com/doc/current/templating/twig_extension.html
please be sure to separate your twig extension from your logic (send mail), add this functionality on a service and bridge both (service & twig extension).

Assign a number of people for an appointment in PHP

I'm currently doing an appointment scheduling system.
So, I have time slot of 15 minutes for each hour and, for 9:00am, 10:00am, 11:00am, 2:00pm, 3:00pm, 4:00pm, I will only put 1 patient for these time slot. Then for the rest time slot it will be 2 patients.
How do I code this in PHP?
Below is the code I did for the form:
<form method="POST" action="MAppointment.php">
Patient Name: <input type="text" name="name" class="form-control" required>
<br>
Phone Number: <input type="text" name="contact" class="form-control" required>
<br>
Appointment Date: <input type="date" name="date" class="form-control" onchange="AjaxFunctionForTime" required>
<br>
Appointment Time: <select name="time" class="btn1" id="time" required>
<option value="9:00">9:00am</option>
<option value="9:15">9:15am</option>
<option value="9:30">9:30am</option>
<option value="10:00">10:00am</option>
<option value="10:15">10:15am</option>
<option value="10:30">10:30am</option>
<option value="11:00">11:00am</option>
<option value="11:15">11:15am</option>
<option value="11:30">11:30am</option>
<option value="14:00">2:00pm</option>
<option value="14:15">2:15pm</option>
<option value="14:30">2:30pm</option>
<option value="15:00">3:00pm</option>
<option value="15:15">3:15pm</option>
<option value="15:30">3:30pm</option>
<option value="16:00">4:00pm</option>
<option value="16:15">4:15pm</option>
<option value="16:30">4:30pm</option>
<br>
<input type="submit" value="Book the Appointment">
And this is the code in MAppointment.php
<?php
include('session.php');
$name=$_POST['name'];
$phoneNumber=$_POST['contact'];
$appointmentDate=$_POST['date'];
if(isset($_POST['time'])){
$appointmentTime=$_POST['time'];
mysqli_query($conn,"insert into appointment (name,contact,apptdate,appttime) values ('$name','$phoneNumber','$appointmentDate','$appointmentTime')");
$pid=mysqli_insert_id($conn);
}
?>
<script>
window.alert('Appointment booked successfully!');
window.history.back();
</script>
<?php
?>

I need to keep the values entered by a user after refreshing the webpage

I have created a form entry web page to store the data entered to a local database . The only problem is that after the submit button is clicked on the form it goes to another file named "info1.php" to store the data in the local database , in that file it also validates if the entered data is valid for example the email . If the data is invalid it refreshes the page to the form entry site or web page "home.php"
It would be much better the same user does not have to enter the whole thing again from the start , But instead the same value previously entered still resides in the web page (ie) form Thanks in Advance guys :-)
The form code is :
<?php
session_start();
?>
<head>
<php
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Data Entry Form</title>
<link rel="stylesheet" href="css/normalize.css">
<link href='http://fonts.googleapis.com/css?family=Nunito:400,300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<form action="info1.php" method="post">
<h1>DATA ENTRY</h1>
<fieldset>
<label for="name">Name:</label>
<input type="text" id="name" name="user_name" required/>
<label for="rollnumber">Roll number</label>
<input type="text" id="rollnum" name="roll_number" required/>
<label for="batch">Batch</label>
<select id="batch" name="user_batch" required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="2013-2017">2013-2017</option>
<option value="2014-2018">2014-2018</option>
<option value="2015-2019">2015-2019</option>
<option value="2016-2020">2016-2020</option>
</select>
</fieldset>
<label for="from">Came from</label>
<select id="from" name="user_from" required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="+2">+2</option>
<option value="Diploma">Diploma</option>
</select>
<label for="year">Year</label>
<select id="year" name="user_year" required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<label for="level">Level</label>
<select id="level" name="user_level"required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="UG">UG</option>
<option value="PG">PG</option>
</select>
<label for="email">Email:</label>
<input type="text" id="email_id" name="email_id" required/>
<label for="name">Father`s Name:</label>
<input type="text" id="fname" name="F_name" required/>
<label for="name">Mother`s Name:</label>
<input type="text" id="mname" name="m_name" required/>
<label for="name">Guardian`s Name:</label>
<input type="text" id="gname" name="g_name" >
<label for="mob_number">Students Mobile Number</label>
<input type="number" id="mob_number" name="mob_number"required/>
</select>
<label for="addressline1">Address Line 1</label>
<input type="text" id="addressline1" name="addressline1" required/>
</select>
<label for="addressline2">Address Line2</label>
<input type="text" id="addressline2" name="addressline2" required/>
</select>
<label for="city">City</label>
<input type="text" id="city" name="city" required/>
<label for="pmobnumber">Parents Mobile Number</label>
<input type="number" id="pmob_num" name="pmob_number" required/>
<label for="landnumber">LandLine Number or Parents Number</label>
<input type="number" id="landnum" name="land_number"required/>
<label for="cutoff">+2 cutoff</label>
<input type="number" id="cutoff" name="cutoff">
<label for="dept">Department</label>
<select id="dept" name="dept" required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="CSE">CSE</option>
<option value="IT">IT</option>
<option value="ECE">ECE</option>
<option value="EEE">EEE</option>
<option value="MECH">MECH</option>
<option value="CIVIL">CIVIL</option>
</select>
<label for="medium">Medium of Instruction</label>
<select id="medium" name="medium"required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="ENGLISH">English</option>
<option value="TAMIL">Tamil</option>
</select>
<label for="locality">Locality</label>
<select id="locality" name="locality"required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="RURAL">Rural</option>
<option value="URBAN">Urban</option>
</select>
<label for="intrest">Intrested In</label>
<select id="intrest" name="intrest"required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="PLACEMENTS">Placements</option>
<option value="HIGHER STUDIES">Higher Studies</option>
<option value="Entrepreneurship">Entrepreneurship</option>
</select>
<label for="scholar">Scholarship</label>
<select id="scholar" name="scholar"required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="GOVERNMENT">Government</option>
<option value="PRIVATE">Private</option>
</select>
<label for="income">Parents Annual Income</label>
<select id="income" name="income" required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="Less than 50,000">Less than 50,000</option>
<option value="Less than 1,00,000">Less than 1,00,000</option>
<option value="Less than 2,00,000">Less than 2,00,000</option>
<option value="Less than 5,00,000">Less than 5,00,000</option>
<option value="Less than 10,00,000">Less than 10,00,000</option>
<option value="OTHERS">others</option>
</select>
<label for="year">Caste</label>
<select id="caste" name="user_caste" required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="OC">OC</option>
<option value="BC">BC</option>
<option value="MBC">MBC</option>
<option value="BCM">BCM</option>
<option value="SC">SC</option>
<option value="ST">ST</option>
</select>
<label for="admit">Admitted through</label>
<select id="admit" name="user_admit"required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="Government Quota">Government Quota</option>
<option value="Manegement Quota">Manegement Quota</option>
</select>
<label for="stay">Staying In</label>
<select id="stay" name="user_stay"required/>
<option value="NoneSelected">---Click to Select---</option>
<option value="With parents">With parents</option>
<option value="College Hostel">College Hostel</option>
<option value="Outside Room">Outside Room</option>
</select>
</fieldset>
<button type="submit" onclick="phonenumber(pmob_number)">ENTER DATA</button>
</form>
<script src="js/mob_valid.js"></script>
</body>
The code for the file info1.php is:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = mysqli_connect ($servername , $username , $password) or die("unable to connect to host");
$sql = mysqli_select_db ($conn,'vcet') or die("unable to connect to database");
//code to insert into db...
$user_name=$_POST['user_name'];
$roll_number=$_POST['roll_number'];
$user_batch=$_POST['user_batch'];
$user_from=$_POST['user_from'];
$user_year=$_POST['user_year'];
$user_level=$_POST['user_level'];
$email_id=$_POST['email_id'];
$F_name=$_POST['F_name'];
$m_name=$_POST['m_name'];
$g_name=$_POST['g_name'];
$mob_number=$_POST['mob_number'];
$addressline1=$_POST['addressline1'];
$addressline2=$_POST['addressline2'];
$city=$_POST['city'];
$pmob_number=$_POST['pmob_number'];
$land_number=$_POST['land_number'];
$cutoff=$_POST['cutoff'];
$dept=$_POST['dept'];
$medium = $_POST['medium'];
$locality=$_POST['locality'];
$intrest=$_POST['intrest'];
$scholar=$_POST['scholar'];
$income=$_POST['income'];
$user_caste=$_POST['user_caste'];
$user_admit=$_POST['user_admit'];
$user_stay=$_POST['user_stay'];
if (!filter_var($email_id, FILTER_VALIDATE_EMAIL)) {
echo '<script language="javascript">';
echo 'alert("Sorry...You entered an invalid email id..!!!")';
echo '</script>';
//mysqli_query("delete from studentinfo where roll_number='$roll_number'");
header( "refresh:1;url=http://localhost/DB/home.php" );
}else
{
//executes the rest of the code
$mob_number1=$mob_number;
if(!preg_match('/^\d{10}$/',$mob_number1)) // phone number is valid
{
echo '<script language="javascript">';
echo 'alert("Sorry...You entered a wrong Mobile number..!!!")';
echo '</script>';
//mysqli_query("delete from studentinfo where roll_number='$roll_number'");
header( "refresh:1;url=http://localhost/DB/home.php" );
}
else // phone number is valid
{
$msg="";
$num_length = strlen((string)$pmob_number);
if($num_length!=10){
echo '<script language="javascript">';
echo 'alert("Sorry...You entered a wrong Mobile number..!!!")';
echo '</script>';
//mysqli_query("delete from studentinfo where roll_number='$roll_number'");
header( "refresh:1;url=http://localhost/DB/home.php" );
}else
{
$user_info = "INSERT INTO studentinfo"." (user_name,roll_number,user_batch,user_from,user_year,user_level,email_id,F_name,m_name,g_name,mob_number,addressline1,addressline2,city,pmob_number,land_number,cutoff,dept,medium,locality,intrest,scholar,income,user_caste,user_admit, user_stay)". "VALUES ('$user_name', '$roll_number','$user_batch','$user_from','$user_year','$user_level','$email_id','$F_name','$m_name','$g_name','".$mob_number."','$addressline1','$addressline2','$city','$pmob_number','$land_number','$cutoff','$dept','$medium','$locality','$intrest','$scholar','$income','$user_caste','$user_admit','$user_stay')";
if (!mysqli_query($conn,$user_info)) { die('Error: ' . mysqli_error($conn)); } echo '<script language="javascript">';
echo 'alert("Awesome!! your INFORMATION was added to the DATABASE")';
echo '</script>';
header( "refresh:1;url=http://localhost/DB/home.php" );
mysqli_close($conn);
}
}
}
?>
As RiggsFolly said in the comments, you could use the SESSION to keep the values, to use a SESSION variable, you have to open the session at the beginning of each file where you'll use it.
Basically, on your form page : ( you should ofc check if the $_SESSION['input_value'] var exists )
<?php session_start();
if(!isset($_SESSION['input_value']) {
$_SESSION['input_value'] = "Default value for this input";
}
?>
some code ...
<input value='<?php echo $_SESSION['input_value']; ?>'>
on your form treatment page :
<?php session_start();
some code ...
$_SESSION['input_value'] = $_POST['input_value']; // ( or $_GET['input_value'] )
?>

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