CodeCharge Studio
search Register Login  

Visual Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 Tricky Listbox problem (RESOLVED)

Print topic Send  topic

Author Message
saseow

Posts: 744
Posted: 07/22/2016, 10:37 PM

I somehow doubt that this is even possible but maybe...just maybe:
I have a record form with lots of controls but two are pertinent to this, a client listbox and a ticket type listbox. The client listbox contains all the client IDs and names in the database and the ticket type contains all the ticket type Ids and names in the database.

Now, there is a flag field in the client table (true or false) that must determine if one additional ticket type is displayed in the listbox or not. So, I need to get this flag value when the client listbox changes value and then change the ticket type listbox SQL and refresh it to either show or not show the additional ticket type. I cannot submit the form when the client listbox changes as there are many fields that are required.

Two obvious solutions would be to either make the user select a client before going to the form page or duplicating all the ticket types in the table for each client and then adding client IDs to all the ticket types then using dependent listboxes to display the ticket types. Neither of these are ideal.

So, that is the hassle. Hopefully some guru on this forum can come up with a solution.
View profile  Send private message
eratech


Posts: 513
Posted: 07/31/2016, 7:28 AM

I'll have to reread this tomorrow when not so tired. I like challenging problems.

Eric
_________________
CCS 3/4/5 ASP Classic, VB.NET, PHP
Melbourne, Victoria, Australia
View profile  Send private message
MichaelMcDonald

Posts: 640
Posted: 08/04/2016, 4:20 AM

Short answer use Jquery to grab the onchange event of the listbox, if listbox value = whatever criteria-> then ajax post the variable to a php page, use it in a php query, return the query result as json_encode, when it gets sent back to the referring page in the originating ajax post 'on success' append the result to the listbox.

It might sound tricky but it's not.
Play with these to get started..

This will get the text value from the listbox:
<script>
$(document).ready(function(){
$('#the_html_ID_of yourlistboxgoeshere').change(function(){
var listboxtext = $(this).find('option:selected').text();
Alert(listboxtext);
});
});
</script>

This will get the select value from the listbox:
<script>
$(document).ready(function(){
$('#the_html_ID_of yourlistboxgoeshere').change(function(){
var listboxvalue = $(this).find('option:selected').val();
alert(listboxvalue);
});
});
</script>
_________________
Central Coast, NSW, Australia.

View profile  Send private message
saseow

Posts: 744
Posted: 08/04/2016, 4:30 AM

Michael, thank you so much for this. I was pretty sure that I could get the first listbox value via jquery but the refresh of the second listbox had me stymied. I am close to useless as far as ajax is concerned unfortunately.
I have done it by making the user select the client before going to the edit page but your method will be so much better.
I will give it a try and see if I can get it to work. Will post here either way.
Thanks again Michael. Very much appreciated!
Trevor
View profile  Send private message
MichaelMcDonald

Posts: 640
Posted: 08/04/2016, 6:24 AM

Give me a few more details and I will start to put it together, I am a bit time poor at the moment, so be might be in dribs and drabs.

The only thing I would have to research I haven't done for a while is the append to the listbox after json_encode pass back.

Either way I can come up with the ajax post code pretty quickly so that at least in the on success of the ajax post, you can throw the returned json_encoded value to an alert box.

What is the value/s required from the listbox and what is the php query that should be run taking this into account and what is the value to be passed back to append the listbox?

And what is the id of the listbox or id's of the listboxes ?
_________________
Central Coast, NSW, Australia.

View profile  Send private message
saseow

Posts: 744
Posted: 08/04/2016, 7:57 AM

You really are kind Michael and I would appreciate any help. Here are the details:

The client listbox is:
Name: client_id
Data Source: clients (table)
Bound Column: ID (this is an incremental primary key)
Text Column: client_name
The html id of this is: id="Contentslas_ticketsclient_id"

The sql to run would be: "SELECT extended_support FROM clients"

