to get value from database table into select box - php

i m creating a combo box which gets value from mysql database table.
here is a sample code which i m implementing but it will not populates selectbox values.
mydata +='<div class="content nodisplay"><div class="row"><div class="label" style="font-size:22px;">Subject</div><div class="data">
<select id="fillsubject" name="fillsubject">';
$.post(document.URL,qstring,function(data){
var subjects = $(data).filter('.subjects');
$.each(subjects,function(index,value){
var subid = $(this).attr('id');
var subname = $(this).text();
mydata += "<option value='"+subid+"'>"+subname+"</option>";
//mydata += "<option value='english'>english</option>";
});
});
mydata +='</select></div></div></div>';

var mydata +='<div class="content nodisplay"><div class="row"><div class="label" style="font-size:22px;">Subject</div><div class="data"><select id="fillsubject" name="fillsubject"></select></div></div></div>';
$.post(document.URL,qstring,function(data){
var subjects = $(data).filter('.subjects');
var subopts = '';
$.each(subjects,function(index,value){
var subid = $(this).attr('id');
var subname = $(this).text();
subopts += "<option value='"+subid+"'>"+subname+"</option>";
});
//after the loop is over building your <options/> inject the html
$('#fillsubject').html(subopts);
});
You are splitting the <select></select> that's not necessary. You can do it as above.

How does the code sample fit into the rest of your source code?
Rather than using
mydata += "<option value='"+subid+"'>"+subname+"</option>";
in your .each function, you would be better off using
$("#fillsubject").append("<option value='"+subid+"'>"+subname+"</option>");
I don't know what you're doing with mydata after the code sample you post, but since .post is asynchronous, mydata may have already been displayed to the user when your js gets the reply from your server. If this is the case, then the suggestion above will work as expected.

Related

showing php hidden rows when passed to modal

I have an HTML table that has rows retrieved from database.
using tbody tag I hid some rows to show them in a modal and do some editing if necessary.
the modal works perfectly with editing in database and all but the data that supposed to be displayed for editing are hidden as well!
how to fix this?
here is my table with hidden rows code which are inside a while statement:
<tbody style="display:none">
<td data-target="FLOOR"><?php echo $row['FLOOR'];?></td>
<td data-target="ORDER_OFFICE"><?php echo $row['ORDER_OFFICE'];?></td>
<td data-target="SERIAL_NO"><?php echo $row['SERIAL_NO'];?></td>
<td data-target="POINT_NO"><?php echo $row['POINT_NO'];?></td>
<td data-target="NOTE"><?php echo $row['NOTE'];?></td>
<td data-target="OTHER_NAME"><?php echo $row['OTHER_NAME'];?></td>
<td data-target="ORDER_TELE"><?php echo $row['ORDER_TELE'];?></td>
</tbody>
<script>
// append values in input fields
$(document).on('click','a[data-role=update]',function(){
var id = $(this).data('id');
var FLOOR = $('#'+id).children('td[data-target=FLOOR]').text();
var ORDER_OFFICE = $('#'+id).children('td[data-target=ORDER_OFFICE]').text();
var SERIAL_NO = $('#'+id).children('td[data-target=SERIAL_NO]').text();
var POINT_NO = $('#'+id).children('td[data-target=POINT_NO]').text();
var NOTE = $('#'+id).children('td[data-target=NOTE]').text();
var OTHER_NAME = $('#'+id).children('td[data-target=OTHER_NAME]').text();
var ORDER_TELE = $('#'+id).children('td[data-target=ORDER_TELE]').text();
$('#FLOOR').val(FLOOR);
$('#ORDER_OFFICE').val(ORDER_OFFICE);
$('#SERIAL_NO').val(SERIAL_NO);
$('#POINT_NO').val(POINT_NO);
$('#NOTE').val(NOTE);
$('#OTHER_NAME').val(OTHER_NAME);
$('#ORDER_TELE').val(ORDER_TELE);
$('#userId').val(id);
$('#myModal3').modal({backdrop: "static"});
});
// now create event to get data from fields and update in database
$('#save').click(function(){
var ID = $('#userId').val();
var FLOOR = $('#FLOOR').val();
var ORDER_OFFICE = $('#ORDER_OFFICE').val();
var SERIAL_NO = $('#SERIAL_NO').val();
var POINT_NO = $('#POINT_NO').val();
var NOTE = $('#NOTE').val();
var OTHER_NAME = $('#OTHER_NAME').val();
var ORDER_TELE = $('#ORDER_TELE').val();
$.ajax({
url : 'edit_order.php',
method : 'post',
data : {FLOOR : FLOOR, ORDER_OFFICE : ORDER_OFFICE, SERIAL_NO : SERIAL_NO, POINT_NO : POINT_NO
,NOTE : NOTE, OTHER_NAME : OTHER_NAME, ORDER_TELE: ORDER_TELE, ID: ID},
success : function(response){
// now update user record in table
$('#'+id).children('td[data-target=FLOOR]').text(FLOOR);
$('#'+id).children('td[data-target=ORDER_OFFICE]').text(ORDER_OFFICE);
$('#'+id).children('td[data-target=SERIAL_NO]').text(SERIAL_NO);
$('#'+id).children('td[data-target=POINT_NO]').text(POINT_NO);
$('#'+id).children('td[data-target=NOTE]').text(NOTE);
$('#'+id).children('td[data-target=OTHER_NAME]').text(OTHER_NAME);
$('#'+id).children('td[data-target=ORDER_TELE]').text(ORDER_TELE);
$('#myModal3').modal('toggle');
}
});
});
});
</script>
thank you.
If you want to show this table for editing just delete style="display:none" from tbody tag by using javascript after you rendered page or if the condition you wait turn to true. I do not recommend to use style directly inside of tag if you want to change it conditionally. You can do it by using javascript.
Or you can check if hide or not when rendering on PHP side.
<tbody style="<?php echo $ifConditionTrue ? "display:none" : "";?>">'
Or short way
<tbody style="<?= $ifConditionTrue ? "display:none" : "";?>">'

