I have two dropdowns in my form.
1)Brand Name of car
2)Model name in which data is going to be retrieved from database on the basis of brand name selected.
so I have taken a div tag and appended that second dropdown.The data in dropdown is displayed properly but the problem is in storing the data selected from the second dropdown, It stores the model name upto the space only.length in the database for that field is also taken properly.
eg- if the model name is "series 1" than it will store only series.
Php page of second dropdown:
<?php
$brand = $_GET['brand'];
include('connection.php');
$sql = "select * from modelname where Brand_Name='" . $brand . "'";
$result = mysqli_query($conn, $sql);
$html = "";
$html = '<select name="model">';
while ($row = mysqli_fetch_assoc($result)) {
$html.='<option value=' . $row['M_Name'] . '>' . $row['M_Name'] . '</option>';
}
$html.='</select>';
echo $html;
?>
because you are not quoting your attributes.
value=hello world
is seen as
value="hello" world
Add the missing quotes.
Related
I am trying to POST a form that is generated through a while loop that iterates on some elements of a table and they can vary in number according to the different companies. Because the IDs of the input/edit fields are created by a while-loop they all have the same id and this is creating a problem when I try to submit the form, is there a way to change automatically the IDs? I tried to use Javascript with no success. Is there a way to name the IDs according to the loop cycle?
Also is there a better way to create a number of input/edit fields according to the number of items to be edited in an SQL table?
echo '<form id="myForm" action="edit.php" method="POST">';
echo '<div class="leftcontact">';
$i=0;
while($query_data = mysqli_fetch_row($result)) {
$_SESSION["List"]=$query_data[2];
$_SESSION["List_company_id"]=$query_data[0];
$result2 = mysqli_query($connection, "SELECT * FROM `calling_lists` WHERE `calling_list_id`='".$_SESSION["List"]."'");
$query_data2 = mysqli_fetch_row($result2);
$_SESSION["list_name"]=$query_data2[1];
echo '<div class="form-group">';
echo '<p>List number</p>';
echo '<select class="dropdown" name="ID">';
echo "<option selected = 'selected' value=\"".$_SESSION["List_company_id"]."\">".$_SESSION["list_name"]."</option>";
$query1="SELECT * FROM `calling_lists`";
$result1=mysqli_query($connection,$query1) or die ("Query to get data from list table failed: ".mysql_error());
while ($row1=mysqli_fetch_array($result1)) {
$list_name=$row1["calling_list_name"];
$list_description=$row1["calling_list_description"];
$list_id=$row1["calling_list_id"];
echo "<option value=\"$list_id\">"
. $list_name .
"</option>";
};
$i=$i+1;
};
Just use your $i variable to create the id attribute of the controls you emit from your while loop. So they would be named for example “select1”, “select2” and so on.
it was late and I could not figure out the answer, but with a fresh look I found it. In the code there are actually 2 problems, one is that the operator is missing and there was a problem with " ' ".
The working code is here below, in case anybody needs it. It creates a number of dropdown menu, as many as my $results rows.
Hope it helps anybody.
$i=0;
while($query_data = mysqli_fetch_row($result)) {
$_SESSION["List_company_id"]=$query_data[3];
$_SESSION["List"]=$query_data[1];
$_SESSION["list_name"]=$query_data2[2];
echo '<div class="form-group">';
echo "<p>List number $i</p>";
echo '<span class="icon-case hidden-xs"><i class="fa fa-home"></i></span>';
echo "<select class='dropdown' name=\"".$_SESSION['List_company_id']."\">";
echo "<option selected = 'selected' value=\"".$_SESSION["List_company_id"]."\">".$query_data[2]."</option>";
$query1="SELECT * FROM `calling_lists`";
$result1=mysqli_query($connection,$query1) or die ("Query to get data from list table failed: ".mysql_error());
while ($row1=mysqli_fetch_array($result1)) {
$list_name=$row1["calling_list_name"];
$list_description=$row1["calling_list_description"];
$list_id=$row1["calling_list_id"];
echo "<option value=\"$list_id\">
$list_name
</option>";
};
echo "</select>";
echo "</div>";
$i=$i+1;
};
I have run into a problem and Im not sure where to start... So my problem is- I need to use a dropdown which is automatically populated by a query concatenating two fields to make a single full-name. Then based off the name of the person selected change the value of the text input to be the dob, then also update every change when a new name selected. I have tried using the onchange event for select but just cant work out the right combination.
<?php
$conn = new mysqli('localhost', 'username', 'password', 'database') or die ('Cannot connect to db');
$result = $conn->query("SELECT id, dob, CONCAT(`space`,`firstname`,`lastname`) AS `whole_name` FROM `table`");
echo "<select name='id'>";
while ($row = $result->fetch_assoc()) {
$data = $id
unset($id, $name);
$id = $row['id'];
$name = $row['whole_name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
?>
<input type="text" id="text" value="<?php echo "$data" ?>"/>
Things to note - due to the concatenate needing a separator 'space' has been added to a field. at this time the sql query works exactly as intended. When loading the page in the current state the text field comes up with the correct value for the first name, but does not update when changing value of select.
Im new with PHP and mySQL - self learning as I go. Any help appreciated.
Ajax not needed here. You can just put the dob value as special attribute for each options, and then change the text input with javascript when the select value change.
Here the new select with dob in attribute :
while ($row = $result->fetch_assoc()) {
$data = $id
unset($id, $name);
$id = $row['id'];
$dob = $row['dob'];
$name = $row['whole_name'];
echo '<option dob="'.$dob.'" value="'.$id.'">'.$name.'</option>';
}
and then update the input when the select change, here an example with jquery :
$("select[name=id]").change(function(event) {
//get the dob attribute
var dob = $('option:selected', this).attr('dob');
// update the text input
$("input#text").val(dob);
});
I have a form where user selects the category while adding the product.
When user want to edit the product, i am displaying all the previously populated values but could not able to figure out how to display the category he selected.
addproduct.php (displaying the categories from the database)- this code is working fine and can see all the categories in dropdown
<?php
require'dbconn.php';
$subject = mysql_query("select * from categories", $link);;
while($subjectData = mysql_fetch_array($subject)){
echo $subjectData['value'];?>
<option value="<?php echo $subjectData['name'];?>"><?php echo
$subjectData['name'];?>
</option>
In the edit product i want to display all the categories like above, but want to display the selected category in the form which i could not able to do.
editproduct.php (rough draft code) -- not working
<?php
require'dbconn.php';
$subject = mysql_query("select * from categories", $link);;
while($subjectData = mysql_fetch_array($subject)){
echo $subjectData['value'];?>
<option select="<?php echo $cat;?>"value="<?php echo $subjectData['name'];?>"><?php echo
$subjectData['name'];?>
</option>
$cat - category value(previously selected) pulled from database
require'dbconn.php';
$subject = mysql_query("select * from categories", $link);;
<option value="<?php echo $cat;?>"><?php echo $cat;?></option>
while($subjectData = mysql_fetch_array($subject)){
echo $subjectData['value'];?>
<?php if($cat!=$subjectData['name']){?> <option value="<?
php echo $subjectData['name'];?>"><?php echo
$subjectData['name'];?>
</option>
<?php } ?>
Try using this code and please use mysqli as mysql is deprecated. previously selected category should be before while loop. Hope it helps
Two issues with your code:
You are using mysql functions, which are depreciated and don't even exist in the current version of PHP. Use mysqli or PDO functions.
The html you are generating is invalid syntax.
I'll leave the first issue to you to correct.
For the 2nd issue, all of the non-selected options in your dropdown will not have the selected attribute.
Only the selected item will have that attribute. The code below assumes that the variable $cat has the previously selected value, and each row has a
column named 'cat'. When $cat matches the value in the column 'cat', it will add selected='selected' to the option.
<?php
require 'dbconn.php';
$subject = mysql_query("select * from categories", $link);;
while($subjectData = mysql_fetch_array($subject)){
echo $subjectData['value'];
$selected = "";
if($cat == $subjectData['cat']) {
$selected = "selected='selected' ";
}
echo "<option ".$selected."value=".$subjectData['name'].">";
echo $subjectData['name'];
echo "</option>\n";
}
?>
I'm trying to make an invoice php page which contain 2 dropdown option: Supplier_Name and Item_ID. Both of them is not predetermined. Supplier_Name option obtained from another page (List of Supplier, which can be modify). Item_ID option obtained from another page too (List of Item, which can be modify too). I'm quite new about this and my data is small, so I'm looking the simplest way possible.
So far, I knew how to populate Supplier_Name from List of Supplier database. I used this code:
<td>Supplier</td>
<td>
<?php
mysql_connect("localhost","root","");
mysql_select_db("stock");
$result = mysql_query("SELECT * from input_supplier_data");
$jsArray = "var invoice = new Array();\n";
echo '<select name="supplier_name" onchange="changeValue(this.value)">';
echo '<option></option>';
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['supplier_name'] . '">' . $row['supplier_name'] . '</option>';
$jsArray .= "invoice['" . $row['supplier'] . "'] = {name:'" . addslashes($row['supplier']) . "',desc:'".addslashes($row[''])."'};\n";
}
echo '</select>';
?>
</td>
<br /<input type="text" name="input_supplier_data" id="input_supplier_data"/>
<script type="text/javascript">
<?php echo $jsArray; ?>
function changeValue(id){
document.getElementById('input_supplier_data').value = supplier_name[id].name;
};
</script>
When a Supplier selected, only their product will be displayed in the second dropdown option. Since List of Item page contain all item from all supplier, I don't know how to limit them. What I can do so far is only if the supplier was predetermined. I used this code:
SELECT input_item_data.`item_id` FROM `input_item_data` WHERE input_item_data.`supplier` = 'UNILEVER'
But as I stated earlier, the item is not predetermined, it can be modify in List of Item page. Hope anyone can help me. Thanks.
in my code i'm showing drop down select field list base on user school choice. for example -
if user choose 'School of Economics' from drop down list, i'm showing in another drop down list just the relevant lanes (based on a mysql query).
to do this i have 5 div's, on for etch school:
<div id='a'>
<span><label>Lane</label></span>
<?php
$sql = "SELECT lane_name FROM lane WHERE `lane_school_id` = 1 ORDER BY `lane_name` ASC";
$result = mysql_query($sql);
echo "<select name='lane_name'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['lane_name'] ."'>" . $row['lane_name'] ."</option>";
}
echo "</select>";
?>
</div>
<div id='b'>
<span><label>Lane</label></span>
<?php
$sql = "SELECT lane_name FROM lane WHERE `lane_school_id` = 2 ORDER BY `lane_name` ASC";
$result = mysql_query($sql);
echo "<select name='lane_name'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['lane_name'] ."'>" . $row['lane_name'] ."</option>";
}
echo "</select>";
?>
</div>
i have a listener to show just the right 'lane' div when user chooses school and hide all other 'lane' div's:
$(document).bind('pageinit', '#indexPage', function(){
$("#a").show();
$("#b").hide();
$("#c").hide();
$("#d").hide();
$("#e").hide();
//this will call our toggleFields function every time the selection value of School field changes
$("#school").change(function () {
toggleFieldsA();
toggleFieldsB();
toggleFieldsC();
toggleFieldsD();
toggleFieldsE();
});
});
function toggleFieldsA() {
if ($("#school").val() == 'School of Economics'){
$("#a").show();
}
else
$("#a").hide();
}
function toggleFieldsB() {
if ($("#school").val() == 'School of Computer Science')
$("#b").show();
else
$("#b").hide();
}
the problem: when user submit the form, i get the wrong 'lane_name' from POST array. i'm getting the last school selected lane value (that is hidden from the user) and not the user selected lane name , plz help
You need to give each <select> element a unique name, otherwise the last element with a particular name will be the only one you can access. PHP has no way of determining which of the 5 lane_name you are referring to.
To figure out what option set was selected you could make the first option blank: <option value=""></option>. You can add some logic to reset to the blank option when you hide/show the DIVs so only one of the lane_names will have any data in it.
The mysql_* is depreciated as of PHP 5.5.0. You should consider switching to mysqli.