I do have a problem with two dropdown boxes on one page. Cause I have to work without a submit I use the "onblur=document.getElementById.submit" in the "". This works fine but only for one dropdown box. Using the same(equal) code for both forms will result in destroying the entries of form1 when selecting an item in form2 and visa verse.
Form-1:
<form id="formunit" name="formunit" method="post" action="" >
<select name="cbo_unit" required="required" form="formunit" style="width:100pt" onblur="document.getElementById('formunit').submit();">
<option value="0">--select--</option>
<?php
if ($_POST["cbo_unit"] == "MH") {
echo "<option value=\"MH\" selected=\"selected\">Motorhome</option>";
} else {
echo "<option value=\"MH\">Motorhome</option>";
}
if ($_POST["cbo_unit"] == "RT") {
echo "<option value=\"RT\" selected=\"selected\">Racetruck</option>";
} else {
echo "<option value=\"RT\">Racetruck</option>";
}
?>
</select>
<?php
if(isset($_POST['cbo_unit']) && $_POST['cbo_unit'] > '') {
$unit = $_POST['cbo_unit'];
}
?>
</form>
Form-2:
<?php
fc_opendb("1","something",$connect);
$dbtable = "course";
$result = mysqli_query($GLOBALS["connect"],"SELECT * FROM " . $dbtable . " WHERE blocked = 'no' ORDER BY matchcode ASC");
?>
<form id="formcourse" name="formcourse" method="post" action="" >
<select name="cbo_course" required="required" form="formcourse" style="width:130pt" onblur="document.getElementById('formcourse').submit();">
<option value="0">--select--</option>
<?php
while ($row = mysqli_fetch_assoc($result)) {
if ($row['matchcode'] == $_POST['cbo_course']) {
$selected = "selected";
} else {
$selected = "";
}
echo '<option value="' . $row['matchcode'] . '"' . $selected . '>' . utf8_encode($row['name']) . ' - ' . utf8_encode($row['place']) . '</option>'."\n";
}
?>
</select>
<?php
if(isset($_POST['cbo_course']) && $_POST['cbo_course'] > '') {
$course = $_POST['cbo_course'];
}
mysqli_close($GLOBALS["connect"]);
?>
</form>
Related
I have an SQL-database with many tables. Now I would like to create an input-form to be able to get data into the db without writing the entire sql-code every time. And this should work as follows:
All table names are listed in a drop-down menu. After having selected a table name, a new table with 4 columns is created automatically:
The first column of this table simply contains an increasing number.
The second column contains the field-names of the selected table.
In the third column there are empty input fields to enter the values for the database. Only in the third line (=product name) there is a drop-down menu with all product names from the main-table of the db.
The fourth column contains the data type (e.g. int or varchar)
All tables in the database have the same structure in the first 3 columns: the first column contains the table-id, the second column the foreign-key (=master_id) and the third column the product_name.
Up to this point, the script works well with the following 2 php-files (javasql.php and getuser.php):
javasql.php:
enter code here
<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="" class="optdrugs">please select</option>
<?php
include("files/zugriff.inc.php"); // database Access
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = 'product'";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo '<option class="optdrugs" value="'. $row['TABLE_NAME'] . '">' .
$row['TABLE_NAME']. '</option>';
echo '<br>';
}
?>
</select>
</form>
<br>
<div id="txtHint"><b>Bitte Tabelle auswählen:</b>
<br>
<?php
if (isset($_POST["submit"])) {
$sent = $_POST['sent'];
$q = $_POST['tablename'];
$column_passed = unserialize($_POST['column']); // content of array
$column is passed from getuser.php
foreach ($_POST["insertvalue"] as $key => $value) {
echo $value . "<br>";
$werte[] = "'$value'";
}
$sql="INSERT INTO $q ($column_passed) VALUES (" .
implode(", ", $werte) . ")"; // data entry
mysqli_query($db, $sql);
if (mysqli_affected_rows($db) > 0) {
echo "<h3 style='color:blue'>successful</h3>";
} else {
echo "<h3 style='color:red'>not
successful</h3>";
}
}
?>
</div>
</body>
</html>
enter code here
getuser.php:
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<form id="formdatabase" name="formdatabase" action="javasql.php"
method="post">
<input type="hidden" name="sent" value="yes">
<?php
$q = strval($_GET['q']);
$con = mysqli_connect('localhost','root','','product');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM $q";
$result = mysqli_query($con,$sql);
$numcols = mysqli_num_fields($result); // gets number of columns in result table
$field = mysqli_fetch_fields($result); // gets the column names from the result table
$data_type_array = array(
1=>'tinyint',
2=>'smallint',
3=>'int',
4=>'float',
5=>'double',
7=>'timestamp',
8=>'bigint',
9=>'mediumint',
10=>'date',
11=>'time',
12=>'datetime',
13=>'year',
16=>'bit',
252=>'text',
253=>'varchar',
254=>'char',
246=>'decimal'
);
$data_type_array = array_flip($data_type_array);
echo "<table>";
echo "<tr>";
echo "<th>" . 'Nr' . "</th><th>" . 'Column names' . "</th>
<th>" . 'Values for db-entry' . "</th><th>" . 'Type' . "</th>";
echo "</tr>";
echo "<tr>";
$nr = 1;
for($x=0;$x<$numcols;$x++):?>
<td><?= $nr; ?></td>
<td><?= $field[$x]->name; ?></td>
<?= $column[] = $field[$x]->name; ?>
<td>
<?php
if ($field[$x]->name == 'Name') { // if-Beginn
?>
<select name="insertvalue[<?= $x; ?>]" id="insertvalue<?=
$x; ?>" size="1" onchange = "javascript:getSelectedRow()">
<?php
include("files/zugriff.inc.php");
$sql = "SELECT * FROM product_main ORDER BY Name";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo '<option class="optdrugs" value='. $row['Name'] . '>' .
$row['Name'] . '</option>';
echo '<br>';
}
?>
</select>
<?php
$name_option = "";
} else {
$name_option = "<input type='text' id='insertvalue" . $x . "'
name='insertvalue[" . $x . "]' size='50'>";
echo $name_option;
}
?>
</td>
<?php
$key = array_search($field[$x]->type, $data_type_array);
if($key !== false){
echo "<td>" . $key . "</td>";
}else{
echo "<td>" . $field[$x]->type . "</td>";
}
?>
<td><?= $field[$x]->type; ?></td>
<?= $nr = $nr + 1; ?>
</tr>
<?php endfor;
echo "</table>";
mysqli_close($con);
?>
<input type="hidden" name="tablename" value="<?= $q; ?>">
<input type="hidden" name="column" value="<?php echo htmlentities
(serialize($column)); ?>">
<input type="submit" value="Enter values" name="submit">
</form>
</body>
</html>
Since I need the master_id (= foreign key) in addition to the product-name for database entry, I would like to extend my script, so that the respective master_id is automatically sent to the input field in line 2, when a product-name is selected in line 3 ... without clicking a button. I tried to do this with javascript but it didn´t work. As far as I know, the solution would be to use AJAX but unfortunately, I am not very used to AJAX.
I would be more than happy, if someone could help me to solve this problem!
I have created two select boxes each have different country names with flag images using select2 plugin and values are fetching from Database below is the code.
JS CODE
$(".currencyconverterselect").select2({
templateResult: addflag,
templateSelection: addflag
});
function addflag(opt) {
if (!opt.id) {
return opt.text;
}
var $opt = $(
'<span><img src="./img/flags/' + $(opt.element).attr('data-country-code') + '.png" class="userPic" /> ' + $(opt.element).text() + '</span>'
);
return $opt;
};
HTML CODE
<label style="color:white">Currency from</label>
<select name="fromcurrency_cc" id="fromcurrency_cc" class="form-control charts_currency" style="width:100%" required="required">
<?php
$sql = "SELECT fc.country_id,fc.code,fc.name,cc.country_code AS countrycode FROM fx_currency fc LEFT JOIN fx_country cc ON fc.country_id = cc.id ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if ($row['code'] == 'AED')
{ ?>
<option data-country-code='<?php echo $row['countrycode']; ?>' value="<?php echo $row['code']; ?><?php echo $row['countrycode']; ?>" selected>
<?php echo $row['name']; ?>(
<?php echo $row['code']; ?> )</option>
<?php } else { ?>
<option data-country-code='<?php echo $row['countrycode']; ?>' value="<?php echo $row['code']; ?><?php echo $row['countrycode']; ?>">
<?php echo $row['name']; ?>(
<?php echo $row['code']; ?> )</option>
<?php }}
} else {
echo "0 results";
}
?>
</select>
I have also created a button below is the code
</i>
I want to swap the value of FROM currency to TO Currency and vice versa by clicking the button.
I have tried below code
$("#swapvalues_btn1").on('click', function() {
var pickup = $('#fromcurrency_cc').val();
$('#from').val($('#tocurrency').val());
$('#to').val(pickup);
});
Select2 will listen for the change event on the that it is attached to. If you make any external changes that need to be reflected in Select2 (such as changing the value).
Just use below code
$("#swapvalues_btn1").on('click', function() {
var fromcurrency = $('#fromcurrency').val();
var tocurrency = $('#tocurrency').val();
$('#fromcurrency').val(tocurrency).trigger('change');
$('#tocurrency').val(fromcurrency).trigger('change');
});
For More follow the link
https://select2.github.io/options.html#events
I am stuck in getting value from drop down. Drop down is dynamically filled from sql server database.
Dropdown 1 displays product name and it is dynamically filled.
Dropdown 2 displays environment name and it is filled by HTML.
I am getting value of environment but not product.
Please help me. Thanks
Here is the code:
<form action="" method="post">
//Dropdown 1
<p>Product Name:
<select name="productname">
<option value="">Select</option>
<?php
if( $conn )
{
$sql_dd = "SELECT ProductName from Product";
$stmt = sqlsrv_query( $conn, $sql_dd );
if( $stmt === false) {die( print_r( sqlsrv_errors(), true) );}
$rows = sqlsrv_has_rows( $stmt );
if ($rows === false)
echo "There is no data. <br />";
else
{ while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
echo "<option value=''>".$row['ProductName']."</option>";
}
}
?>
//Dropdown 2
Client Type:
<select name="environment" style="width: 10%;">
<option value="">Select</option>
<option value="en1">en1</option>
<option value="en2">en2</option>
<option value="en3">en3</option>
</select>
<input type="submit" class="theme-btn" value="Search" name="submit"/>
<?php
if(isset($_POST['submit']) )
{
$productname = $_POST['productname'];
$environment= $_POST['environment'];
echo "productname: ".$productname." environment: ".$environment;
}?>
The value is not added
this:
echo "<option value=''>".$row['ProductName']."</option>";
should be
echo "<option value='" . $row['ProductName'] . "'>". $row['ProductName'] ."</option>";
I have the following php page with four dropdown list correctly populated with four differents mysql query.
I would populate Categories_2 through a query depending on CATEGORIES_1.c_id and the same nested for others dropdown.
Tried to call myFunction() onChange but it seems not working. I don't get the alert.
Does anyone know how I could do this? Any help would be much appreciated.
I know I have to use mysqli but it's an existing page and I will do it later.
<script>
var myDropdown=document.getElementsByName('cat1')[0];
function myFunction(){
alert('option changed : '+myDropdown.value);
}
</script>
<form enctype="multipart/form-data" method="post" action="import.php">
<label for="cat_name">Categories_1</label>
<select name = "cat1" onChange="myFunction()">
<?php
$s = mysql_query("SELECT * FROM `CATEGORIES_1`");
while($row = mysql_fetch_assoc($s)) {
echo ('<option value="' . $row['c_id'] . '">' . $row['c_name'] . '</option>'); } ?>
</select>
<br>
<label for="cat_nam">Categories_2</label>
<select>
<?php
$s = mysql_query("SELECT c_name FROM `CATEGORIES_2`");
while($row = mysql_fetch_assoc($s)) {
echo ('<option value="0">' . $row['c_name'] . '</option>'); } ?>
</select>
<br>
<label for="cat_nae">Categories_3</label>
<select>
<?php
$s = mysql_query("SELECT c_name FROM `CATEGORIES_3`");
while($row = mysql_fetch_assoc($s)) {
echo ('<option value="0">' . $row['c_name'] . '</option>'); } ?>
</select>
<br>
<label for="cat_ame">Categories_4</label>
<select>
<?php
$s = mysql_query("SELECT c_name FROM `CATEGORIES_4`");
while($row = mysql_fetch_assoc($s)) {
echo ('<option value="0">' . $row['c_name'] . '</option>'); } ?>
</select>
You need to use ajax for that .. i hope this helps ..
JS
$(document).ready(function(){
$(document).on('change','#cat1',function(){
$.ajax({
cache: false,
url: 'getSecondSelect.php',
type: 'post',
data: 'input=' + $(this).val(),
success: function(data){
$('#cat2').html(data)
}
})
})
}
FORM
<form enctype="multipart/form-data" method="post" action="import.php">
<label for="cat_name">Categories_1</label>
<select id="cat1" name="cat1">
<?php
$s = mysql_query("SELECT * FROM `CATEGORIES_1`");
while($row = mysql_fetch_assoc($s)) {
echo ('<option value="' . $row['c_id'] . '">' . $row['c_name'] . '</option>'); }
?>
</select>
<br />
<label for="cat_name">Categories_2</label>
<select id="cat2" name="cat2"></select>
<br />
getSecondSelect.php
$s = mysql_query("SELECT c_name FROM `CATEGORIES_2` WHERE CATEGORIES_1.c_id='".$_POST['input']."'");
while($row = mysql_fetch_assoc($s)) {
echo ('<option value="0">' . $row['c_name'] . '</option>'); }
This is wrong method to call your dropdoen value
var myDropdown=document.getElementsByName('cat1')[0];
Your myFunction would be
<script>
function myFunction(){
var myDropdown=document.getElementById('cat1');
var myDropdowval= myDropdown[myDropdown.selectedIndex].value;
alert(myDropdowval);
}
</script>
here you assign id id="cat1" to your dropdown
<select name = "cat1" id="cat1" onChange="myFunction()">
<?php
$s = mysql_query("SELECT * FROM `CATEGORIES_1`");
while ($row = mysql_fetch_assoc($s)) {
echo ('<option value="' . $row['c_id'] . '">' . $row['c_name'] . '</option>');
}
?>
</select>
Use ajax call and different php pages to generate response based on AJAX call, send selected drop-down id as parameter to php page. and set that incoming response to your below drop-downs.
If your using Jquery then use
$('[name=cat1]').val()
<select id="myselect">
<option value="1">Mr</option>
<option value="2">Mrs</option>
<option value="3">Ms</option>
<option value="4">Dr</option>
<option value="5">Prof</option>
</select>
alert($( "#myselect option:selected" ).text());
and here is working JSFIDDLE
I am creating a dropdown list in php. how can i put the selected item when someone selects an item.
my php code:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<select name="app" id="dropdown" value="" onchange="this.form.submit()" ><option>--select-app--</option>
<?php
$sql="select * from application";
$result=mysqli_query($con, $sql) or die("ereor selecting app ".mysqli_error($con));
while($row=mysqli_fetch_array($result))
{
$selected = $row['name'];
echo "<option id=". $row['id']."value = ".$row['id'].">".$row['name']."</option>";
}
echo "</select>";
?>
i want this: if I select an item it will show it as selected. how can I do this
You can do it in php like
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<select name="app" id="dropdown" value="" onchange="this.form.submit()" >
<option>--select-app--</option>
<?php
$sql="select * from application";
$result=mysqli_query($con, $sql) or die("ereor selecting app ".mysqli_error($con));
$selected_val = $_POST['app']; //Should be $_GET, $_POST, $_SESSION whatever your selected value is
while($row=mysqli_fetch_array($result))
{
if(trim($row['id']) == trim($selected_val)) //<== Change this line
$selected = 'selected="selected"';
else
$selected = '';
echo '<option id="'. $row['id'].'" value="'.$row['id'].'" '. $selected.'>'. $row['name'] .'</option>';
//^Change this line
}
echo "</select>";
?>
In jQuery you can do it like
$('#dropdown').val('<?php echo "My val"; //The value goes here ?>');
Assuming that you mean you want to retain the selection after the form is submitted, you can do this inside the while loop:
$selected = (isset($_POST['app']) && $_POST['app'] == $row['id'] ? 'selected' : '');
echo "<option id=".$row['id']." value = ".$row['id']." ".$selected.">".$row['name']."</option>";