pass variable into string in symfony and jquery

I want to pass the id into a string but always I get an error
var c = document.getElementById("predefinedMessage");
var id= c.selectedIndex;
var text= "{{ form.predefinedMessage.vars.choices[id].data.message }}";
$('message').val(text);
});
how I can pass the id in the text variable
If the element that you're selecting is a drop down list, I suggest the following code using jQuery as you're already using it:
// Select by the ID predefinedMessage
var dropDown = $("select#predefinedMessage");
// Get the selected index from the dropdown
var selectedIndex = dropDown.find("option:selected").index();
var text = "{{ form.predefinedMessage.vars.choices[" + selectedIndex + "].data.message }}";
$('message').val(text);

Display data from DB in dynamically generated select box

I have the code that dynamically generates textboxes and select boxes upon a button click. I want to fetch the data from DB and display in the dynamically generated select box.
Fiddle http://jsfiddle.net/hEByw/11/ shows how the text and selectboxes are generated dynamically.
I have tried the following part of code to fetch the data from DB and Put into dynamically generated select box(Tax Type) but its not working for me.
//To Display the tax types from DB
$(function(){
var items="";
$.getJSON("get_tax_type.php",function(data){
$.each(data,function(index,item)
{
items+="<option value='"+item.id+"'>"+item.name+"</option>";
});
$("#tax_type' + counter + '").html(items);
});
});
I feel the way I am doing is wrong.Can anybody suggest where am I doing wrong or the proper way of implementing it. I am newbie in jquery. Any help is appreciated.Thanks in advance.
PHP Code(get_tax_type.php)
<?php
include('includes/db.php');
$q = "select TaxID, TaxName from tax";
$sql = mysql_query($q);
$data = array();
while($row = mysql_fetch_array($sql, true)){
$data[] = $row;
};
echo json_encode($data);
?>
Try this. UPDATED
$(document).ready(function() {
var items = "";
$.getJSON("get_tax_type.php", function(data) {
alert(data);
$.each(data, function(index, item) {
$("#tax_type" + parseInt(index) + parseInt(1)).empty();
$("#tax_type" + parseInt(index) + parseInt(1)).append("<option value='" + item.TaxID+ "'>" + item.TaxName+ "</option>");
});
}, 'json');
});

Get just one of a divs IDS with jQuery

