Why Can't I Select Dynamically (PHP) Generated Options? - php

I am having an issue with a table where each cell contains an id'd input or select field. The cell contents are dynamically generated from a MySQL query. Everything works fine except for my dynamically generated SELECT drop downs. I am using the following code in a switch statement:
case 'crew_1':
case 'crew_2':
echo '<td><select class="'.classExt($types[$key]).' '. $key .'_c" id="' . $key . '--' . $idx .'">';
echo '<option value="" selected="selected"></option>';
while ($rowtech = mysqli_fetch_assoc($rtech)){
echo '<option value="' . $rowtech['name'] . '">' . $rowtech['name'] . '</option>';
} mysqli_data_seek($rtech,0);
echo '<option value="TEST">TEST</option>';
echo '</select></td>'; break;
This creates the following HTML:
<td>
<select class="varchar crew_1_c" id="crew_1--1">
<option value="" selected="selected"></option>
<option value="Name 1">Name 1</option>
<option value="Name 2">Name 2</option>
<option value="TEST">TEST</option>
</select>
</td>
From this I can select the blank or 'TEST' option and it returns the correct value in a console.log, but if I choose one of the dynamically generated names, the box returns back to the default blank value. I get the same responses on Chrome and IE.
Where is my disconnect?
Included for the person requesting the script generating to output to console:
$('tbody select, tbody input').change(function(){
console.log($(this).val());
});

OP, you're missing the name attribute.
This works fine for me:
<form method="post" action="">
<select class="varchar crew_1_c" id="crew_1:1" name="choice">
<option value="" selected="selected"></option>
<option value="Name 1">Name 1</option>
<option value="Name 2">Name 2</option>
<option value="TEST">TEST</option>
</select>
<input type="submit" value="Submit" name="submit" />
</form>
<?php
var_dump($_REQUEST);
?>

Found my issue. The JS request earlier had me dig back through my code to discover that I was setting my values toUpperCase() for submission for consistency, but that was breaking my value match on the drop down. I set my values to uppercase in the select options also and TA-DA!
Thanks for the help, guys. Sometimes it takes a fresh pair of eyes to point out the obvious.

Related

Whats the possibility of one updating a database field with a form having a drop down without changing the value of the drop down field

Lets say i input data into a mysql database using a drop down like:
<div>
<input name="name" type="text" id="name" placeholder="Enter Name"></div>
<div>
<select name="position" id="position" type="text">
<option selected="selected">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
With a normal text field, i can simply include the value field to show on the form the initially entered data in the table. And change that so when i submit form, it updates the related field.
<div>
<select name="position" id="position" type="text" value="<?php echo $row_position['Position']; ?>>
<option selected="selected">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
So assuming i do not want to update the field relating to the drop down, how do i it? Because what happens is, once i submit the form without selecting any option from the drop down, it picks the first option from the list which is the "Select" option and uses that to update the related field in the database.
I'd suggest at least two steps to make it user-friendly and achieve your goals:
First, make the initially entered selection selected if available:
<?php
$positions = Array("A", "B", "C");
?>
<div>
<select name="position" id="position">
<option value="-1">Select</option>
<?php
foreach($positions as $v) {
$selected = ($row_position['Position']===$v) ? "selected" : "";
echo "<option value='$v' $selected>$v</option>";
}
?>
</select>
</div>
And on the receiving php-script check if you've received a valid option, otherwise send error-message:
<?php
//other stuff...
if($_POST['position']>0) {
// save that value
} else {
// send error to user
}
// even more stuff..
?>
Notes: in select tag there is no type=text nor a value=anything available.
Assuming you are fetching dropdown options from database.
Note: Array keys, variables are only for demonstration purposes. This may differ from your actual records.
<div>
<select name="position" id="position">
<?php foreach($options as $option): ?>
<option value="<?= $option['position'] ?>" <?php if($option['position'] == $current_record['position']) { echo "selected"; } ?>> <!-- e.g. $option['id'] -->
<?= $option['name'] ?>
</option>
<? endforeach; ?>
</select>
</div>
What i have done that has solved the issue was to simply add the value from the database to the selectedoption or the first option field. So by default without changing the drop down, the first option with the value from the database is selected. And so there would be no change.
<div>
<div><?php echo $row_position['Position']; ?></div>
<select name="position" id="position">
<option value="<?php echo $row_position['Position']; ?>Select to Change</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
This gives a simple or straight forward solution to the problem.
Will still try the other suggestions to see how it all plays out. Thanks guys.

