Cannot insert data in the database using option(textarea) - php

I got this code but it is not inserting the content of the option (textarea) into the database.
connection.php
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "copy";
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$db);
?>
submit.php
<?php
include 'connection.php';
$foodA = $_POST['foodA'];
$foodB = $_POST['foodB'];
$foodC = $_POST['foodC'];
$foodD = $_POST['foodD'];
$foodE = $_POST['foodE'];
if(!$_POST['submit']) {
echo "please fill out the form";
header('Location: select.html');
}
else {
$sql = "INSERT INTO remove(foodA, foodB, foodC, foodD, foodE) VALUES (?,?,?,?,?);";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt,"sssss",$foodA,$foodB,$foodC,$foodD,$foodE);
mysqli_stmt_execute($stmt);
echo "User has been added!";
header('Location: select.html');
}
select.html
<html lang="en">
<title>Catering Service</title>
<meta charset="utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="js/js.1.js" type="text/javascript"></script>
</head>
<body>
<form action="submit.php" method="post">
<select multiple="multiple" class="options" id="textarea" >
<option value="foodA">foodA</option>
<option value="foodB">foodB</option>
<option value="foodC">foodC</option>
<option value="foodD">foodD</option>
<option value="foodE">foodE</option>
</select>
<button type="button" id="copy" onclick="yourFunction()">Copy</button>
<button type="button" id="remove" onclick="yourFunction()">Remove</button>
<select id="textarea2" multiple class="remove" >
<input type="submit" name="submit" />
</form>
</select>
</html>

You need to name the <select> so you can use the data.
name="food[]"
Like this
<select multiple="multiple" name="food[]" class="options" id="text area" >
<option value="foodA">foodA</option>
<option value="foodB">foodB</option>
<option value="foodC">foodC</option>
<option value="foodD">foodD</option>
<option value="foodE">foodE</option>
</select>
Then if you want the value to be 0 or 1, depending on selected or not, you can use the following to replace this:
$foodA = $_POST['foodA'];
$foodB = $_POST['foodB'];
$foodC = $_POST['foodC'];
$foodD = $_POST['foodD'];
$foodE = $_POST['foodE'];
to
$foodA = 0;
$foodB = 0;
$foodC = 0;
$foodD = 0;
$foodE = 0;
foreach ($_POST['food'] as $value) {
if($value == 'foodA')
$foodA = 1;
if($value == 'foodB')
$foodB = 1;
if($value == 'foodC')
$foodC = 1;
if($value == 'foodD')
$foodD = 1;
if($value == 'foodE')
$foodE = 1;
}

You need to name your select with name=food or something so it will be in $_POST['food']. Each option in the select does not show up in $_POST, only what is selected will be in the name of the select. Each option is not it's own thing.
<select multiple="multiple" name="food" class="options" id="textarea" >
<option value="foodA">foodA</option>
<option value="foodB">foodB</option>
<option value="foodC">foodC</option>
<option value="foodD">foodD</option>
<option value="foodE">foodE</option>
</select>
When you save the data it will have:
$_POST['food'] with the value of 'foodA' if multiples, it will be 'foodA, foodB'

Related

for loop over selection option value HTML with PHP

