My idea is to get the complete address about the user with a select element, actually I have a variable that has all the addresses $all = (array)$this->_user->getAddressCollection(); and if i make a tiny script only to get the response, the response looks like below:
I use that variable in a foreach to get the id of the address and the addresses name:
<select class="addrs">
<?
foreach($all as $addre => $info):
?>
<option value="<?php echo $info->getIdCustomerAddress(); ?>">
<?php echo $info->getAddress1() . ' ' . $info->getAddress2() . ' - ' .
$info->getAddress3(); ?>
</option>
<?php endforeach; ?>
</select>`
Since here, I dont know how to fill a form when user select one of the addresses are display on the select
Related
In a drop down select field I pull several options based on a mysqli query
Once I select them, I use javascript to pick up the variables and infill form fields
Once I select the desired option, a form field infills with a reference number
I want to use that reference number for another select field to query the DB for a separate list
Here is the select to get the ref #
<select id="cdetail" name="cdetail"><option value="">Select</option>
<?
$mlo="SELECT * from client WHERE loan_originator ='$uname'";
$mlo_res = mysqli_query($dbc, $mlo);
while ($row = $mlo_res->fetch_assoc()){
echo "<option value=" . $row['filename'] . ">Filename: " . $row['filename'] . "</option>";
?>
</select>
Once I make the choice from the client table, I will see the filename in the 'cdetail' form field
I want to add another select field to pull from another table based on the selection in the above select option
<select id="ndetail" name="ndetail"><option value="">Select</option>
<?
$filename = $row['filename'];
$quote1="SELECT * from nclient WHERE filename ='$filename'";
$quote_res = mysqli_query($dbc, $quote1);
while ($row1 = $quote_res->fetch_assoc()){
echo "<option value=". $row1['ln_amt'] . ">Loan Amt: " . $row1['filename'] . "</option>";
?>
</select>
Trying to pull the filename from the 1st query and creating a global variable did not work the way I coded it.
I was able to use javascript to push the value into a separate field:
<script>
$('#cdetail').on('change', function () {
var x = $('#cdetail option:selected').val()
$('#flnm').val(x);
});
</script>
This will populate (on change) the flnm field with the filename, but I have not been able to grab that input and convert it into a php variable
i.e.
<? $filename = $('#flnm').val(x); ?>
or
<? $filename = ('#flnm').val(x); ?>
or
<? $filename = val(x); ?>
or
<? $filename = flnm; ?>
Short answer: No, you can't pass variables from javascript to php like that.
But you can do it the other way around (passing php variable to javascript in a php file):
<script>
var variable = '<?= $phpVariable ?>';
</script>
What you can do, is to use AJAX requests to pass the javascript (client side) variables to php (server side).
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";
}
?>
UPDATE
I'm working with codeigniter, and i have simple select option like this:
<select id="field_' + count + '" name="fields[]' + '" class="form-control no_inv" >
<?php
$jj = "<script>var e = document.getElementById('id_barang').value;document.write(e);</script>";
$noInv = $this->modelku->select_inv($jj);
?>
<option value="" selected="selected" disabled>Pilih no inventaris</option>
<?php
foreach($noInv->result() as $inv){ ?>
<option value="<?php echo $inv->no_inv ?>">
<?php echo $inv->no_inv ?>
</option><?php }
?>
</select><br>
And this is my html element with id = id_barang:
<select name="id_barang" id="id_barang" class="form-control">
<?php $idBarang = $this->modelku->select_idBrang() ?>
<?php foreach($idBarang->result() as $idBr){ ?>
<option value="<?php echo $idBr->id_barang ?>"><?php echo $idBr->id_barang ?></option>
<?php } ?>
</select required>
select_inv function from modelku:
public function select_inv($idbrang)
{
$this->db->select("no_inv");
$this->db->from('detail_barang');
$this->db->where('kondisi', 'Ada');
$this->db->where('id_barang ', $idbrang);
$query = $this->db->get();
return $query;
}
But when i click the select option, the value from no_inv doesn't appear in my select option?
Can someone help me pls?
You can't assign a value to a PHP variable by using javascript, since they have different times for execution, and when you use the script tag, unless the browser loads it, it will be only a PHP string. I think you'll need to change this to use a PHP value for the assignment.
the problem lies in using a variable $idbrang in the manual written where clause. To use a variable change the line
$this->db->where("kondisi = 'Ada' AND id_barang = '$idbrang' ");
to
$this->db->where('kondisi', 'Ada');
$this->db->where('id_barang ', $idbrang);
more info here
note: I gave this answer before the OP was completely remodeled
You can't do this:
<select id="field_' + count + '" name="fields[]' + '" class="form-control no_inv" >
<?php
$jj = "<script>var e = document.getElementById('id_barang').value;document.write(e);</script>";
$noInv = $this->modelku->select_inv($jj);
?>
<!-- // ... -->
</select>
Because the content of your PHP $jj variable is plain text, it will not select your #id_barang value. $jj will returns exactly what you put into quotes.
One way to do what you expect is using Ajax to pass javascript values to a PHP file using POST/GET methods. Here are more details.
This : id="field_' + count + '" name="fields[]' + '" will not work too because you're using a javascript variable into HTML. You can set your select id using plain javascript into a <script> tag.
I am showing the dropdowns based on the above selected dropdowns. I want the result in third dropdown. For that I am writing the sql query in php and writing the change event in jquery but i am unable to get the result. I am stuck up there
My jquery looks like
$(document).ready(function(){
$("#parent_cat,#city").change(function(){
$.get('loadlocation.php?city=' + $(this).val() , function(data) {
$("#sub_cat").html(data);
});
});
});
parent_cat and city are from selected values
<label for="category">Category</label>
<select name="parent_cat" id="parent_cat">
<?php while($row = mysql_fetch_array($query_parent)): ?>
<option value="<?php echo $row['name']; ?>"><?php echo $row['name']; ?></option>
<?php endwhile; ?>
</select>
<br/><br/>
<label for="city">city</label>
<select name="city" id="city">
<?php while($row = mysql_fetch_array($query_parent1)): ?>
<option value="<?php echo $row['city']; ?>"><?php echo $row['city']; ?></option>
<?php endwhile; ?>
</select>
<br/><br/>
And my php file loadlocation.php is
<?php
include('config.php');
$parent_cat = $_GET['parent_cat'];
$city = $_GET['city'];
$query = mysql_query("SELECT table_place_detail.post_title FROM table_terms, table_place_detail, table_post_locations
WHERE table_place_detail.post_location_id = table_post_locations.location_id AND table_place_detail.default_category = table_terms.term_id AND table_post_locations.city = '$city' AND table_terms.name = '$parent_cat'");
while($row = mysql_fetch_array($query)) {
echo "<option value='$row[post_title]'>$row[post_title]</option>";
}
?>
I want to fetch the values of parent_cat, city to loadlocation.php but i am not able to get those values. I want to load the two values and get the query excecuted and the values should shown in 3rd dropdown as below can any one help this issue
<label>Vendors List 1</label>
<select name="sub_cat" id="sub_cat"></select>
Two things stand out
You send only one value, ?city=
According to the manual jQuery.get(), you can send additional parameters as a plain object. This means, you don't need to build a query string, but can pass parent_cat and city separately, e.g.
$.get("loadlocation.php",
{ parent_cat: $('#parent_cat').val(), city: $('#city').val() },
function(data) {
$('#sub_cat').html(data);
});
And finally, the mandatory hint at each mysql_* page
Warning This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
mysqli_query()
PDO::query()
I don't know much about Javascript and Jquery, but I do have to use it on a HTML select tags. I have two database tables, grouptypes and groups. A grouptype can have multiple groups, but a group only belongs to one grouptype. I have two select tags, one is grouptypes and another one is groups. What I want to do is that whenever a user select a grouptype in the grouptypes dropdown menu, it triggers a handler to retrieve the corresponding groups that belong to that grouptype. I need javascript or Jquery to get it done, but I am not able to write it myself. So anyone can help me? I would so appreciate it.
GroupType: <select name="groupType">
<?php foreach($grouptypes as $type) : ?>
<?php echo "<option value='" . $type['name'] . "'>" .$type['name']. "</option>"; ?>
<?php endforeach ?>
</select><br />
Group: <select name="groups">
<?php foreach($groups as $group) : ?>
<?php echo "<option value='" . $group['name'] . "'>" .$group['name']. "</option>"; ?>
<?php endforeach ?>
</select><br />
<?php
$query = "SELECT id, name FROM group_type";
$grouptypes = $_db->getResultsForquery($query);
$query = "SELECT id, name FROM groups";
$groups = $_db->getResultsForquery($query);
?>
Here's what I would do. It uses jquery to send a get to a page called groups.php which will return some html you can parse. Then you parse it :)
$('#grouptype-select').change(function(){ //.change will detect when the select has been changed (an -option- has been chosen)
var grouptype = $(this).val(); //create a variable from the -option- chosen
$.get('groups.php', 'grouptype=' + grouptype, function(response){ //use jquery to send -get- to groups.php
$('#list-of-groups').html(response); //insert response into ur doc
}
});
Something like http://www.yoursite.com?grouptype=abc will be sent using get asynchronously (ajax) Good luck!
PS Here is a jsfiddle to get you tinkering: http://jsfiddle.net/yLyTw/1/