user1395824
user1395824

Reputation:

jQuery Ajax call to filter values from mySQL.

I want to send queryString fetched by click radio and server to get response in xml. I'm almost near to the solution, cannot debug the problem why it's not working.

<script>

$(document).ready(
                function() {
                       $("#myCity").click(  
                    /* $("input[type=radio][name=myCity]").click( */
                                       function() {
                                                 var radioCity = $('input:radio[name=myCity]:checked').val(); 
                                              /* radioCity = $('input[name=myCity]').filter(':checked').value();  */

                                              link = "http://localhost:8080/Shipping_Order/getCity_xml.jsp?qString="+radioCity;

                                        $.ajax({ 
                                            type:"GET",
                                            url : link,
                                            data : radioCity,
                                            dataType : "xml",
                                            success : function() {

                                                /* var myCity = $('input:radio[name=myCity]:checked').value; */
                                                /* var myCity = $('input[name=myCity]:radio:checked').val() */

                                                for ( var i = 0; i < xmlDoc.getElementsByTagName("city").length; i++) {

                                                    $("#radioTable").append(
                                                                    '<tr><td id="username"'+i+'>'
                                                                            + xmlDoc
                                                                                    .getElementsByTagName("username")[i].childNodes[0].nodeValue
                                                                            + '</td><td id="city"'+i+'>'
                                                                            + xmlDoc
                                                                                    .getElementsByTagName("city")[i].childNodes[0].nodeValue
                                                                            + '</td> <td id="contact"'+i+'>'
                                                                            + xmlDoc
                                                                                    .getElementsByTagName("contact")[i].childNodes[0].nodeValue
                                                                            + '</td></tr>');

                                                }
                                            }

                                        });
                                    });
                });

On click event of jQuery, variable "radioCity" get the current radio value and ajax "url" sending this value passing with queryString by GET request. server (getCity_xml.jsp) respond request in xml data and later it will append by selector name #radioTable.

In HTML i created

getCity_xml.jsp

<%
response.setContentType("text/xml");

try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/shipping_order","root","root");
Statement st = con.createStatement();
String myCity = request.getParameter("qString");
ResultSet rs = st.executeQuery("select username, contact, city from user where city ="+myCity);

while(rs.next())
{

    out.println("<user>");
    out.println("<username>"    +rs.getString(1)+   "</username>");
    out.println("<contact>"     +rs.getInt(2)+      "</contact>");
    out.println("<city>"        +rs.getString(3)+   "</city>");
    out.println("</user>"); 

}


rs.close();
st.close();

con.close();

}catch (SQLException ex) {out.println("Exception Occured");}  

%>

Upvotes: 0

Views: 764

Answers (1)

WeMakeSoftware
WeMakeSoftware

Reputation: 9162

Just a side note. Never ever ever!!!111 use String concatenation in SQL queries.

By doing this, you introduce a SQL injection vulnerability to your code. Consider visiting this tutorial : http://www.tutorialspoint.com/jdbc/jdbc-statements.htm

As for the question, you should escape the city name with quotes

ResultSet rs = st.executeQuery("select username, contact, city from user where city = '"+myCity + "'");

Upvotes: 1

Related Questions