File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Can AJAX helps me to do that Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Can AJAX helps me to do that" Watch "Can AJAX helps me to do that" New topic
Author

Can AJAX helps me to do that

Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Hi friends...

After a long time from surfing i find many sample codes for dependent drop down. But in my case, I have a page that has a combo box.When i select a value from that combo box the remaining text fields could display the details that related to that value selected in the combo box. But i may find difficult as i am a beginner in JSP and i never used AJAX before. So will someone please suggest me some sites or tutorials regarding my problem.... Thanks in advance

[ July 09, 2008: Message edited by: Bear Bibeault ]

Never try to be a hard-worker. Be a smart-worker.
My Blog
Jetendra Ivaturi
Ranch Hand

Joined: Feb 08, 2007
Posts: 159
Use AJAX if you want interaction with server.

Otherwise javascript would be the best, for this.

Google about select you might find. I dont have the exact link now.

But have done the same. Just a guess try with multiple selects.


SCJP 1.4 & 1.5, SCWCD 1.5. Learn and Let Learn.
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Hi Jetendra ..
My problem is when i select a option from combo box,the other fields in my form like text field,text area might be filled up with the details regarding the option selected. Like if i select "username" from combobox, then textfield1,2,3 are filled with user password, user address,and user email.Like this way..But when i go for AJAX it's dependent drop down's which i don't want.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Like if i select "username" from combobox, then textfield1,2,3 are filled with user password, user address,and user email.Like this way..


will you fetch these values from a database on the server?
or if you know them beforehand then you can use Java beans.



Hope this helps .


SCJP, SCWCD.
|Asking Good Questions|
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

No i need to fetch these values from database and i need it to be loaded after i select the option..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

First of all...
Originally posted by Rajkumar balakrishnan:
I have a page that has a combo box.
No, you don't. there is no such thing as a "combo box" in HTML. Please read this for more information.

Secondly, yes, Ajax is a good way to achieve this. Upon a change of selection, you can fire off an Ajax request to obtain the necessary data for display.

If you want to look ay an example of doing just this sort of interaction using jQuery as the Ajax library (highly recommended), see the examples for chapter 8 of my jQuery in Action book. You can download the code from the book's web site.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Carlos Herrera
Greenhorn

Joined: Jul 09, 2008
Posts: 5
You can do that with JSP, Javascript and a Java class (JavaBean or not) as well.

Add an onChange=YourJavaScriptFunction to the html tag which is modified by the user.

On YourJavaScriptFunction you can call a method from your Java class which uses the selected option (a String) to get the other information you need from the database. You'll have to use some SQL or similar to get the info from the database. You'll also need code for the connection to the database, set the driver of the DBMS you are using, and stuff.

You can't forget to import you Java class on your JSP file as well.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Originally posted by Carlos Herrera:
On YourJavaScriptFunction you can call a method from your Java class ...
No, you can't. That's what the Ajax is for. JavaScript has no means to call Java methods on the server.
Carlos Herrera
Greenhorn

Joined: Jul 09, 2008
Posts: 5
I think I expressed myself in a bad way. You call the Java class inside your JavaScript function, but also, inside the JSP tags. Actually, I believe you're calling your method Java through Java.

Something like this:

function Whatever(){
<%
MyBean theBean = MyJavaClass.myJavaMethod(request.getParameter("field"));

String field1 = theBean.getField1();
String field2 = theBean.getField2();
String field3 = theBean.getField3();
%>

document.my_form.field1.value = '<%=field1%>';
document.my_form.field2.value = '<%=field2%>';
document.my_form.field3.value = '<%=field3%>';
}

I'm not all that knowledgeable about JSP, but I'm pretty sure this works.
Or maybe I didn't undestand what was being asked and I'm just saying nonsense. Sorry if that's the case.
[ July 10, 2008: Message edited by: Carlos Herrera ]
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

I just do the coding but when i select the values it seems to be idle. No changes have been there in my form.
Here is the code



And i call my function in the onChange of the select menu. But there is no response from AJAX. Here is my servlet code and Bean file.



Bean file


Help me regard my problem...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Originally posted by Carlos Herrera:
I think I expressed myself in a bad way. You call the Java class inside your JavaScript function, but also, inside the JSP tags. Actually, I believe you're calling your method Java through Java.
Something like this:
No, that still won't work.

The JSP is executed on the server long before the HTML page with the JavaScript is sent to the browser. Embedding it in JavaScript does not in any way make it part of the JavaScript function. Perhaps this article will be helpful in understanding why this cannot be done.

Also, let's not hijack this thread any further with this. If you want to discuss this issue further, please start a new topic.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Originally posted by Rajkumar balakrishnan:
I just do the coding but when i select the values it seems to be idle. No changes have been there in my form.
What has your debugging steps revealed?

Have you used Firebug in Firefox to see if your Ajax request is being sent correctly and returning the expected response?
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445


Have you used Firebug in Firefox to see if your Ajax request is being sent
correctly and returning the expected response?


I have firebug installed but it lokks like this

XMLHttpRequest Status=0 readyState=0 multipart=false


And everything like statusText,responseText seems to be null?

What's wrong with my code?
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

After a long time of surfing i got the idea so i change the code like the below one
var xmlHttp

function showCustomer(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert("Your browser does not support AJAX!");
return;
}
var url="getvendor.jsp";
url=url+"?q="+str;
//url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}


And i get the value from getvendor.jsp..In firebug it shows no error..instead it show the below as response

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Get Vendor</title>
</head>
<body>
</body>
</html>


But i write some code to print the values in the table..What's wrong with my code...Please help me...

:roll:
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Hi friends,
Now my AJAX file works fine it passes the parameter to the JSP file but now the JSP file seems to be not working. It returns nothing..Instead it shows an error in the console like this..
Error: java.sql.SQLException: null, message from server: "Unknown column 'q' in
'where clause'"


Will i move this query to JSP page..!!!

Or please help me friends....
Carlos Herrera
Greenhorn

Joined: Jul 09, 2008
Posts: 5
I read the article, Bear. All good.

Yes, I'll open a new topic and I would appreciate if we could discuss this further, because I'm pretty sure that I'm doing what Rajkumar was asking using code that is very similar to the one I posted above.

I guess I must be using a jsp file in a way that is not recommended. I'll open the topic on the JSP forum.
[ July 11, 2008: Message edited by: Carlos Herrera ]
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Carlos Herrera
Which article? Could you please suggest me such article....
Carlos Herrera
Greenhorn

Joined: Jul 09, 2008
Posts: 5
Sorry for that, Rajkumar.

I was trying to quote Bear, so the article was the one he recommended about JSP. I edited my message after that.

But I found something that could be helpful to you:
http://elcio.com.br/ajax/combos/exemplo4.php

Check the source of that page.
It populates a select field based on the option selected on another select field. I think it's similar to what you're looking for.
[ July 11, 2008: Message edited by: Carlos Herrera ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Originally posted by Rajkumar balakrishnan:
Will i move this query to JSP page..!!!
Why would this be moved to the JSP forum when the problem is clearly in your JDBC?

This post is too long to move to JDBC now. I'd suggest narrowing your problem to the JDBC issues and posting a new topic in the JDBC forum.
[ July 11, 2008: Message edited by: Bear Bibeault ]
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Ok... i am free now...I'll fix the problem with the help of you ranchers and finally i recommend the moderator to close the post since no issue's were going to discuss here..And once again thank you for all ranchers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can AJAX helps me to do that