Hi I work like loop through the selection option values with PHP instead of duplicating it, from value 500 to 510. Can you kindly show me how to do it?
<body>
<div class="form-group">
<br> <label>Job Description</label>: <b><?php echo $row['job_desc']; ?><br></b>
<select class="form-control" name="job_code">
<option value="500">System Analysis</option>
<option value="501">Programmer</option>
<option value="502">Database Designer</option>
<option value="503">Electrical Engineer</option>
<option value="504">Mechanical Engineer</option>
<option value="505">Civil Engineer</option>
<option value="506">Clerical Support</option>
<option value="507">DSS Analyst</option>
<option value="508">Application Designer</option>
<option value="509">Bio Technician</option>
<option value="510">General Support</option>
</select>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" name="update" value="Update Data">Save</button>
</div>
</body>
Employee BDJob DB
<?php
$connection = mysqli_connect("localhost", "root", "");
$db = mysqli_select_db($connection, 'amaz');
if (isset($_POST['update'])) {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$sex = $_POST['sex'];
$emp_salary = $_POST['emp_salary'];
$dept_name = $_POST['dept_name'];
$job_code = $_POST['job_code'];
$query = "UPDATE employee
SET employee.first_name='$_POST[first_name]',employee.last_name='$_POST[last_name]',employee.sex='$_POST[sex]',employee.emp_salary='$_POST[emp_salary]',employee.dept_id='$_POST[dept_name]',employee.job_code='$_POST[job_code]'
WHERE employee.emp_num='$_POST[id]'";
$query_run = mysqli_query($connection, $query);
}
?>
You make a SELECT query that looks at the jobs table:
$server = 'localhost';
$user = 'root';
$pass = '';
$db = 'amaz';
$mysql = new mysqli($server, $user, $pass, $db);
if ($mysql->connect_error !== null) {
printf("Connect failed: %s\n", $mysql->connect_error);
exit;
}
$select = $mysql->query("SELECT * FROM jobs WHERE job_code > 409 and job_code < 511");
$jobs = [];
while ($row = $select->fetch_array(MYSQLI_ASSOC)) {
$jobs[] = $row;
}
?>
<select class="form-control" name="job_code">
<?php foreach ($jobs as $job): ?>
<option value="<?=$job['job_code']?>"><?=$job['job_desc']?></option>
<?php endforeach; ?>
</select>
This will output:
<select class="form-control" name="job_code">
<option value="500">System Analysis</option>
<option value="501">Programmer</option>
<option value="502">Database Designer</option>
...etc
</select>

Validation for multiselect using PHP