Query Select Sql with Condition from a Value of Html combobox

I have a problem of my web programming using php. I have an input form where there are two combobox with several options.
In the form, there also a textfield of Code where the value is determined by what is selected from both combobox (as the condition) from mySql database using select query.
Like= Select code from tableA where condition1=itemComboA and condition2=itemComboB;
I wanna ask, how to retrieve the value of the textfield automatically when user select an item in both combobox?
The query above still not working, then I manage it to select the code in another page then I redirect it to the input form by using link with the Code Like this: http://localhost/data/input.php?code=6
I still can't Set the value='<?php echo $code ?>;' in the value of textfield because the select query isn't working.
Your html will look like this
<form action="input.php" action="GET">
<select name="code">
<option value="1"> option 1</option>
<option value="2"> option 2</option>
<option value="3"> option 3</option>
</select>
<select name="code2">
<option value="drp1"> option 1</option>
<option value="dpr2"> option 2</option>
<option value="drp3"> option 3</option>
</select>
<input type="submit" value="Submit" />
</form>
for testing purposes, in your php file, print this to show content of your array
print_r($_GET);
from there you can get the values to put into your mysql query
$query = 'SELECT code FROM tableA WHERE condition1 = ' . mysql_real_escape_string($_GET['code1']) . ' AND condition2 = ' mysql_real_escape_string($_GET['code2']) . ';';
At the top of your php file you can have:
<?php
if(!empty($_POST) {
$query = 'SELECT code FROM tableA WHERE condition1 = ' . mysql_real_escape_string($_POST['itemComboA']) . ' AND condition2 = ' mysql_real_escape_string($_POST['itemComboB']) . ';';
}
?>
It seems as though your form doesn't have method="post" defined so you may need use $_GET instead of $_POST.

JScript Variables with Onchange

After a bit of help and learning, I have managed to change a target via JScript, but I can't seem to figure out how to make a variable or make it do a calculation.
My code so far is (these are three Form Boxes + a Submit Button):
NUMBER
<input name="contact_build"
type="text"
id="contact_build"
value="<?php echo $row_contact['contact_build']; ?>" />
TYPE
<select id="contact_type"
name="contact_type"
onchange="targ.value=this.value" />
<?php
$specresult = mysql_query("SELECT * FROM `types` ORDER BY type_value");
while ($specrow = mysql_fetch_array($specresult)) {
echo '<option value="' . $specrow['type_name'] . '">' . $specrow['type_name'] . '</option>';
}
?>
</select>
VALUE
<input name="contact_value" id="targ" value="<?php
$var1 = $row_contact['contact_build'];
$var2 = $row_value['type_value'];
$total = round($var2 * $var1);
echo "" . $total . "";
?>" />
<input type="submit" name="Submit2" value="Update" />
What I want to do is make "TARG" the "$VAR2" variable, so that it can work out the VALUE calculation then echo (or what ever command is required) the answer in the VALUE box, as soon as the user changes the option in the TYPE box. Hope this makes sense.
At the moment I have managed to get it to show the "TYPE" value in the "VALUE" box. It ignores any "echo" command above.
Am I looking at this incorrectly and it should be done another way?
PS: I know it's MYSQL; I am working on learning PDO as we speak to change it over. :-) But for the moment, I just want to figure this out using the language that I have basic knowledge of.
Go through the code below.
<body>
<input name="contact_build"
type="text"
id="contact_build"
value="<?php echo $row_contact['contact_build']; ?>" />
<select onchange="change(this.value)" >
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="3"> 3</option>
</select>
<input type="text" id="targ" value="2"/>
<script>
function change(val) {
var cnt_bld = document.getElementById("contact_build").value;
var total_val = Math.round(parseInt(val) * parseInt(cnt_bld));
document.getElementById("targ").value = total_val;
}
</script>
</body>
In the above code i have called a function change(this.value) which will accept the select box value. Then in the function I have used that value to change the value of input field. Look at document.getElementById. With your code your were directly using a id instead. Hope this helps.
EDIT
this line
echo '<option value="' . $specrow['type_name'] . '">' . $specrow['type_name'] . '</option>';
can be done like this if you have those name and value.
echo '<option value="' . $specrow['type_value'] . '">' . $specrow['type_name'] . '</option>';
assuming your another column name as type_value where values are stored like 100,200and so on and type_name is the name like good, better or best.
Hope this is what you may want
<html>
<body>
<input name="contact_build"
type="text"
id="contact_build"
value="10" />
<select onchange="change(this.value)" >
<option value="0">Select</option>
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="3"> 3</option>
</select>
<input type="text" id="targ" value="0"/>
<script>
function change(val) {
document.getElementById("targ").value = document.getElementById("contact_build").value*val;
}
</script>
</body>
</html>

Using select dropdowns in a form

I am trying to use HTML select dropdowns to help sort the search results on my website and I am having some trouble. In the code below, I specifically select Option1 and when I try to echo the result after I submit the form, it always echo's 'FAIL'. I was wondering if someone can help me understand what I am doing wrong and what is the proper way to retrieve the data from the option the user selected, after the form was submitted?
<?php
echo '<form method="post" action="search.php">
<select>
<option name="one">Option1 </option>
<option name="two">Option2</option>
</select>
<input type="text" name="searchword" />
<input type="submit" value="Search" />
</form>';
if(isset($_POST['one'])){
echo 'Option1 is set';
}else{
echo 'FAIL';
}
?>
thank you
This is because the name attribute goes with the select tag.
<select name="dropdown">
<option>Option 1</option>
<option>Option 1</option>
</select>
if(isset($_POST['dropdown'])) {
echo $_POST['dropdown']; //echoes 'Option 1'
}
If you like, you can add a value attribute to the option element, but you don't have to.
If you do
<option value="foobar">Option1</option>
The form will post foobar and not Option1.
<?php
echo '<form method="post" action="search.php">
<select name="my_option">
<option value="one">Option1 </option>
<option value="two">Option2</option>
</select>
<input type="text" name="searchword" />
<input type="submit" value="Search" />
</form>';
echo "My option value is : " . $_POST['my_option'];
?>
You need to name your select tag and access that instead.
<select name="options"></select>
echo $_POST['options']; will give you your selected option
Instead of name="one" an option needs a
value="myvalue"
and your select tag needs an
name="nameofthisthinghere"

Multiple Select Box in Wordpress Custom Meta Box Behaves and looks like text input

I am trying to add a select dropdown with multiple="multiple" in a custom meta box, and the select is appearing with fancy styling, is not clickable, and is not the size I am setting:
<select multiple="multiple" size="3" name="location">
<option value="">Please select</option>
<option value="0">All</option>
<?php
foreach(get_terms('town',array('get' => 'all')) as $term)
{
if (!empty($term->name))
{
$str .= "<option value='" . $term->term_id . "'";
$str .= (is_object_in_term($post->ID, "town", $term->name)) ? " selected>" : ">";
$str .= $term->name . "</option>";
}
} echo $str;?></select>
What I get is more like a text input box in appearance, although firebug shows the code is correct for a select box multiple complete with options. Any help gratefully received.
UPDATE :: ADding code of select for Bingjie's request in comments:
<select name="location" size="3" multiple="multiple">
<option value="">Please select</option>
<option value="0">All</option>
<option value="5">Akbuk</option><option value="4">Altinkum</option></select>
add style css.
<select multiple="multiple" size="3" name="location" style="height:200px;">
Hi thanks for you updates. I think the meta box is still custom meta data which is Key-value paired, so it is not allow to select multiple keys. when you retrieve post_meta, you call
get_post_meta($post_id, $key, $single);
so it is not allowed to use multiple keys.
I am not sure, but it is the only reasonable explanation since the select code appears correct.

Categories