in a php form I have the list of brands scrollable from a select field and I would modify the selected brand displaying it in a field beside.
The php form is this:
<?php
include '../sys/conn.php';
$brands = mysqli_query ($conn, "
*(query to select brands name and id)*
") or die ("Query not valid: " . mysqli_error($conn));
mysqli_close($conn);
?>
...
<form role="form" >
<label>Brands List</label>
<select class="form-control" name='brands list'>
<?php while ($listabrand=mysqli_fetch_array($brands)){
echo '<option>'.$listabrand['0'].' - '.$listabrand['1'].'</option>';
}?>
</select>
...
<label>Modify Brand</label>
<input type="text" name='brand-name' class="form-control" required placeholder="Brand Name to modify">
</form></html>
Basically I need to select the brand's name from the named "brands list" field and display it into the 'brand-name' in order to modify and save it.
Any help?
First of all, name attribute cannot contain space.
Correct this in <select class="form-control" name='brands list'>
And for the displaying selected brand in the value of text field, use javascript or jquery.
Showing example in JQuery:
$('select[name="brands-list"]').change(function(){
var selectedBrand = $(this).val();
$('input[name="brand-name"]').val(selectedBrand);
});
This is the right code.
<select class="form-control" name='brands-list'>
<?php
while ($listabrand=mysqli_fetch_array($brands)){
echo '<option value="'.$listabrand['1'].'">'.$listabrand['0'].' - '.$listabrand['1'].'</option>';
}?>
</select>
<div class="col-lg-8">
<form role="form">
<div class="form-group">
<label>Marca</label>
<input type="text" name='brand-name' class="form-control">
</div>
</form>
</div>
<script>
$('select[name="brands-list"]').change(function(){
var selectedBrand = $(this).val();
$('input[name="brand-name"]').val(selectedBrand);
});
</script>
Related
I'm having problems with the passing of the selected options by the client (the select box is a multiple choice select box built with jquery). Basically, through the POST method, it's passing just the last option selected by the client, ignoring all the others.
<form action="inserimento_fraseP.php" method="post">
sigla Frase P <input type="text" name="siglaP" required/> <br />
significato: <input type="text" name="significatoSiglaP" required/> <br />
<br />
sigla Frase H: <select class="mul-select" multiple="true" name="siglaH"> <?php select('siglaH', 'FraseH'); ?> </select>
<input type="submit" value="inserisci"/>
</form>
<?php
function select($name, $tabella){
include 'connessione_db.php';
$sql = "SELECT DISTINCT $name FROM $tabella ORDER BY $name";
echo $sql;
$result = $conn->query($sql);
echo "<option></option>";
while($row = $result->fetch_assoc())
{
echo "<option name='$row[$name]' value = '$row[$name]' > $row[$name] </option>";
}
}
?>
PLEASE change the name=siglaH[]
<select class="mul-select" name="siglaH[]" multiple="multiple">
---
</select>
you can check the by
print_r(implode(',', $_POST['siglaH']));
And ya one more thing I would like to tell you in your database store this field as varchar
Please set select name = siglaH[] then only you will get multiple data
I edited the name of the select from "siglaH" to "siglaH[]" and in the .php file the action refers to I was able to handle the selected options as an array.
as part of my project I have two files:
modify_brand.php (php form)
update_brand.php (script to update the DB)
In File 1. the brands are listed in a select field and displayed in a separated field that allows to modify the brand's name.
The file 2. contains the query that update the DB.
The problem consists of passing the ID of the selected brand to the UPDATE query stored in update_brand.php in order to identify the record to update.
May be I am approaching the matter in a wrong way, but if not do you know how to pass to the query the ID?
modify_brand.php
<form role="form" action='../php/update_brand.php' method='post'>
<label>BRANDS LIST</label>
<select name='brands-list'>
<?php
while ($listabrand=mysqli_fetch_array($brands)){
echo '<option value="'.$listabrand['1'].'">'.$listabrand['0'].' - '.$listabrand['1'].'</option>';
}?>
</select>
<label>BRAND'S NAME TO MODIFY</label>
<input type="text" name='brand-name'>
<button type="submit" name='modify-btn' class="btn btn-default">Modifica</button>
</form>
.....
<script>
$('select[name="brands-list"]').change(function(){
var selectedBrand = $(this).val();
$('input[name="brand-name"]').val(selectedBrand);
});
</script>
update_brand.php
<?php
require '../sys/conn.php';
$mod_brand=$_POST['brand-name']
if (isset($_POST['modify-btn'])){
$brand=mysqli_real_escape_string($conn, $mod_brand] );
if ($mod_brand !=''){
$update = mysqli_query($conn,"
UPDATE mg_terms SET name= $brand
WHERE term_id=......... // THIS IS WHERE THE ID OF SELECTED BRAND SHOULD BE PLACED
");
header('Location: ../pages/success.html');}
else{header('Location: ../pages/error.html');}}
mysqli_close($conn);
?>
Hope I was enoughly clear.
So what you want to do is to set the select options value to the id, So when you submit your form, the selected id is submitted to the php. But you're gonna have to change your Javascript a bit. It should set the input's value to the options text, not the options value:
<form role="form" action='../php/update_brand.php' method='post'>
<label>BRANDS LIST</label>
<select name='brands-list'>
<?php
while ($listabrand=mysqli_fetch_array($brands)){
echo '<option value="'.$listabrand['0'].'">'.$listabrand['0'].' - '.$listabrand['1'].'</option>';
}?>
</select>
<label>BRAND'S NAME TO MODIFY</label>
<input type="text" name='brand-name'>
<button type="submit" name='modify-btn' class="btn btn-default">Modifica</button>
</form>
.....
<script>
$('select[name="brands-list"]').change(function(){
$('input[name="brand-name"]').val($('select[name="brands-list"] option:selected').text().split(' - ')[1]);
});
</script>
And on the php side:
<?php
require '../sys/conn.php';
$mod_brand=$_POST['brand-name'];
$mod_id=$_POST['brands-list'];
if (isset($_POST['modify-btn'])){
$brand=mysqli_real_escape_string($conn, $mod_brand );
$brand_id=intval($mod_id );
if ($mod_brand !=''){
$update = mysqli_query($conn,"
UPDATE mg_terms SET name= '$brand'
WHERE term_id=$brand_id
");
header('Location: ../pages/success.html');}
else{header('Location: ../pages/error.html');}}
mysqli_close($conn);
?>
i have this html for that displays 2 dropdown. one is for category and second is for subcategory
<form action="a_insert_product.php" method="post">
<div class="module_content">
<fieldset>
<label>Category</label>
<select name="catname">
<?php
$sql = "SELECT * FROM category";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result))
{
$catname=$row["catname"];
$catid=$row["id"];
?>
<option value="<? echo $catname.'-'.$catid;?>"><? echo $catname;?></option>
<?}
}?>
</select>
</fieldset>
<fieldset>
<label>Subcategory</label>
<select name="subcatname">
<?php
$sql = "SELECT * FROM subcategory";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result))
{
$subcatname=$row["subcatname"];
$subcatid=$row["id"];
?>
<option value="<? echo $subcatname.'-'.$subcatid;?>"><? echo $subcatname;?></option>
<?}
}?>
</select>
</fieldset>
<fieldset>
<label>Product Name</label>
<textarea rows="2" name="prodname"></textarea>
</fieldset>
</div>
<footer>
<div class="submit_link">
<input type="submit" name="submit" value="Submit" class="alt_btn">
</div>
</footer>
</form>
Although it is working fine but i wish to display the subcategory according to the category that is selected in the category dropdown, i don't have much knowledge regarding javascript. can anyone plz tel how it can be done
You can do it using AJAX, for example, assign an "id" attribute to both your catname and subcatname dropdowns:
<select name="catname" id="category-dropdown">
<select name="subcatname" id="subcategory-dropdown">
And then use some AJAX magic, for example:
<script type="text/javascript">
$("#category-dropdown").change(function() {
var selection_value = $(this).val();
$.post("subcategories.php", { category: selection_value },
function(result){
$.each(result, function(index, subcat) {
var option_html = "<option>" + subcat.name + "</option>";
$("#subcategory-dropdown").append(option_html);
}
},
"json");
});
</script>
Please note that this example uses jQuery, so you will have to add it to your page before using. Also, you will need to create a PHP page that will provide you with the response for subcategories, in this case I have used JSON, which is fairly easy to use.
I would do the following:
On the first dropdown, you'd have an on change handler which checks for what is selected in the dropdown. I would link that to an ajax call which then brings in the correct data to allow you to populate the second dropdown.
Google or search on here "JQuery on change" and "JQuery ajax" - there's tons of resources out there.
This is a different approach to same ends
Can anybody help me on this. For showing the rezults from an MySql database i have to select school, class, subject, and exam. But listing all the classes or all the exams is not very practical so i want to do another function that when i choose some schools in the first select box it shows me in the second select box only the classes of the selected schools.
My code is:
<div id="allselects">
<form action="viewing.php" method="post" name="filt">
<div class="multsarrange">
<h1 class="choosetext" >Chose Schools</h1>
<select class="multipleselect" name="schools[]" size="8" multiple="multiple" id="shkll">
<?php
$sql = "SELECT * FROM schools ";
$scc=mysql_query($sql);
while ($db_f = mysql_fetch_assoc($scc)) {
$schcd=$db_f['schoolcode'];
$schc=$db_f['schoolname'];
echo "<option value=$schcd >$schc</option>";
}
?>
</select>
</div>
<div class="multsarrange" id="clasaajax">
<h1 class="choosetext" >Chose an Classes</h1>
<select class="multipleselect" name="classes[]" size="8" multiple="multiple" ">
<?php
$c = "SELECT * FROM classes ";
$cl=mysql_query($c);
while ($db_f = mysql_fetch_assoc($cl)) {
$clsc=$db_f['schoolID'];
$claid=$db_f['classID'];
$clay=$db_f['year'];
$clanm=$db_f['className'];
$name=schoolidton($clsc)." ".$clay." ".$clanm;
echo "<option value=$claid >$name</option>";
}
?>
</select>
</div>
<div class="multsarrange">
<h1 class="choosetext" >Chose Subjects</h1>
<select class="multipleselect" name="subjects[]" size="8" multiple="multiple">
<?php
$sb = "SELECT * FROM subjects ";
$sbi=mysql_query($sb);
while ($db_f = mysql_fetch_assoc($sbi)) {
$sbnm=$db_f['subjectName'];
$sbid=$db_f['subjectID'];
echo "<option value=$sbid >$sbnm</option>";
}
?>
</select>
</div>
<div class="multsarrange">
<h1 class="choosetext" >Chose Exams</h1>
<select class="multipleselect" name="exams[]" size="8" multiple="multiple">
<?php
$e = "SELECT * FROM exams ";
$ex=mysql_query($e);
while ($db_f = mysql_fetch_assoc($ex)) {
$id=$db_f['examID'];
$sub=$db_f['subjectID'];
$desc=$db_f['description'];
$year=$db_f['year'];
$data=$db_f['data'];
$exnam=subidton($sub)." - ".$year." - ".$desc." - ".$data;
echo "<option value=$id >$exnam</option>";
}
?>
</select>
</div>
<div id="longsubmit">
</br></br>
<input name="submit" type="submit" value="View" />
</div>
</form>
</div>
What you need to do is the following :
Setup an event listener on the select to listen for the change event - see here
Process the change event by sending the selected value to the PHP script - see here
Using PHP get the selected value and query the database as required (you already do that bit)
Send the associated HTML output or JSON or XML if you just creating a new select list - this is a simple echo
Write the output to the screen using JavaScript - this is either creating a new element based on the reply from the PHP function or inserting the HTML response
There are lots of things within this process - each with multiple options - i suggest you attempt to tackle each one and come back with specific questions if you get stuck
use ajax on abc.php get values or ids of school and make the tags you need and return back the results to the relevant id of html
function get_options(table,id_field,name_field,where_field,field_value,select_id,edit_id){
$('#'+select_id).html('<option>Loading...</option>');
$.ajax({
type: "POST", url: "abc.php", data: "&table="+table+"&id_field="+id_field+"&name_field="+name_field+"&where_field="+where_field+"&field_value="+field_value+ "&edit_id=" + edit_id +"&get_option=1",
complete: function(data){
//alert(data.responseText);
$('#'+select_id).html(data.responseText);
}
});
}
You have to use AJAX inorder to achieve this.
check this
http://www.plus2net.com/php_tutorial/ajax_drop_down_list.php
We are working on a large form and there is 1 option for selecting installments for user, so we are using form select to show duration like 6 12 18 24 30 36 and it should give 6 boxes if admin selects 6 with 2 input fields DATE and Amount after that it should be saved to mysql using php, so can you give some advice or code please? thanks...
#seopps AFAIU you want a raw format, how to develope a form and proceed further, i m giving u just a hint, you can manipulate/modify according to your need
installment.php
<script type="text/javascript">
$(document).ready(function(){
$('select').change(function () {
$('div#wayofpay').show();
});
});
</script>
<form method="post" action="nextpage.php">
<select name="installment" id="installment">
<option value=''>Select Duration:</option>
<?php foreach(range(0,36,6) as $ts) { ?>
<option value='<?php echo $ts?>'><?php echo $ts;?></option>
<?php } ?>
</select>
<div id="wayofpay" style="display:none">
<label for='start_date'>Date:</label><input type="text" name="start_date" id="start_date" value="">
<label for='amount'>Amount:</label><input type="text" name="amount" id="amount" value="">
</div>
</form>
nextpage.php
$post_data = array_map('mysql_real_escape_string',$_POST);
// create mysql connection and use below query
$qry= "INSERT INTO yourtable SET
installment_duration = $post_data['installment'];
start_date=$post_data['start_date'];
amount =$post_data['amount'] ";
mysql_query($qry);
For the client side you can use the below jQuery script
Working example here
Enter Duration: <input type="text" id="duration" />
<button>Go</button><br />
<form id="orders">
</form>
And the javascript..
<script>
$('button').click(function(){
var count = parseInt($('#duration').val(), 10);
if(!isNaN(count)){
for(i=1;i<=count;i++){
$('#orders').append(i+". ").append("<input class='item' /><br />");
}
}
});
</script>
For the MySQL php part there are numerous examples available out there and a little googling will certainly help.