I have a multiple select on my code, what I need to do is to have a validation when the user click the submit button without selecting any of the given list.
Here's the sample code:
index.php
<?php
IF(isset($_POST['Rtype'])){
$RetrieveType = $_POST['Rtype'];
IF($RetrieveType == 'date_range'){
$start_date = new DateTime($_POST['start_date']);
$sd = date_format($start_date,'Y-m-d');
$p_week=array();
$m_month=01;
$end_date = new DateTime($_POST['end_date']);
$end_date-> add(new DateInterval('P1D'));
$ed =date_format($end_date,'Y-m-d');
}
ELSEIF($RetrieveType == 'weekly'){
$p_week = $_POST['week'];
$m_week = implode(',',$p_week);
$m_month =$_POST['month_m'];
$m_year =$_POST['year_m'];
$p_lob = $_POST['lob'];
$sc_lob=implode(',',$p_lob);
} ELSE
{
$RetrieveType = 'date_range';
$sd = date('Y-m-01');
$start_date = date('Y-m-01');
$ed = date('Y-m-d');
$end_date = date('Y-m-d');
$m_month=date ('m');
$p_week=array();
$m_year= date ('Y');
$sc_lob='';
}
?>
<html>
<head>
<head>
<body>
<form action="index.php" method="POST" class="form-inline">
<label for="sel1">Week:</label>
<select data-placeholder="<?php echo (isset($_POST['week']) ? "Week/s Currently Selected: ".implode(",",$_POST['week']) : "Select Week"); ?>"
class="chosen-select" multiple tabindex="4" style="width:350px;" name= "week[]">
<option value="1" id="empty">Week 1</option>
<option value="2" id="empty">Week 2</option>
<option value="3" id="empty">Week 3</option>
<option value="4" id="empty">Week 4</option>
<option value="5" id="empty">Week 5</option>
</select>
<label for="sel1">LOB:</label>
<select data-placeholder="<?php echo (isset($_POST['lob']) ? "LOB/s Currently Selected: ".implode(",",$_POST['lob']) : "Select LOB"); ?>"
class="chosen-select" multiple tabindex="4" style="width:350px;" name= "lob[]">
<?php
$lob = array();
$q = "SELECT distinct LOB from roster where EmployStatus='active' and SDLead <> '' group by LOB";
$params = array();
$query = sqlsrv_query($conn, $q);
while($rowsss= sqlsrv_fetch_array($query)) {
$lob = $rowsss['LOB'];
echo "<option value='".$lob."'>".$lob."</option>";
}
for ($i = 0; $i <= count($lob) - 1; $i++) {
IF($sc_lob == $lob[$i])
{$isSelected = 'selected';}
elseif(1==1)
{$isSelected='';}
echo "<option ".$isSelected." value='".$lob[$i]."'>".$lob[$i]."</option>"; }
?>
<input type="hidden" name="Rtype" value="weekly">
<input class="btn btn-primary" type="submit" />
</form>
What I wanted to achieve is when the user clicked the submit button without selecting any on the "Week" list or "LOB" list, there would be an error message that says "This field is required, select at least one.". Actually I already did this before, when I'm just using a checkbox. I placed the code below after the label tag:
sample code:
<?php
if(isset($_GET['err']))
{
$err = $_GET['err'];
if($err == 1)
{
echo "<span class='text-danger'>* This field is required, select at least one.</span>";
}
else
{
echo "";
}
}
else
{
echo "";
}
?>
Try this
if(isset($_POST['submit']){
errors = 0;
if($_POST['nameOfSelect'] == ""){
errors++;
}
if(errors == 0){
return true;
}else{
return false;
}

How to make a list filled from database visible in php?

I am taking the values of the course and the semester,and searching in database for the instructors who teach the course at that semester,It worked. But I want the values to be shown in a list after submitting it, I want the list to appear only after submitting.Then I want to choose from the list an instructor and add him in database with the course and the semester selected first,without re selecting them.Please help..
<?php
session_start();
$_SESSION['courses'] = $_SESSION['semester'] = $coursechosen = $semesterchosen = "";
if ($_SERVER["REQUEST_METHOD"] == "GET") {
error_reporting(E_ALL ^ E_NOTICE);
require_once 'connect.php';
$semester = "";
$courses = $_GET['courses'];
$sem = $_GET['sem'];
$semyr = $_GET['semyr'];
$semester = $sem . $semyr;
if (isset($_GET['courses']) && isset($_GET['sem']) && isset($_GET['semyr'])) {
$_SESSION['courses'] = $courses;
$_SESSION['semester'] = $semester;
$instructors = mysql_query("select Instructor_Id from teach where
Semester='$semester' AND course_code='$courses' ");
if (!$instructors) {
die("Database access failed: " . mysql_error());
}
$row = mysql_num_rows($instructors);
}
}
?>
<?php
$instructor = $_GET['instructor'];
if (isset($_SESSION['courses']) && isset($_SESSION['semester'])) {
$coursechosen = $_SESSION['courses'];
$semesterchosen = $_SESSION['semester'];
$query = "Insert INTO coordinators(instructor_id,course_code,semester)
VALUES ('$instructor','$coursechosen','$semesterchosen')";
// $query="Insert INTO instructors(Fname)VALUES('$instructor')";
mysql_query($query);
}
?>
<form method="get" action="Chairman.php" onsubmit="return
validate()">
<select name="courses" id="courses" >
<option value="courses"><--Courses--></option>
<option value="PHYS220">Physics for Engineers</option>
<option value="MATH210">Calculus II</option>
<option value="MATH225">Linear Algebra with Applications</option>
</select>
<select name="sem" id="sem" >
<option selected="selected">Season</option>
<option value="Fall">Fall</option>
<option value="Spring">Spring</option>
<option value="Summer">Summer</option>
</select>
<select name="semyr" id="semyr" >
<option>Year</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<div> <input type="submit" value="Submit" ></input></div>
<div>
<select name="instructor">
<?php
if ($row !== 0) {
for ($i = 0; $i < $row; ++$i) {
$instructor_id = mysql_fetch_array($instructors);
$instructorsname = mysql_query("select * from instructors where
Id='$instructor_id[0]'");
$rowname = mysql_num_rows($instructorsname);
$instructorsnames = mysql_fetch_array($instructorsname);
echo "<option
value='$instructorsnames[0]'>" . $instructorsnames['Fname'] . "
" . $instructorsnames['Lname'] . "</option>";
}
}
?>
</select>
</div>
</form>
</body>
</html>
mysql_fetch_array returns an array with your data. So, to show instructors, you have to iterate through your list and write them out. Try:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<option value='$row['id']'>".$row['Fname']."
".$row['Lname']."</option>";
}
That should give you a list of option. But I'm not quite sure if I understood what your question is. If that doesn't answer it, could you be more specific?

Filter data from mysql by countries

I want to filter my data by countries from a dropdown menu:
<form name="filter_form" method="POST" action="display_data.php">
Select a country:
<select name="value">
<option name="country" value="AU">Austria</option>
<option name="country" value="BE">Belgium</option>
<option name="country" value="BU">Bulgaria</option>
</select>
<input type="submit" name="btn_submit" value="Submit Filter" />
<?php
if($_POST['country'] == 'BE') {
$query = mysql_query("SELECT * FROM think WHERE Country='Belgium'");
}
elseif($_POST['country'] == 'AU') {
$query = mysql_query("SELECT * FROM think WHERE Country='Austria'");
} else {
$query = mysql_query("SELECT * FROM think");
}
?>
The code does not filter any data.
If anyone can help, thanks!
When you use select tag, the Server page will refer name of select tag and not option.
Change your code as below:
<select name="country">
<option value="AU">Austria</option>
<option value="BE">Belgium</option>
<option value="BU">Bulgaria</option>
</select>
Avoid writing redundant code.
Change your code with below code:
<form name="filter_form" method="POST" action="display_data.php">
Select a country:
<select name="country">
<option value="AU">Austria</option>
<option value="BE">Belgium</option>
<option value="BU">Bulgaria</option>
</select>
<input type="submit" name="btn_submit" value="Submit Filter" />
<?php
if(isset($_POST['country']))
{
switch($_POST['country'])
{
case 'BE' : $countryName = 'Belgium';
break;
case 'AU' : $countryName = 'Austria';
break;
default : $countryName = '';
break;
}
$where = '';
if($countryName != '')
{
$where = "WHERE Country='".$countryName."'";
}
$query = mysql_query("SELECT * FROM think ".$where."");
}
?>
if($_POST['country'] == 'BE') {
should be
if($_POST['value'] == 'BE') {
And so on for others !!
You need to change the location of the name attribute. You now have it on the option element but it needs to be on the select element.
<form name="filter_form" method="POST" action="display_data.php">
Select a country:
<select name="country">
<option value="AU">Austria</option>
<option value="BE">Belgium</option>
<option value="BU">Bulgaria</option>
</select>
<input type="submit" name="btn_submit" value="Submit Filter" />
<?php
if($_POST['country'] == 'BE') {
$query = mysql_query("SELECT * FROM think WHERE Country='Belgium'");
}
elseif($_POST['country'] == 'AU') {
$query = mysql_query("SELECT * FROM think WHERE Country='Austria'");
} else {
$query = mysql_query("SELECT * FROM think");
}
?>

PHP and HTML select tag

I have started with PHP and HTML and would like to execute simple code with three drop-down lists (select tag). But when I make selection from one, the other two selected values disappear. Should I save values when submitting them and then echo. If yes, how can I do it. Sample code will be appreciated.
<?php // formtest_2.php
$name_vid = $name_type = $name_model = "";
if (isset($_POST['vid']))$name_vid=($_POST['vid']);
else $name_vid="no";
if (isset($_POST['type']))$name_type=sanitizeString($_POST['type']);
else $name_type="no";
if (isset($_POST['model']))$name_model=sanitizeString($_POST['model']);
else $name_model="no";
echo <<<_END
<html>
<head>
<title>Form Test Two</title>
</head>
<body>
Make your selection:
<form action="formtest_2.php" method="post">
<select name="vid" size="1">
<option value="" selected>Product</option>
<option value = "apple">apple</option>
<option value = "cherry">cherry</option>
<option value = "orange">orange</option>
</select>
<input type="submit"/><br />
<select name="type" size="1">
<option value="" selected>Color</option>
<option value = "green">green</option>
<option value = "red">red</option>
<option value = "orange">orange</option>
</select>
<input type="submit"/><br />
<select name="model" size="1">
<option value="" selected>Model</option>
<option value = "1-a">1-a</option>
<option value = "2-b">2-b</option>
<option value = "3-c">3-c</option>
</select>
<input type="submit"/><br />
</form>
</body>
</html>
_END;
echo "Your selection is: $name_vid and $name_type and $name_model";
function sanitizeString($var)
{
$var = stripslashes($var);
$var = htmlentities($var);
$var = strip_tags($var);
return $var;
}
?>
I think you need to re-select the values of the drop downs after submission. The following code would be a fix.
<?php // formtest_2.php
$name_vid = $name_type = $name_model = "(nothing)";
if (isset($_POST['vid']) && $_POST['vid']) $name_vid=($_POST['vid']);
if (isset($_POST['type']) && $_POST['type']) $name_type=sanitizeString($_POST['type']);
if (isset($_POST['model']) && $_POST['model']) $name_model=sanitizeString($_POST['model']);
?>
<html>
<head>
<title>Form Test Two</title>
</head>
<body>
Make your selection:
<form action="formtest_2.php" method="post">
<select name="vid" size="1">
<option value="">Product</option>
<option value = "apple" <?php if($name_vid == 'apple') echo 'selected="selected"'; ?>>apple</option>
<option value = "cherry" <?php if($name_vid == 'cherry') echo 'selected="selected"'; ?>>cherry</option>
<option value = "orange" <?php if($name_vid == 'orange') echo 'selected="selected"'; ?>>orange</option>
</select>
<br />
<select name="type" size="1">
<option value="">Color</option>
<option value = "green" <?php if($name_type == 'green') echo 'selected="selected"'; ?>>green</option>
<option value = "red" <?php if($name_type == 'red') echo 'selected="selected"'; ?>>red</option>
<option value = "orange" <?php if($name_type == 'orange') echo 'selected="selected"'; ?>>orange</option>
</select>
<br />
<select name="model" size="1">
<option value="">Model</option>
<option value = "1-a" <?php if($name_model == '1-a') echo 'selected="selected"'; ?>>1-a</option>
<option value = "2-b" <?php if($name_model == '2-b') echo 'selected="selected"'; ?>>2-b</option>
<option value = "3-c" <?php if($name_model == '3-c') echo 'selected="selected"'; ?>>3-c</option>
</select>
<input type="submit"/><br />
</form>
</body>
</html>
<?php
echo "Your selection is: $name_vid and $name_type and $name_model";
function sanitizeString($var)
{
$var = stripslashes($var);
$var = htmlentities($var);
$var = strip_tags($var);
return $var;
}
?>
I would not use heredoc syntax ("<<<") to print HTML.
Try this out, i made one submit button, and re selected the values after submit :
<?php
// formtest_2.php
$name_vid = $name_type = $name_model = "";
if (isset($_POST['vid']))
$name_vid = ($_POST['vid']);
else
$name_vid = "no";
if (isset($_POST['type']))
$name_type = sanitizeString($_POST['type']);
else
$name_type = "no";
if (isset($_POST['model']))
$name_model = sanitizeString($_POST['model']);
else
$name_model = "no";
///////////////////
if($name_vid == 'apple')
{
$v1 = 'selected';
}
elseif($name_vid == 'cherry')
{
$v2 = 'selected';
}
elseif($name_vid == 'orange')
{
$v3='selected';
}
////////////////////////
if($name_type == 'green')
{
$t1 = 'selected';
}
elseif($name_type == 'red')
{
$t2 = 'selected';
}
elseif($name_type == 'orange')
{
$t3='selected';
}
///////////////////////
if($name_model == '1-a')
{
$m1 = 'selected';
}
elseif($name_model == '2-b')
{
$m2 = 'selected';
}
elseif($name_model == '3-c')
{
$m3='selected';
}
?>
<html>
<head>
<title>Form Test Two</title>
</head>
<body>
Make your selection:
<form name="fruite_form" action="formtest_2.php" method="post">
<select name="vid" size="1">
<option value="" selected>Product</option>
<option <?=$v1?> value = "apple">apple</option>
<option <?=$v2?> value = "cherry">cherry</option>
<option <?=$v3?> value = "orange">orange</option>
</select>
<select name="type" size="1">
<option value="" selected>Color</option>
<option <?=$t1?> value = "green">green</option>
<option <?=$t2?>value = "red">red</option>
<option <?=$t3?>value = "orange">orange</option>
</select>
<select name="model" size="1">
<option value="" selected>Model</option>
<option <?=$m1?> value = "1-a">1-a</option>
<option <?=$m2?>value = "2-b">2-b</option>
<option <?=$m3?>value = "3-c">3-c</option>
</select>
<input name="submitButtom" id="submitButton" type="submit"/><br />
</form>
</body>
</html>
<?
echo "Your selection is: $name_vid and $name_type and $name_model";
function sanitizeString($var)
{
$var = stripslashes($var);
$var = htmlentities($var);
$var = strip_tags($var);
return $var;
}
?>
Feel free top add back your submit buttons.
But give each a unique name and also don't forget to name your form, and as a good practice, give then an id too which will be the same as name attribute.
Update: Just to make sure you are really getting a post, since this is your initial question, print this at the end of your code
echo $_POST['vid'] . ' ' . $_POST['type'] . ' ' . $_POST['model'] . '<br/><br/>';
and see if post really holds the value after submission, if you get a value from post, but not from your assigned variables, then check your conditions used at the top to assign your values.
// you need to check if post isset and has an actual value, only than, the varibale should be assigned. Just like what Sithu said.

Categories