I am using php and getting data from mysql. I would like to have a dropdown of countries and then when the country is selected then the prefix must be the result either in a text box on the same line or just below the dropdown box.
so far my code gives me the prefix concatenated with prefix eg
Zimbabwe-263
here is the code
<?php
include 'config.php';
$query="SELECT countryname, countryprefix FROM cc_country";
$result = mysql_query($query);
$options="";
echo "<select name='processor' value=''>
<option>Select A Country</option>";
while($nt=mysql_fetch_array($result))
{
echo "<option value='".$nt['countryprefix']."'>".$nt['countryname']."-".$nt['countryprefix']."</option>";
}
If you want the selected value to display in textbox you can use jquery for that.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<sctipt>
$('select').change(function(){
var value=$(this).val();
$('#text').val(value);
});
</script>
Create a textbox with id text for this
You will need to fire another query that will fetch the country prefix on selection of the country.That is pass the value of your select box to he sql query.You can do this using jquery or javascript by giving an id to the select box you are using .
var countryname = $('#yourId').val();
then do a ajax or jquery post to a PHP file which will have the blow query.
For example:
$query="SELECT countryprefix FROM cc_country where countryname='your post value";
then below your while loop add a text field which will display the value returned by your above query
$('select').change(function(){
var value = $(this).val();
$('.textbox').val(value); // html <input type="text" class="textbox">
});
Related
<select id="postnr" name="post_postnr" onChange="updatePlace()">
<?php do { ?>
<option value="<?php echo $row_postnr['postnr'] ?>" ><?php echo $row_postnr['postnr']?></option>
<?php } while ($row_postnr = mysql_fetch_assoc($postnr));?>
</select> <div id="place" style="display:inline"></div>
When user changes value in dropdown, I want to search a database (same table) for the corresponding place and paste it into innerHTML of #place.
The values for the dropdown is collected from a SQL table, column postnr.
postnr place
-----------------
1234 Place1
3456 Place2
and so on...
So when 1234 is selected, Place1 should pop up next to it.
Very basic question about the use of AJAX.
updatePlace() should be a Javascript method that will make an AJAX request to a specific PHP page. This page have to connect to the database and get all needed values.
Take a look at this sample to have a clearer idea.
Hope that helps
If you are using jQuery, you can use the following (the way to do it is the same without jQuery):
<script type="text/javascript">
$("#postnr").on('change', function(){
$.get('get_place.php', {postnr: postnr}, function(data){
$("#place").html(data);
});
});
</script>
Then create a *get_place.php* file with something like the following:
$query = mysql_query("SELECT * FROM places WHERE postnr='".mysql_real_escape_string($_GET['postnr'])."'");
$row = mysql_fetch_assoc($query);
echo $row['place'];
I have two fields, one of which has the jQuery autoComplete plugin.
<input type='text' name='primary_diagnosis' id='icd1-diagnosis'/>
<input type='text' name='ICD_No1' id='icd1-num' />
$(document).ready(function(){
$("#icd1-diagnosis").autocomplete("autocomplete-icd.php",
{
selectFirst: true
});
});
Is there a way to automatically fill up the second text input once a value has been selected for the first? For example, selecting "Dengue fever [classical dengue]" for the first text input, then query the database for certain value to yeild "A90" as the value for the second text input.
Here's autocomplete-icd.php
<?php
include('config.php');
$q=$_GET['q'];
$my_data=$q;
$sql=mysql_query("SELECT col9 FROM tb_data_icd WHERE col9 LIKE '%$my_data%' ORDER BY col9",$con);
if($sql)
{
while($row=mysql_fetch_array($sql))
{
$col9=$row['col9'];
echo "$col9"."\n";
}
}
?>
You should be able to use the change event to fire off some javascript to update the second input's value. Something like this:
$("#icd1-diagnosis").change(function(){
//retrieve your value
var val = 'sample';
$('#icd1-num').val(val);
});
Im having two pages search.php and load.php...in search.php i have checkboxes and im using jquery for passing checkboxes to load.php
<div class="checkBoxes">
<input type="checkbox" name="art[]" value="1" >Digital Art <br />
<input type="checkbox" name="art[]" value="2" >Traditional Art <br />
<input type="checkbox" name="art[]" value="3">Photography <br />
</div>
<script language="javascript">
$(document).ready(function(e) {
$(".btn_advancedsearch").click(function(){
$('#content').fadeIn(1500);
$("#content").load("/search/advancedsearch.php?type="+ $("[name='type']:checked").val()+"&category="+$("input[type='checkbox']").val());
window.scroll(0,0);
});
});
</script>
In load.php im using $category=$_REQUEST['category']; and now i want to write query for getting all selected category from checkboxes but i dont know how...i tryed this query but its not working $sql = "SELECT * FROM art WHERE categoryID IN (implode(',', $category))";
This is because $("[name='type']:checked").val() will return you array object and you cannot pass this directly as a query string parameter. Do the following steps
1. Fetch the selected checkbox values and make them comma separated in a variable (with single quote so use can directly use in the query) _eg. '1','2','3'
2. Pass this value via query string
3. Use this value in your select query -- As you're using in operator so you can directly feed this value as select * from table name where col in (valueYouReceived)
Code:
Somewhere in JS -- When you click on the button $(".btn_advancedsearch").click(function(){
...
...
var selectedValues = "'";
$('input[type=checkbox]').each(function() {
if($(this).prop('checked'))
{
selectedValues += $(this).val()+"',";
}
});
selectedValues = selectedValues.substring(0, selectedValues.length-1); // To remove last comma (,)
Just "alert()" the value and see what you get alert(selectedValues)
Now Pass this variable as a query parameter
$("#content").load("/search/advancedsearch.php?type="+selectedValues+"&category="+$("input[type='checkbox']").val());
At the php part get the value as you're getting it
$category=$_REQUEST['category'];
Use this value in your sql query
"SELECT * FROM art WHERE categoryID IN ($category)";
Hope this helps!!
Ok, found solution. I'm not sure for code but you can change and some put this:
jQuery(document).ready(function($) {
$(":checkbox").bind("click", function(event) {
if ($(this).is(':checked')) {
var checked = $(this).val();
$(".btn_advancedsearch").hide();
$("#content").load('/search/advancedsearch.php?type=' + checked);
$('#content').fadeIn(1500);
window.scroll(0,0);
}
});
});
So, here's the deal. I have an html table that I want to populate. Specificaly the first row is the one that is filled with elements from a mysql database. To be exact, the table is a questionnaire about mobile phones. The first row is the header where the cellphone names are loaded from the database. There is also a select tag that has company names as options in it. I need to trigger an onChange event on the select tag to reload the page and refill the first row with the new names of mobiles from the company that is currently selected in the dropdown list. This is what my select almost looks like:
<select name="select" class="companies" onChange="reloadPageWithNewElements()">
<?php
$sql = "SELECT cname FROM companies;";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs)) {
echo "<option value=\"".$row['cname']."\">".$row['cname']."</option>\n ";
}
?>
</select>
So... is there a way to refresh this page with onChange and pass the selected value to the same page again and assign it in a new php variable so i can do the query i need to fill my table?
<?php
//$mobileCompanies = $_GET["selectedValue"];
$sql = "SELECT mname FROM ".$mobileCompanies.";";
$rs = mysql_query($sql);
while ($row = mysql_fetch_array($rs)) {
echo "<td><div class=\"q1\">".$row['mname']."</div></td>";
}
?>
something like this. (The reloadPageWithNewElements() and selectedValue are just an idea for now)
Save the value in a hidden input :
<input type='hidden' value='<?php echo $row['cname'] ?>' id='someId' />
in your JavaScript function use the value from this hidden input field:
function reloadPageWithNewElements() {
var selectedValue = document.getElementById('someId').value;
// refresh page and send value as param
window.location.href = window.location + '?someVal='+ selectedValue;
}
Now again in your PHP file retrieve this value from url for use as:
$someVal = null;
if (isset($_GET['someVal']) {
$someVal = $_GET['someVal'];
}
see if this works!!!
The best option would be using AJAX.
reloadPageWithNewElements() is a function which calls a page of your own site which will return the data you would like to put in your table.
If you are using JQuery, AJAX is very easy to implement:
$.ajax({
url: '/yourPage',
data: { selectedCompany: $('.companies').val() },
success: function(result) {
//delete your tablerows
$(".classOfTable tr").remove();
//put the result in your html table e.g.
$('.classOfTable').append(result);
},
dataType: html
});
The browser will send a request to "/yourPage?selectedCompany=Google" or something
All you have to do is let this page print out only html (maybe even easier is to print only the tablerow (<tr>).
If you have any further questions, please ask.
I would use jQuery to do it.
first You need to add 'id' attribute to every option tag
<option id="option1">
<option id="option2">
and so on...
then with jQuery:
$('<option>').change(function() {
var id=$(this).attr('id');
...save data here (i.e: with ajax $.post(url, { selected_id: id}, callback }
});
In Dreamweaver I have a list box or menu/list.
I am dynamically adding data from an array into it, so when the page loads, I have a list-box with names in.
This is how it looks
<?php
echo "<select name="."username"."id="."username".">";
foreach ($user_array as $arr) {
echo "<option>$arr</option>";
}
echo "</select>";
?>
Now how do I go about making the listbox executesomething when the user selects something in it?
Because I have three other textboxes, and when the user selects a name in the list box, I want to put that name he selected into a variable (also don't know how I'm going to do that with a list box) and then search through my database and insert some of the data of that person in the textboxes.
So all i need to know is:
How to create that on event click event and
How to put that selected value then in a variable (inside the event)
You can easily execute a javascript function by using something like this:
<select name=".$username."id=".$username." onchange='yourfunction()' >
The following php code generated a select field with a dynamic number of options.
<?php
$user_array = Array("Smith", "John", "Bob", "Jake");
echo "<select name="."username"." id="."username".">";
foreach ($user_array as $arr) {
echo "<option>$arr</option>";
}
echo "</select>";
?>
The following JavaScript handles change events on the select element.
$(document).ready(function() {
var username = $('#username');
var selectedValue = null;
username.change(function(){
alert(username.val());
selectedValue = username.val();
});
});