if the extended support is 0 then the ticket_type list box source would not include the ID 5 and if the extended support were 1 then the ticket_type list box type would have all records.

Receiving list box:
Name: ticket_type
Data Source: st_ticket_types (table)
Bound Column: ID (this is an incremental primary key)
Text Column: ticket_type
The html id of this is: id="id="Contentslas_ticketsticket_type""

So, if a client has selected extended support, the ticket types would have all records but if they have not, the ticket type would not have the "Extended Support" record which is ID 5.
e.g.
If the client has Extended support:
SQL = "SELECT st_ticket_types.ID,st_ticket_types.ticket_type FROM st_ticket_types"
and if the client does not have extended support:
SQL = "SELECT st_ticket_types.ID,st_ticket_types.ticket_type FROM st_ticket_types WHERE st_ticket_types.ID <> 5"

The json return and ticket_type list=box refresh is what is getting to me. Thank you so much for your offer to help Michael!
View profile  Send private message
MichaelMcDonald

Posts: 640
Posted: 08/07/2016, 3:52 PM

Trev,

Have a play with this code, it's from a working project of mine.
I have a master listbox 'assetclassid' and dependent on it's value, send it via jquery POST to retrieve 'supplierid', and populate the 'supplierid' listbox/select with the values or array or returned values from a php query.

Here's the code that retrieves the selected listbox value, posts it to the php page and renders the passed back value via json_encode. These 2 lines says get the selected value, make it a variable to get ready to POST to PHP page for use in query:

$("#ModalWorkordersrecordWorkorderassetclassid").change(function(){

var assetclassid = $("#ModalWorkordersrecordWorkorderassetclassid").find('option:selected').val();


Appending the listbox/select with POSTED back json_encode array is this line:

$('#ModalWorkordersrecordWorkordersupplierid').html(options);

Ignore the fact that I am passing 3 values (there's a concatenation) to the SELECT, just use 2 if required.

here's the whole script:

<script>
$(document).ready(function() {
$("#ModalWorkordersrecordWorkorderassetclassid").change(function(){

var assetclassid = $("#ModalWorkordersrecordWorkorderassetclassid").find('option:selected').val();

if(assetclassid > 0){
$.ajax({
type: "POST",
url: "services/smlborderDLB.php",
data: {assetclassid : assetclassid },
dataType: "json",
success: function (data) {
var options = '';
var i;
for (var i = 0; i < data.length; i++) {
options += '<option selected value="' + data.idsupplier + '">' + data.displayorder +'.'+ data.displayname + '</option>';
}
$('#ModalWorkordersrecordWorkordersupplierid').html(options);
$("#ModalWorkordersrecordWorkordersupplierid").val('1');
}
});
}
if(assetclassid < 1){
$('#ModalWorkordersrecordWorkordersupplierid').html('');
}
});
});
</script>

And here's a php query that json-encodes the array ( php records the page the request was posted from and posts back the query result there)


php (in the before initialize event on it's own page)

$assetclassid = CCGetFromPost("assetclassid");
$unsetsupplierid = CCGetFromPost("unsetsupplierid");

if($unsetsupplierid == 1){
CCSetSession("supplierid","");
}


$siteid = CCGetSession("siteid");


if($siteid != NULL and $assetclassid > 0){


$db = new clsDBfm();

// initialise the array variable

$resultassetclassid = [];

$SQL = "SELECT supplier.idsupplier, supplier.displayname, suppliermasterlistbox.displayorder FROM suppliermasterlistbox LEFT JOIN supplier ON suppliermasterlistbox.supplierid = supplier.idsupplier"
." WHERE supplier.idsupplier > 0 and suppliermasterlistbox.assetclassid = '$assetclassid' and suppliermasterlistbox.siteid > 0 and suppliermasterlistbox.siteid = '$siteid' ORDER BY suppliermasterlistbox.displayorder ASC";

$db->query($SQL);


// this bit here populates the array

while($db->next_record()){

array_push($resultassetclassid, [
idsupplier => $db->f('idsupplier') ,
displayorder => $db->f('displayorder'),
displayname => $db->f('displayname'),
]);
}

// This bit here POSTS back the json_encode array

if(! empty($resultassetclassid)){
echo json_encode($resultassetclassid);
}

// In my project, I have an empty select/list box- pretty must just the SELECT tags, so if the query returns no results, I throw it this to give it the value="0" select stuff:


if(empty($resultassetclassid)){
array_push($resultassetclassid, [
idsupplier => '0' ,
displayname => 'Select'
]);
echo json_encode($resultassetclassid);
}


$db->close();
}


heres my html listbox/select:

<!-- BEGIN ListBox supplierid -->
<select id="ModalAssetrecordAssetsupplierid" name="{supplierid_Name}">
<option selected value="">Select</option>
{supplierid_Options}
</select>
<!-- END ListBox supplierid -->



Any questions, feel free to ask. There is more code here than you require, but the basic pattern should be deciperable.




footnote: you will lose this value in any on validate event, but from reading your requirements this component does not attract any validation.

I have 2 more scripts that I use to manage this solution in an on validate event.
_________________
Central Coast, NSW, Australia.

View profile  Send private message
saseow

Posts: 744
Posted: 08/07/2016, 9:48 PM

Thank you so much Michael. I will have a look at this over the next couple of days and let you know how it goes.
Really appreciate what you have done!
View profile  Send private message
solesz

Posts: 137
Posted: 08/08/2016, 4:31 AM

I would made a dependent listbox, using built in feature generator.
- In the wizard put the master and slave as usual, specify the dependent field, probaly client_id etc.
- let CCS generate the service page, you can find it in the services subfolder within the project folder
- (remark: during runtime the request will send the client_id in the url, as a querystring to this service page)
- open the service for editing
- you will find a very simplified Grid on that page
- now select the Grid and go to Events of that grid, then define a BeforeShow event->code.
- write a code to load the passed client_id from querystring, then call a dlookup function to get the Flag value from Client database.
- upon the Flag value change the Grid's datasource.
....
Imoprtant:
Either way you solve the listbox element problem imho you need to check upon inserting of the record if the client has right to use the special ticket type. It prevents tricky users to change the listbox elements on client side in the DOM.
View profile  Send private message
saseow

Posts: 744
Posted: 08/08/2016, 4:37 AM

Interesting approach solesz!
Now have two possibilities to look at. :)
Thank you very much!
View profile  Send private message
saseow

Posts: 744
Posted: 08/08/2016, 10:01 PM

OK, naturally I tried the easiest approach first and solesz, your method worked just fine. A few changes. The client ID I got via the URL parameter 'keyword' and removed this from the where clause in the service page grid. The code to check the flag in the client table and to alter the simple grid's SQL I put into the BeforeBuildSelect as it did not work in the BeforeShow event.

MIchael and solesz, thank you both for your valuable input. MIchael, I am still going to try out your code as I can see that it having some really good uses down the line.

Once again, thank you both so very much!
View profile  Send private message
solesz

Posts: 137
Posted: 08/09/2016, 7:35 AM

Thanks for the correction, you allright, it is much better to use the BeforeBuildSelect.
View profile  Send private message
saseow

Posts: 744
Posted: 08/09/2016, 7:47 AM

Forget those small things solesz, your method does work, is simple and will do the job nicely. Thank you so much for the idea.
View profile  Send private message
girongancev

Posts: 2
Posted: 10/01/2016, 3:13 AM

Short answer use Jquery to grab the onchange event of the listbox, if listbox value = whatever criteria-> then ajax post the variable to a php page, use it in a php query, return the query result as json_encode, when it gets sent back to the referring page in the originating ajax post 'on success' append the result to the listbox.

http://taruhanjudibolavipbet88.com | http://dewabet.com
_________________
http://dewabet.asia | http://agenbola1.com | http://bolalive77.com
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.