I have a div with more than one ID and I need to get each ID separately and send both variables off to a PHP file. I feel this would be the simpler option if achievable but the other option is to split the two IDS on the server side with PHP once the ID has been sent to it by jQuery. One ID is the timestamp the other the ID in the database.
HTML div example:
<div style="margin-bottom:10px;" id="1000003 2012-09-04 21:24:32" class="newsItem"></div>
The 1000003 is the ID i need to get into one separate variable and the rest is the timestamp I need in another separate variable.
The jQuery I have:
window.setInterval(function(){
//scripting here
var obj = $('.newsItem:first').map(function(_, elem){
return elem.id;
});
var ids = $.makeArray(obj); //variable of IDS
$.get("AJAX/get_feed_updates.php",{ ids: ids },function(result){
$("#newsFeed").html(result);
});
}, 5000);
The PHP file just puts the ID into a variable and queries the database with it.
Store one in the DATA attribute. Here is another reference to how its used.
Is this right?
window.setInterval(function(){
//scripting here
var theId = $('.newsItem:first').attr('id');
var a = theId.split(' ');
var theId = a[0];
var theStamp = a[1]+''+a[2];
$.get("AJAX/get_feed_updates.php",{ ids: theId },function(result){
$("#newsFeed").html(result);
});
}, 5000);
You should use data attribute. Then you can do something like this..
<div data-timestamp="1000003 2012-09-04 21:24:32" id="myId"></div>

Displaying data as a table using $.ajax() and jQuery

I'm new to jQuery and PHP.
I'm developing a web page to display a set of records taken from a database. Here I have used PHP and jQuery.
I need to display a set of records as a table. Data is retrieved from a MySQL database using php. Set of rows is passed to the html page as a string using json_encode().
The problem is that I can't display those data in a table row by row. I'm using a table created with <div>. So I need to know how to display this string of data row by row and separating the value for each column.
Here is what I have done to display only one row, but the data is not displayed as a table. No compile error either. I need help to extend this to display multiple rows too.
demo.html (page I'm going to display the records):
<div class="table">
<div class="headRow">
<div class="cell">ID</div>
<div class="cell">First Name</div>
<div class="cell">Last Name</div>
<div class="cell">Age</div>
<div class="cell">Class</div>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: 'beta.php' ,
data:"",
dataType: 'json',
success:function(data){
var elementArray = new Array(); //creating the array
elementArray = data.split(""); //splitting the string which was passed using json_encode()
var id = elementArray[0]; //passing values corresponding to the columns
var fname = elementArray[1];
var lname = elementArray[2];
var age = elementArray[3];
var grade = elementArray[4];
$("<div>", { //creating a new div element and assiging the value and appending it to the column 1
"class":"cell",
"text":id
})
.appendTo("document.body");
$("<div>", { //cloumn 2 value
"class":"cell",
"text":fnam
})
.appendTo("document.body");
$("<div>", { //cloumn 3 value
"class":"cell",
"text":lname
})
.appendTo("document.body");
$("<div>", { //cloumn 4 value
"class":"cell",
"text":age
})
.appendTo("document.body");
$("<div>", { //cloumn 5 value
"class":"cell",
"text":grade
})
.appendTo("document.body");
}
});
});
</script>
</div>
</div>
demo.php (retrieving data from the database):
$result = mysql_query("SELECT * FROM student WHERE StuId=1",$con) or die (mysql_error());
$resultArray = mysql_fetch_row($result);
echo json_encode($value);
If someone can help me it would be a great.
try this one....
var id = elementArray[0];
var fname = elementArray[1];
var lname = elementArray[2];
var age = elementArray[3];
var grade = elementArray[4];
then create table using these values something like this....
$("<table>").appendTo("document.body");
$("table").html("<tr><td>"+id +"</td><td>"+fname +"</td><td>"+lname +"</td><td>"+age +"</td><td>"+grade +"</td></tr>);
Here is a Jfiddle, if you use .html and append to a empty table on your page you can append the var contents returned from your ajax query just the same
http://jsfiddle.net/L4G79/1/
this your code will look something like
var id = elementArray[0]; //passing values corresponding to the columns
var fname = elementArray[1];
var lname = elementArray[2];
var age = elementArray[3];
var grade = elementArray[4];
$("table").html("<tr><td>"+id +"</td><td>"+fname +"</td><td>"+lname +"</td><td>"+age +"</td><td>"+grade +"</td></tr>");

Categories