Letting PHP select an option in real time? - php

I'm building a CMS that have 2 database tables, subjects & pages ... I have two drop down lists..And i want to let the user to insert values in it ...
The first drop down list is to select which table to insert in. The second drop down list is to select the position of the inserted item in that selected table.
So I want the user to select the table first, then -using PHP- change the second drop down list to contain the available position in that table. Note that all the previous stuff is in the same web page without pressing any button or something.
So all I want, a method to make PHP check the selected option in the first drop down list, and change the options in the second according to the first drop down list, all that in the same page without pressing any button..
Is that possible in PHP ?

You can use PHP in the background, but as selecting an option happens on the client side you'll have to use a combination of JavaScript/Ajax.
Have a look at YUI Autocomplete or other Ajax autocomplete frameworks.

The short answer to your question is no. PHP is server side, i.e. it can't be changed by user interactions without refreshing the page as it has finished executing before the HTML reaches the browser.
To do this you will need to use javascript and ajax.

Related

mysql - How to send a query back to a database when I button a submit button is clicked

I have a simple query such as
SELECT * FROM COLOUR WHERE ID = '1'
when someone clicks a submit button I would like the information that is displayed from this query to save to another table within a database, is this possible?
It is possible, but it is perhaps the basis of the question which you are missing. You have tagged this question with the "PHP" Tag, leading me to believe that you are trying to submit the information gathered from a page into another table.
In PHP Forms (read about them here), the submit buttons call to a php file on your server, and you can basically run whatever you want from there assuming you have access. If it is a flat query, you might consider running a:
SELECT * FROM COLOUR WHERE ID = '1'
INTO `TABLE_2`;

Update select contents without reloading form

I have quite a long form which has many HTML form selects pulling data from MySQL tables - quite often a client will be mid way through filling in the form when they realise that the value they want for department, for example, has not yet been entered into the system so is not in the list.
I have added a link to a simplified popup for adding departments but after it is added it does not appear in the select list as the contents of that select are based on what was available on the load of the page when the initial query ran - how can I get the select to update without having to submit and then edit, without the page refreshing/reloading and without the client losing the data they have currently added?
Thanks
Did read what you are asking, since its too long) But did you try javascript? changing form values after paged loaded is possible only with javascript.
When submit form on that popup, save it with ajax in database, and as response get inserted data... Then on ajax success add new option in select ( with returned data )

what is the best way to make a pre-canned response feature

what is the best way to make a pre-canned response feature within a form that submits a query on selection of a drop down list.
(the drop down list is currently populated via php sql foreach loop).
I wish to use the id of the item selected in the drop down, which is stored in the value, to report back to the sql to retrieve the same row of data only a different column....the message also that dynamically populates a text area without having php post from and back to the same page.
If I'm understanding you correctly, why not json encode the data you use to populate the select list, and then take that extra step/lookup out of the process.
Then the onchange for the select list can simply grab the right item from the object and display the message in the text area, as well as set the id in a hidden field if you need that as well.
This way, you've taken the extra call to the server out of the process, and removed at least one additional sql query as well.

interface with user input as trigger for server-side php and client side update?

I had a hard time phrasing my question's title. Basically here's the problem:
I've got this interface consisting of 5 visual rows that only differ by input and by name.
Each row has 2 select boxes/lists in it, I want the second select list to be filled with one of 7 different arrays (who are made by querying the database) depending on the selection the user made in the first selection box(the first box only influences the ORDER BY part of the query).
How do I make this happen immediately after the user makes a selection in the selection box?
Meaning without the user pressing any other submit button. I want most of the code processing to be server side but the output to be visible on client side immediately. And it would be ugly if the whole page refreshes each time the user changes a selection box, as he needs to select something in all 5.
Do I need to combine my php with some javascript to achieve this? And if so what kind of code am I looking for?
If you are using jquery this could be quite simple...
/* pick up the first select box changing */
$(".select_box_class").change(function() {
/* show some loading indicator */
show_loading_indicator();
/* fire off a ajax call to the server which could return the html for the next select box or the data to build up the html in the client */
$.post('/path/to/server.php',{selection:$(this).val()}, function(newSelect) {
/* here you can update/create you next select box */
$("#select_box2_placeholder").html(newSelect);
turn_off_loading();
});
});

Php Two Combo box in a form control each other. How?

On php page in a form, One combo box has list of my Customer from mysql table customer. Another combo box has invoiceno stored in invoice table which has respective customer records. I want to select customer from first combo box and filter invoiceno from the second one according to the customer. Any one help me for php or java or Jquery or both codeings? Means if I select customer1 then in the second combo box should show all invoiceno respective to the custermer1. No Refresh or ReLoad or Post form Pl. If I get the first selection in a php variable format example $customer, it is enough for me. Thanks for any one help me.
Based on what is given, i think one is only restricted to pushing you in the right direction. In a case where you there a large number of customers, it most likely be that you are working with a database, thus the following process:
The Page of the selects is where you will need to make an AJAX request on change of the select element.
Using your JavaScript of your favorite JavaScript library you'll make the request passing a value that you'll query your database for.
Ofcourse you'll need to configure your PHP for GET or POST depending of you AJAX request, then query the database.
Format the databases output to be a valid HTML of your selected element.
Earlier you'll have configured your AJAX script to populate the proper element once the request has been successful.
You dont want to POST to the script. You dont want to invoke any server side activity. And you want to get the value user selects into a PHP variable. From what I understand, this means, you dont clearly get where PHP plays a role. The way you want it, you might want to use XAJAX : http://www.xajax-project.org But even this causes various REQUESTs to the PHP script internally.
I would suggest the below:
Do a natural join in the SQL query.
For a customer C1, there might be 100 Invoice numbers I1. The result of the query may be outputted in a JSON format. Something like this:
"RESULT" : [
"C1" : ["I1", "I2", I3"],
"C2" : ["I11", "I22", I33"]
]
ALL the data will be sent to the browser. This data can be stored as a Javascript Object. Use JSON.parse("<PHP response here>");
Whenever the users selection changes in a combo box, have a function in Javascript to load the corresponding values in the second list.
EDIT: In case, you are dealing with a larger database, and you expect a larger dataset, I would HIGHLY recommend XAJAX -- Simple and easy! :-)
If AJAX is not an option, you must load all the things (include customer & invoice), then you can use pure javascript/css to do that. It's a dirty work around, but it works.
First, let says that you have n customer, so you will have 1 combo box to select the customer; and n combo box for the invoices that associated with them. Those invoice combobox may have id = their ids in database.
Hide all the invoice combobox, with css : display: none
Use javascript: onchange to get the change event in the customer combobox, then show the approriate invoice combobox(display:block). This can be done easily by css property of Jquery, or simply manipulate by javascript replace function (to replace the html class of the combobox)
I'm sure that this way works, but the price is that you must load all data of customers & invoices, which maybe huge. So that if possible, you should try the AJAX approach like Thrustmaster & Babiker propose.

Categories