php, phpMyAdmin - dropdown values not inserting into database - php

I'm trying to add the values, that I pick from the dropdown menus (see HTML), to a database table.
Yet this is not working out like I planned.
I'm I doing something wrong?
At the moment the columns for the dropdown values are set as INT in the table of the database.
Code below:
HTML:
<head>
<meta charset ="UTF-8">
<title>El Gusto - Sigaren toevoegen</title>
</head>
<body>
<form action="handler.php" method="post">
<span class="tab"><b>Sensatie in de mond:</b><br>
<span class="tab"><span class="tab">Lip:
<select>
<option value="1" name="sensatie_lip_1">1</option>
<option value="2" name="sensatie_lip_2">2</option>
<option value="3" name="sensatie_lip_3">3</option>
<option value="4" name="sensatie_lip_4">4</option>
</select><br>
<span class="tab"><span class="tab">Tong:
<select>
<option value="1" name="sensatie_tong_1">1</option>
<option value="2" name="sensatie_tong_2">2</option>
<option value="3" name="sensatie_tong_3">3</option>
<option value="4" name="sensatie_tong_4">4</option>
</select><br>
<span class="tab"><span class="tab">Wangen:
<select>
<option value="1" name="sensatie_wangen_1">1</option>
<option value="2" name="sensatie_wangen_2">2</option>
<option value="3" name="sensatie_wangen_3">3</option>
<option value="4" name="sensatie_wangen_4">4</option>
</select><br>
<span class="tab"><span class="tab">Keel:
<select>
<option value="1" name="sensatie_keel_1">1</option>
<option value="2" name="sensatie_keel_2">2</option>
<option value="3" name="sensatie_keel_3">3</option>
<option value="4" name="sensatie_keel_4">4</option>
</select><br>
</span><br>
<b>PRESENTATIE (SIGARENBAND, DOOS, ...)</b>
<select>
<option value="1" name="presentatie_1">1</option>
<option value="2" name="presentatie_2">2</option>
<option value="3" name="presentatie_3">3</option>
<option value="4" name="presentatie_4">4</option>
<option value="5" name="presentatie_5">5</option>
</select><br>
<br>
<b>KWOTERING</b>
<select>
<option value="1" name="kwotering_1">1</option>
<option value="2" name="kwotering_2">2</option>
<option value="3" name="kwotering_3">3</option>
<option value="4" name="kwotering_4">4</option>
<option value="5" name="kwotering_5">5</option>
</select><br>
<br>
<input type='submit' name="Toevoegen" value='Toevoegen'>
</form>
</body>
PHP:
//WARM - SENSATIE IN DE MOND (DROPDOWN)
//LIP
$sensatie_lip_1 = isset($_POST['sensatie_lip_1']);
$sensatie_lip_2 = isset($_POST['sensatie_lip_2']);
$sensatie_lip_3 = isset($_POST['sensatie_lip_3']);
$sensatie_lip_4 = isset($_POST['sensatie_lip_4']);
//TONG:
$sensatie_tong_1 = $_POST['sensatie_tong_1'];
$sensatie_tong_2 = $_POST['sensatie_tong_2'];
$sensatie_tong_3 = $_POST['sensatie_tong_3'];
$sensatie_tong_4 = $_POST['sensatie_tong_4'];
//WANGEN:
$sensatie_wangen_1 = $_POST['sensatie_wangen_1'];
$sensatie_wangen_2 = $_POST['sensatie_wangen_2'];
$sensatie_wangen_3 = $_POST['sensatie_wangen_3'];
$sensatie_wangen_4 = $_POST['sensatie_wangen_4'];
//KEEL:
$sensatie_keel_1 = $_POST['sensatie_keel_1'];
$sensatie_keel_2 = $_POST['sensatie_keel_2'];
$sensatie_keel_3 = $_POST['sensatie_keel_3'];
$sensatie_keel_4 = $_POST['sensatie_keel_4'];
//PRESENATIE (DROPDOWN)
$presentatie_1 = $_POST['presentatie_1'];
$presentatie_2 = $_POST['presentatie_2'];
$presentatie_3 = $_POST['presentatie_3'];
$presentatie_4 = $_POST['presentatie_4'];
$presentatie_5 = $_POST['presentatie_5'];
//KWOTERING (DROPDOWN)
$kwotering_1 = $_POST['kwotering_1'];
$kwotering_2 = $_POST['kwotering_2'];
$kwotering_3 = $_POST['kwotering_3'];
$kwotering_4 = $_POST['kwotering_4'];
$kwotering_5 = $_POST['kwotering_5'];
//DATABASE
$connect = mysql_connect("localhost", "root", "") or die ("Couldn't connect to the database!");
mysql_select_db("el_gusto") or die ("Couldn't find db!");
$query = mysql_query("INSERT INTO sigaren(sensatie_lip_1, sensatie_lip_2, sensatie_lip_3, sensatie_lip_4, sensatie_tong_1, sensatie_tong_2, sensatie_tong_3, sensatie_tong_4, sensatie_wangen_1, sensatie_wangen_2, sensatie_wangen_3, sensatie_wangen_4, sensatie_keel_1, sensatie_keel_2, sensatie_keel_3, sensatie_keel_4, presentatie_1, presentatie_2, presentatie_3, presentatie_4, presentatie_5, kwotering_1, kwotering_2, kwotering_3, kwotering_4, kwotering_5) VALUES
('$sensatie_lip_1',
'$sensatie_lip_2',
'$sensatie_lip_3',
'$sensatie_lip_4',
'$sensatie_tong_1',
'$sensatie_tong_2',
'$sensatie_tong_3',
'$sensatie_tong_1',
'$sensatie_wangen_1',
'$sensatie_wangen_2',
'$sensatie_wangen_3',
'$sensatie_wangen_4',
'$sensatie_keel_1',
'$sensatie_keel_2',
'$sensatie_keel_3',
'$sensatie_keel_4',
'$presentatie_1',
'$presentatie_2',
'$presentatie_3',
'$presentatie_4',
'$presentatie_5',
'$kwotering_1',
'$kwotering_2',
'$kwotering_3',
'$kwotering_4',
'$kwotering_5')");
if($query) {
echo "Toevoeging gelukt!";
}
else{
echo mysql_error();
}
?>

HTML:
<select name="sensatie_lip">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
PHP:
$sensatie_lip = $_POST['sensatie_lip']; // you will get your selected value

instead of giving name to option tag give name to select tag.
Currently you are not getting the values of dropdown after submitting the form

Rightly said by Disha and Jasbir,Select way used is incorrect and try changing it as suggested and see that to work
<select name="sensatie" >
<option value="1">sensatie_wangen_1</option>
<option value="2">sensatie_wangen_2</option>
</select>
So it will display 2 drop down as sesatie_wangen_1, 2 and on save db will have value 1, or 2. It is a hint and you need to adjust code to make it work based on this

Related

how to show gender using simple html dom parser in php?

i'm unable for success in how to show gender and state from below coading
please solve this.
$gender = $html->getElementById("cpBody_rbtnListGender")->getAttribute("value");
$state = $html->getElementById("cpBody_ddlState")->getAttribute("value");
this is for gender
<span id="cpBody_rbtnListGender" class="form-control pull-left radio-input"><input id="cpBody_rbtnListGender_0" type="radio" name="ctl00$cpBody$rbtnListGender" value="MALE" checked="checked" tabindex="3"><label for="cpBody_rbtnListGender_0">Male</label><input id="cpBody_rbtnListGender_1" type="radio" name="ctl00$cpBody$rbtnListGender" value="FEMALE" tabindex="3"><label for="cpBody_rbtnListGender_1">Female</label><input id="cpBody_rbtnListGender_2" type="radio" name="ctl00$cpBody$rbtnListGender" value="TRANSGENDER" tabindex="3"><label for="cpBody_rbtnListGender_2">Transgender</label><input id="cpBody_rbtnListGender_3" type="radio" name="ctl00$cpBody$rbtnListGender" value="OTHER" tabindex="3"><label for="cpBody_rbtnListGender_3">Other</label></span>
and this is for state
<div class="input-group">
<label class="control-label label-fixed">State</label>
<select name="ctl00$cpBody$ddlState" id="cpBody_ddlState" tabindex="15" class="form-control">
<option value="0">--SELECT STATE--</option>
<option value="1">ANDAMAN & NICOBAR ISLANDS</option>
<option value="2">ANDHRA PRADESH</option>
<option value="3">ARUNACHAL PRADESH</option>
<option value="4">ASSAM</option>
<option value="5">BIHAR</option>
<option value="6">CHANDIGARH</option>
<option value="7">CHHATTISGARH</option>
<option value="8">DADRA & NAGAR HAVELI</option>
<option value="9">DAMAN & DIU</option>
<option value="10">DELHI</option>
<option value="11">GOA</option>
<option value="12">GUJARAT</option>
<option value="13">HARYANA</option>
<option value="14">HIMACHAL PRADESH</option>
<option value="15">JAMMU AND KASHMIR</option>
<option value="16">JHARKHAND</option>
<option value="17">KARNATAKA</option>
<option value="18">KERALA</option>
<option value="19">LAKSHADWEEP</option>
<option value="20">MADHYA PRADESH</option>
<option value="21">MAHARASHTRA</option>
<option value="22">MANIPUR</option>
<option value="23">MEGHALAYA</option>
<option value="24">MIZORAM</option>
<option value="25">NAGALAND</option>
<option value="26">ORISSA</option>
<option value="27">PONDICHERRY</option>
<option value="28">PUNJAB</option>
<option selected="selected" value="29">RAJASTHAN</option>
<option value="30">SIKKIM</option>
<option value="31">TAMIL NADU</option>
<option value="32">TELANGANA</option>
<option value="33">TRIPURA</option>
<option value="34">UTTAR PRADESH</option>
<option value="35">UTTARAKHAND</option>
<option value="36">WEST BENGAL</option>
</select>
</div>
state and gender is not showing using getAttribute("value");
You could make use of find.
One option to get the state could be to use the id and get the selected option #cpBody_ddlState option[selected]
$state = $html->find('#cpBody_ddlState option[selected]', 0)->plaintext;
echo $state; //RAJASTHAN
To get the selected radio button you might use #cpBody_rbtnListGender input and loop though the items until the attribute checked matches:
foreach ($html->find('#cpBody_rbtnListGender input') as $input) {
if ($input->hasAttribute("checked")) {
echo $input->getAttribute("value"); // MALE
}
}
idk why you're doing this with php.'
in javascript u can create a p to pass the value to it:
<body onload="pageloaded()">
<p id="display"></p>
in javascript:
var p = document.getElementById("display");
var value = document.getElementById("cpBody_rbtnListGender").value;
function pageloaded(){
p.innerHTML = value;
}
u can do the same for ur other needs

How to display the multiple data on sumoselect dropdown from the database?

I am using sumoselect (http://hemantnegi.github.io/jquery.sumoselect/sumoselect_demo.html) and CodeIgniter. I am displaying data from the database. I am inserting product type multiple data in the database like
producttype
1
1+2
1+2+3
I am displaying like this on HTML page
<select name="producttype[]" class="form-control multipleselect event_stop" id="producttype" multiple="multiple">
<option value="" selected disabled>Select</option>
<option value="1" <?php if($post->producttype =="1") echo 'selected'; ?>>One</option>
<option value="2" <?php if($post->producttype =="2") echo 'selected'; ?>>Two</option>
<option value="3" <?php if($post->producttype =="3") echo 'selected'; ?>>Three</option>
<option value="4" <?php if($post->producttype =="4") echo 'selected'; ?>>Four</option>
</select>
If I am getting 1 from the database then select dropdown displaying One but sometimes I am getting 1+2 or 1+2+3 then how can I show in the select dropdown?
Would you help me out in this?
I don't have any knowledge of sumoselect but it may help you to show selected multiple options as
$producttype_array = explode("+","1+2+3");
<select name="producttype[]" class="form-control multipleselect event_stop" id="producttype" multiple="multiple">
<option value="1" <?php if(in_array(1,$producttype_array)) echo 'selected'; ?>>One</option>
<option value="2" <?php if(in_array(2,$producttype_array)) echo 'selected'; ?>>Two</option>
<option value="3" <?php if(in_array(3,$producttype_array)) echo 'selected'; ?>>Three</option>
<option value="4" <?php if(in_array(4,$producttype_array)) echo 'selected'; ?>>Four</option>
</select>

Echo text based off of dynamic dropdown

I am using Magento and using Custom Options. I have 5 custom options that allow the user to choose the condition of the item. I want some text below to show what the condition means when they select it.
Im sorry for the confusion. I want this to update live on the screen when the appropriate option is selected.
Here is my current code but it isn't displaying the text.
<select name="options[1][]" id="select_1" class="multiselect required-entry product-custom-option" title="" onchange="opConfig.reloadPrice()">
<option value="1" price="0" >Perfect </option>
<option value="2" price="-35" >Excellent </option>
<option value="3" price="-105" >Good </option>
<option value="4" price="-140" >Poor </option>
<option value="5" price="-252" >Broken </option></select>
<?php if( $_POST['select_1']=='1' ){
echo "Perfect Condition Text";
}elseif( $_POST['select_1']=='2' ){
echo "Excellent Condition Text";
}elseif( $_POST['select_1']=='3' ){
echo "Good Condition Text";
}elseif( $_POST['select_1']=='4' ){
echo "Poor Condition Text";
}elseif( $_POST['select_1']=='5' ){
echo "Broken Condition Text";
} ?>
Changing the <select> name to select_1 will solve your problem:
<select name="select_1" id="select_1" class="multiselect required-entry product-custom-option" title="" onchange="opConfig.reloadPrice()">
If you can't change the names for some reason, change $_POST['select_1'] to $_POST['options'][1][0]
If you want to display the value of the <select> without refreshing the page then you could use javascript or jquery. Here is a sample of Javascript:
<script language="javascript">
function displayCondition() {
condition = new Array("", "Perfect", "Excellent", "Good", "Poor", "Broken");
var getsel = document.getElementById('select_1').value;
document.getElementById("divId").innerHTML = condition[getsel];
}
</script>
</head>
<body>
<form name="formName">
<select name="options[1][]" id="select_1" class="multiselect required-entry product-custom-option" title="" onchange="displayCondition()">
<option value="0">Select Condition</option>
<option value="1" price="0" >Perfect</option>
<option value="2" price="-35" >Excellent</option>
<option value="3" price="-105" >Good</option>
<option value="4" price="-140" >Poor</option>
<option value="5" price="-252" >Broken</option>
</select>
<div id="divId" name="divName" ></div>
</form>
</body>
</html>
The array will not be available in PHP $_POST by select_1 index but PHP $_POST is going to have an options array $_POST['options']
The post array name is based on the name of the form element, not the id of the form element. Change the name property of your select element to "select_1" and you will get what you are looking for.

How can I stop an empty SQL query from displaying all results?

I am creating a meat packaging search form, users can search for different packages using multiple forms and dropdown boxes. I have a had a lot of problems but most of it is sorted now, I only need to create an "Any" search for the dropdown boxes and the problem of empty textboxes displaying all results.
Currently when a user sends a search, they may have entered in some other text boxes, but when one of the forms is left empty that automatically displays all of the results. I want it so when a search is sent and a box is empty, the code ignores that form and just checks the ones that have info inside of them.
here is my test code (not my current final form code):
<body>
<?php
$con = mysql_connect("localhost", "root", "");
mysql_select_db("delyn_db", $con);
if (!$con)
{
die("Could not connect: " . mysql_error());
}
$descrip = mysql_real_escape_string($_POST['descrip']);
$sql = "SELECT * FROM delyn WHERE description LIKE '%" . $descrip . "%'";
$r_query = mysql_query($sql);
if ($descrip === "")
{
echo 'Null value';
}
while ($row = mysql_fetch_array($r_query))
{
echo '<br /> Description: ' . $row['description'];
}
?>
</body>
Anyone have any ideas on how to stop this?
EDIT: Sorry here is my HTML with the search boxes. The above php is just where the values are sent.
<body>
<form action="form5null.php" method="post">
<label for="description">Description:</label> <input type="text" name="descrip">
<br>
<label for="trayheight">Trayheight:</label> <input type="text" name="height">
<br>
<label for="traywidth">Traywidth:</label> <input type="text" name="width">
<br>
<label for="traydepth">Traydepth:</label> <input type="text" name="depth">
<br>
<label for="trayrange">Trayrange:</label> <select name="trayrange">
<option value="BBQ">
BBQ
</option>
<option value="Dessert">
Dessert
</option>
<option value="Display">
Display
</option>
<option value="Meat">
Meat
</option>
<option value="Microwave">
Microwave
</option>
<option value="Party">
Party
</option>
<option value="Salad/Wet Pasta">
Salad/Wet Pasta
</option>
<option value="Snacks">
Snacks
</option>
<option value="Standard">
Standard
</option>
</select> <label for="traytype">Traytype:</label> <select name="traytype">
<option value="Open">
Open
</option>
<option value="Cavitised">
Cavitised
</option>
<option value="Lid">
Lid
</option>
<option value="Tray">
Tray
</option>
<option value="Coallition">
Coallition
</option>
<option value="Bowl">
Bowl
</option>
<option value="Hinge pack">
Open
</option>
<option value="Pot">
Pot
</option>
<option value="Base & Lid">
Base and Lid
</option>
<option value="Rectangular">
Rectangular
</option>
<option value="Specalist">
Specialist
</option>
</select>
<br>
<label for="trayshape">Trayshape:</label> <select name="trayshape">
<option value="Rectangular">
Rectangular
</option>
<option value="Oval">
Oval
</option>
<option value="Square">
Square
</option>
<option value="Insert">
Insert
</option>
<option value="Round">
Round
</option>
<option value="Open">
Open
</option>
</select>
<br />
<input type="submit" value="Submit">
</form>
</body>
Add the following as the first option to all your dropdowns:
<option value="*">All</option>
Then use the SQL I provided in your duplicate question here:
Make SQL ignore a specific search value if clicked in a dropdown?
Dirk Nachbar gave a good answer as well.
Have you tried limiting your SQL queries by just checking if the values are blank/null (whichever is appropriate for PHP) and just not running the query if nothing is filled in?
e.g. (pseudocode)
if(box1.HasText || box2.HasText || box3.HasText)
{
RunTheSqlForForm1();
}

Change selected option in select (form)

I have an intial group of options like these:
<select name="item[type]">
<option value="0" class="dr">First</option>
<option value="1" class="dr">Second</option>
<option value="2" class="dr">Third</option>
<option value="3" class="dr">Fourth</option>
</select>
I want to check if a variable isset($test) is defined. If it is, then I want to change the option selected where the value is equal of $test. Something like this <OPTION SELECTED>
For example. $test = 3; so, the option selected should be fourth. If $test is empty or not defined, then the first should be the option that is selected.
One way:
<select name="item[type]">
<option <?=$test==0?'selected="selected"':'';?> value="0" class="dr">First</option>
<option <?=$test==1?'selected="selected"':'';?> value="1" class="dr">Second</option>
<option <?=$test==2?'selected="selected"':'';?> value="2" class="dr">Third</option>
<option <?=$test==3?'selected="selected"':'';?> value="3" class="dr">Fourth</option>
</select>
Another:
<? $selected[$test] = 'selected="selected"'; ?>
<select name="item[type]">
<option <?=$selected[0];?> value="0" class="dr">First</option>
<option <?=$selected[1];?> value="1" class="dr">Second</option>
<option <?=$selected[2];?> value="2" class="dr">Third</option>
<option <?=$selected[3];?> value="3" class="dr">Fourth</option>
</select>
<select name="item[type]" id="selectBoxId">
<option value="0" class="dr">First</option>
<option value="1" class="dr">Second</option>
<option value="2" class="dr">Third</option>
<option value="3" class="dr">Fourth</option>
</select>
<script type="text/javascript">
var test = "<?= $test; ?>";
if (test != '' && parseInt(test)) {
document.getElementById('selectBoxId').selectedIndex = test;
}
</script>
Remove "[type]" from select name, make it simple to "item".
Then execute this code.
$test = isset($_POST['item']) ? $_POST['item'] : "0";
// assuming you are using a loop: in the loop where you create the options
$selected_html = $test == $loop_var ? ' selected="selected" ' : '';
echo "<option value=\"$loop_var\" class=\"dr\"$selected_html>$text</option>";

Categories