It's strictly server-side, but I've always thought a client-side PHP would be interesting Client-side PHP is Javascript. No, client-side Javascript is Javascript. They're completely different languages. It would be possible to construct a browser that could execute PHP code directly. That's what I was talking about.
Learning has never been so easy!
______________________________________________________________
EDIT: It turns out that the script has stopped working, so here is another demo: http://talkerscode.com/webtricks/dynamic-select-option-menu-using-ajax-and-php.php
For anyone wondering why this won't work 'as is' under jquery 1.4, it's because the way in which JSON is parsed by jquery changed at 1.4 to use the browsers native JSON parser.
The effect of this is that you now need to wrap all properties and values in double quotes (ie valid JSON) for the response to be parsed.
ie this will work:
[ {'optionValue': '0', 'optionDisplay': 'Mark'}, {'optionValue':'1', 'optionDisplay': 'Andy'}, {'optionValue':'2', 'optionDisplay': 'Richard'}]
______________________________________________________________
______________________________________________________________
I spent quite a while trying to get my head around AJAX and JSON - I still don't fully understand it, but I thought I'd at least share what I know in case someone else is looking for a similar thing.
Basically, this will query a MySQL database and update a combo list without having to reload the webpage.
It uses javascript to call a server side PHP script that returns JSON. The javascript then uses that JSON data to populate a combobox. all done using the wonderful Jquery!
Please be gentle - it's my first ever how-to guide!
5 Steps total
Step 1: Get the Jquery.js file
You can download the latest from the jquery website: http://www.jquery.com/ but my script isn't tested on the latest so I've attached the one I used.
jquery.js
Step 2: Create your main HTML file
Grab the html file attached 'test.html' and put it in the same folder as jquery.js
This has 2 select boxes, one called department and the other called category.
the javascript in the head section will sit waiting for the department select box to change, and when it does - it will call the next document 'select.php' with the argument id and whatever the value of the department select box is set at.
So for example, if the department select box is set to 1 then the javascript will run load the following: 'select.php?id=1'.
we'll come back to this javascript after we've created the MySQL lookup file.
test.html
Step 3: Sort out your MySQL database
Make sure for the next bit that you have a MySQL database set up. You will need to make sure there is data to query. Basically we will be looking to get the data back out to fill the select box - so create a table called 'categories' and fill it with 2 columns: 'id' and 'category'.
Now add 3 rows:
1; Accounts
2; Admin
3; IT
1; Accounts
2; Admin
3; IT
Now let's move on to create our select php script!
Step 4: Create the PHP select script!
Right, for this you need to edit the script to make sure it links to your database. First of all, download the attached script.
Now update the dbLocation, dbName, username and password variables at the top and put this select.php file in the same directory as the other 2 files.
![Side Side](/uploads/1/2/4/7/124725192/401218695.jpg)
select.php
Step 5: It Should Work!
No theoretically it should work if you upload it to a PHP server but let me explain what's going on...
Basically, when you change the select box 'department', as I mentioned above, the javascript fires out a lookup on select.php using whatever the value of the department select box is set at.
The select.php does an sql query on the table getting all the rows in the table categories where the column 'id' is whatever the department select box was set at.
Once it's done this it encodes all the results as JSON data and sends it back to the html page's javascript.
The javascript then takes that data, loops through each result, and updates the category select box with the data.
If you add a couple more rows to your DB with the Id also set to either 1, 2 or 3 - when you run the script again, it should populate the category select box with more data.
So - in conclusion - this script will work rather neatly when you need to update 1 select box based on another select box after the user has changed it and without having to reload the whole darn page!
Hopefully it will help someone - feel free to comment, and I will answer any questions that I'm able to :)
Have a look through the references, they should help if you get confused :)
Published: Mar 08, 2011 · Last Updated: Mar 18, 2016
References
- Demo of what we're trying to achieve
- JQuery Website
- Remy Sharp's Blog | Auto-populating Select Boxes using jQuery & AJAX
- Wikipedia: Ajax (programming)
- Wikipedia: JSON
4 Comments
- Pimientomichaelmcginn2 Mar 17, 2016 at 07:10pmI followed the steps but it did not work for me. Your demo does not seem to work either. It does not update the 2nd select box when you change the first select box.
- Thai PepperAndrew Samuel Mar 18, 2016 at 09:07amYou are correct... I don't have time at the moment to work out what's changed - but here is another demo script that does work: http://talkerscode.com/webtricks/dynamic-select-option-menu-using-ajax-and-php.php
- Thai PepperAndrew Samuel Mar 18, 2016 at 09:14amThe reason it won't work is because jquery changed the way in which JSON is parsed to use the browsers native JSON parser.The effect of this is that you now need to wrap all properties and values in double quotes (ie valid JSON) for the response to be parsed.
- Pimientomichaelmcginn2 Mar 18, 2016 at 09:42pmThanks for the update. I have been working on getting something similar to this working. My use case was just to get one dropdown box to allow a selection and then use ajax to update a db record when the onchange() function triggered a javascript function which called the ajax code that called the php code that updated the db. I will post sample code when I get it all working. I'm sure some one else will find it helpful.
- Karibasappa G C (KB)
- 13th Jan 2017
- 0
- 4022
- Client side SOAP handler in web service JAX-WS soap handler at client side SOAP handler at client side example
In the previous article, We have created a web service and attached a handler to retrieve the client MAC address in header block, for every incoming SOAP message.
In this article, we will create a web service client which can access the web service published in previous article.
Most important thing here is to define a SOAP handler to the client so that every outgoing message from client will have a client’s MAC address added in the header of a SOAP message.
Step 1
Create a maven project
Step 2
Run wsimport command
wsimport command generates all the required files by taking WSDL generated by the published service in the previous article.
wsimport -keep http://localhost:8888/ws/soapHandler?wsdl -d C:UsersRAJDesktopoutput
-keep : option to keep the generated files
-d : option to sepcify the folder where the generated files will be stored
-keep : option to keep the generated files
-d : option to sepcify the folder where the generated files will be stored
Now check the Output in the folder
Command has generated 6 source files and 6 class files
Command has generated 6 source files and 6 class files
We need to concentrate only on HelloWorldServiceImplService.java for client development
Step 3
Copy all these generated source files into project
HelloWorldServiceImplService.java looks like below
- importjava.net.URL;
- importjavax.xml.ws.Service;
- importjavax.xml.ws.WebServiceClient;
- importjavax.xml.ws.WebServiceFeature;
- * This class was generated by the JAX-WS RI.
- * Generated source version: 2.2
- */
- @WebServiceClient(name ='HelloWorldServiceImplService', targetNamespace ='http://ws.kb.com/',
- wsdlLocation ='http://localhost:8888/ws/soapHandler?wsdl')
- extends Service
- ………………………………………………………
Step 4
Create a SOAP Handler
- importjava.util.Set;
- importjavax.xml.namespace.QName;
- importjavax.xml.soap.SOAPEnvelope;
- importjavax.xml.soap.SOAPHeader;
- importjavax.xml.soap.SOAPMessage;
- importjavax.xml.ws.handler.soap.SOAPHandler;
- importjavax.xml.ws.handler.soap.SOAPMessageContext;
- publicclass MACAddressInjectHandler implements SOAPHandler<SOAPMessageContext>{
- publicboolean handleMessage(SOAPMessageContext context){
- System.out.println('Client : handleMessage() Begin');
- context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
- //If its an outgoing message from client, then outBoundProperty will be true
- try{
- SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
- if(soapHeader null){
- }
- QName qname =new QName('http://ws.kb.com/', 'macAddress');
- SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(qname);
- soapHeaderElement.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
- soapHeaderElement.addTextNode(macAddress);
- //Output the message to console
- System.err.println(e);
- System.err.println(e);
- //Returning true makes other handler chain to continue the execution
- }
- publicboolean handleFault(SOAPMessageContext context){
- System.out.println('Client : handleFault() Begin');
- }
- publicvoid close(MessageContext context){
- System.out.println('Client : getHeaders() Begin');
- }
- privateString getMACAddress(){
- //Write a java code to get client machine MAC address and return it dynamically
- return'E0-DB-55-A4-10-Z4';
- //Returning invalid MAC address
- }
In this handler , we are just adding MAC address to the SOAP header
Step 5
Create a SOAP handler xml file
- <?xml version='1.0' encoding='UTF-8' standalone='yes'?>
- <javaee:handler-chains xmlns:javaee='http://java.sun.com/xml/ns/javaee'
- <javaee:handler-chain>
- <javaee:handler-
- class>com.kb.handler.MACAddressInjectHandler</javaee:handler-class>
- </javaee:handler-chain>
We have created the handler-chain.xml file to add our SOAP handler MACAddressInjectHandler in the handler list.
Step 6
Attach a SOAP handler to web service client
- importjava.net.URL;
- importjavax.jws.HandlerChain;
- importjavax.xml.ws.Service;
- importjavax.xml.ws.WebServiceClient;
- importjavax.xml.ws.WebServiceFeature;
- * This class was generated by the JAX-WS RI.
- * Generated source version: 2.2
- */
- @WebServiceClient(name ='HelloWorldServiceImplService', targetNamespace ='http://ws.kb.com/', wsdlLocation ='http://localhost:8888/ws/soapHandler?wsdl')
- @HandlerChain(file='../handler/handler-chain.xml')
- extends Service
- ………………………………………………………
In this web service client, we are giving information about the handlers using @HandlerChain annotation.
In this annotation,we have specified the handler-chain.xml file name developed in previous step.
Step 7
Create a client to access the published web service
- importcom.kb.ws.HelloWorldServiceImplService;
- publicclass HelloWorldClient {
- HelloWorldServiceImplService service =new HelloWorldServiceImplService();
- HelloWorldService serviceInterface = service.getHelloWorldServiceImplPort();
- String response = serviceInterface.sayHelloWorld();
- }
- }
We have created a client class to invoke the web service.