Show/ hide dynamic combobox - php

I have two comboboxes which are generated by queries from mysql.
When user choose value in first combobox i need to display second combobox, but if nothing is choosen second combox should not be displayed.
The code:
<?php
echo "<form method= \"post\" name=\"formcombo\" action=''>";
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Choose main category</option>";
while($cat2 = mysql_fetch_array($query2)) {
if($cat2['category_id']==#$category)
{
echo "<option value='$cat2[category_id]'>$cat2[category_name]</option></br>";}
else
{
echo "<option value='$cat2[category_id]'>$cat2[category_name]</option>";
}
}
echo "</select></br>";
echo "<select name='subcat'><option value=''>Choose subcategory</option>";
while($cat = mysql_fetch_array($query1)) {
echo "<option value='$cat[subcat_name]'">$cat[subcat_name]</option>";
}
echo "</select>";
echo "<input type=\"submit\" value =\"Submit\">";
echo "</form>";
?>
I use javascript to generate combobox values:
<script type="text/javascript">
function reload(form){
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='main.php?cat=' + val ;
}
</script>

If you are okay with jQuery js library, it is easy.
see this jQuery plugin http://plugins.jquery.com/project/selectchain
try the demo provided in this page and use the plugin to accomplish.

Related

PHP onchange event of select box

I am new to PHP and this community
I was trying to use onchange function in php select
but it seems to be not working
I searched for solutions but basically are all with JS and the value goes to the URL, but my value is going to use in php for loop
don't want to get the parameter in URL
please help
echo "<select onchange='myFunction(this.value)'>";
echo "<option value=''>select your number</option>";
echo "<option value='5'>5</option>";
echo "<option value='10'>10</option>";
echo "<option value='15'>15</option>";
echo "</select>";
//////////////////////////////////////////////////////
function myFunction($numbers){
for($count=0;$count<$numbers;$count++)
echo $count;
}
by the way, inside for loop contains some MySQL so that's why i'm using PHP
function change_function(element){
document.location.href = element.value
}
<select class="form-contro-drop" name="val" onChange="change_function(this);" >
<option value="ur.php?val=1">1</option> <option value="ur.php?val=2">2</option></select>
Hope this helps!
I think you can coding like this
<?php
header('contact-type:text/javascript;charset=utf-8');
echo "<select onchange='myFunction(this.value)'>";
echo "<option value=''>select your number</option>";
echo "<option value='5'>5</option>";
echo "<option value='10'>10</option>";
echo "<option value='15'>15</option>";
echo "</select>";
?>
<script>
function myFunction($numbers){
for($count=0;$count<$numbers;$count++)
echo $count;
}
</script>

Retain select option inside php if statements

I am attempting to create a single PHP page that opens a salesman or customer window based off a hidden input field in a form being posted. The tricky part for me is I also need two select option dropdowns that appear based on the same hidden input field being posted but ALSO retain their value after submit.
I know how to retain the values of a form like so..
<form action="" method="post">
<select name="newyear">
<option <?php if ($sqlyear == '2015cust') { ?>selected="true" <?php }; ?>value="2015cust">2015</option>
<option <?php if ($sqlyear == '2016cust') { ?>selected="true" <?php }; ?>value="2016cust">2016</option>
</select>
</form>
But when I try and implement this method inside my PHP if statements it does not retain the selected option.
if ($open == 'salesman_window'){
echo "This is salesman dropdown menu";
echo "$sqlyear";
echo "<form action='' method='post'>";
echo "<input type='hidden' name='newyear_trigger' value=''>";
echo "<input type='hidden' name='window' value='salesman_window'>";
echo "<select name='newyear'>";
echo "<option if ($sqlyear == '2015users') { selected='true' }; value='2015users'> 2015</option>";
echo "<option if ($sqlyear == '2016users') { selected='true' }; value='2016users'>2016</option>";
echo "</select>";
echo "<button class='documentation_button'>Submit</button>";
echo "</form>";
}
if ($open == 'customer_window'){
echo "this is customer dropdown menu";
echo "$sqlyear";
echo "<form action='' method='post'>";
echo "<input type='hidden' name='newyear_trigger' value=''>";
echo "<input type='hidden' name='window' value='customer_window'>";
echo "<select name='newyear'>";
echo "<option if ($sqlyear == '2015cust') { selected='true' }; value='2015cust'>2015</option>";
echo "<option if ($sqlyear == '2016cust') { selected='true' }; value='2016cust'>2016</option>";
echo "</select>";
echo "<button class='documentation_button'>Submit</button>";
echo "</form>";
}
?>
I have tried breaking up the option with multiple echo ""; and tried a few ".." of these in there as well but either get syntax errors or it just doesn't retain the selection. Everything works in regards to showing the correct dropdown menu based on what is posted but I just cannot get the select option to retain. The $sqlyear is for sure getting the correct value each time the dropdown option is selected so I know it isn't that either. Can anyone help?
A couple of options:
Use a ternary operator in your echo:
echo "<option" . (($sqlyear == '2015users') ? " selected='true'" : '')
. " value='2015users'>2015</option>";
echo "<option" . (($sqlyear == '2016users') ? " selected='true'" : '')
. " value='2016users'>2016</option>";
Keep using the if, but without opening/closing PHP tags as much:
<option <?php if ($sqlyear == '2015users') echo " selected='true'"; ?> value='2015users'>
2015</option>
<option <?php if ($sqlyear == '2016users') echo " selected='true'"; ?> value='2016users'>
2016</option>

how to get value selected on other page?

I have dropdown to edit a form.
All ISBN records are listed in dropdown. The user will select the ISBN that he want to update. Now how to get which ISBN record was selected by that user?
My logic of dropdown is as follows:
function update()
{
$select_query="Select ISBN from book";
$select_query_run = mysql_query($select_query);
echo"<form method='post' action='update.php'><center>Select ISBN you want to Update: ";
echo "<select name='isbn' id='isbn'>";
while ($select_query_array=mysql_fetch_array($select_query_run) )
{
echo "<option value='' >".htmlspecialchars($select_query_array["ISBN"])." </option>";
}
echo "</select>";
echo '<br><br><br><input type="submit" value="Update"></center></form>';
} After selecting ISBN the user will be navigated to update php page whoch is as follow:
<?php
$isbn=$_POST['isbn'];
echo "ISBN Selected: ".$isbn;
?>
Output of update page:
ISBN Selected:
Because your value is empty in select box
echo "<option value='' >".htmlspecialchars($select_query_array["ISBN"])." </option>";
^^
You need to add value in your select box
echo "<option value='".htmlspecialchars($select_query_array['ISBN'])."' >".htmlspecialchars($select_query_array["ISBN"])." </option>";
For this you should try with this:
echo "<option value='' >".htmlspecialchars($select_query_array["ISBN"])." </option>";
Instead of this:
echo "<option value='".htmlspecialchars($select_query_array['ISBN'])."' >".htmlspecialchars($select_query_array["ISBN"])." </option>";

How to get value of input field from a form in a script

I am trying to display a value from a form in a script using console.log() for troubleshooting purposes but it seems to not be working. Here is my script.
Is there something I am missing?
jQuery(document).ready(function() {
var user = jQuery('#id').val;
var file = jQuery('#custom-file-input').val;
console.log(file);
});
echo "<form method='post' enctype='multipart/form-data' id='test_ajax'>";
echo "<select name='id' id='form-option' class='test-only'>";
echo '<option selected="selected">' .'Choose a User'. '</option>';
foreach ($registeredUsers as $key => $value) {
$registered = JFactory::getUser($value);
echo '<option value="'.$registered->id.'">'.$registered->name.'</option>';a
}
echo "</select>";
echo "<input name='uploadedfile' type='file' id='custom-file-input' class='test-only' /> <br/>";
echo '<input type="submit" name="submit" value="Upload" id="custom-submit-input">';
echo "</form>";
Use val() to get the value of an element.
var user = jQuery('#id').val
Should be
var user = jQuery('#form-option').val();
Notice () of val
The id of element is form-option, id is the name of the dropdown.

How to call JavaScript function in "php" inputbox in echo on "onChange" event

javascript function which is used in OnChange event
function calc(A,B,SUM) {
var one = Number(A);
if (isNaN(one)) { alert('Invalid entry: '+A); one=0; }
var two = Number(document.getElementById(B).value);
if (isNaN(two)) { alert('Invalid entry: '+B); two=0; }
document.getElementById(SUM).value = parseInt(one) + parseInt(two);
}
I have forloop for create 10 input fox
Input boxes
<?php
for($k=1;$k<=$textboxindex;$k++)
{
echo "<input type=\"text\" class='textboxsizeqty' name=\"qty[]\" value=\"$qty\" id ='op1' onChange=\"calc('this.value','op2','result');\"/>";
echo "<br/>";
}
?>
Input boxes
<?php
for($k=1;$k<=$textboxindex;$k++)
{
echo "<input type=\"text\" class='textboxsize' name=\"rate[]\" value=\"$rate\" id ='op2' onChange=\"calc('this.value','op2','result');\" />";
echo "<br/>";
}
?>
<?php
for($k=1;$k<=$textboxindex;$k++)
{
echo "<input type=\"text\" class='textboxsize' name=\"sum[]\" value=\"\" id='result'/>";
echo "<br/>";
}
?>
It's doesn't work here what I am doing.
I'm New in PHP.
Thanks
Please try and why do you give the quotes in this.value.
<?php
for($k=1;$k<=$textboxindex;$k++)
{
echo "<input type=\"text\" class='textboxsizeqty' name=\"qty[]\" value=\"$qty\" id ='op1' onChange=\"calc(this.value,'op2','result');\"/>";
echo "<br/>";
}
?>

Categories