-1

I am trying to connect to MySQL Db in the below javascript function.The funcion however does not execute.Can someone please tell me how to connect to MySQL DB and retrieve data through a JS function?

function jsfunction(projectSelected){

  alert("hi");
   document.getElementById("total").value = projectSelected;
   a="hi";

   Class.forName("com.mysql.jdbc.Driver");
   Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/service", "root", "root");
   Statement st4=con.createStatement();

   ResultSet rs4=st4.executeQuery("SELECT * FROM project WHERE project= '"+project_selected+"' " );

  String total_amount=rs.getString("total_amount");


}
nats
  • 1
  • 3
  • You mess javascript and java code. I'm absolutely sure that you have no idea what are you doing. – degr Sep 06 '16 at 10:34
  • I am trying to change value of a text box based on the selection of a drop down.I am a beginner here with limited knowledge of Java,JS,HTML,JSP – nats Sep 06 '16 at 11:03

1 Answers1

-2

You should not use js for sql query. I would do it through a request with AJAX which will execute a php document.

Read this article and get to know how: http://www.w3schools.com/ajax/default.asp

PS: Sorry for my barely available english skills

PPS: I have made a function some time ago that maybe can be usefull for you:

function request(url,value,output_id,output){

    $.post(url,
    {
      value: value,
      output_id:output_id,

    },
    function(data,status){
        if(status==="success"){
         if(output===1){
          alert(data);
          alert("||value"+value)
         }
         if(output_id!=0){

         document.getElementById(output_id).innerHTML =data;
         }
        }
    });}

Usage You have to hand over the id of a httml element(output_id)where you want the result to be shown, a value which will be sent to the php file and the name of the file(url).

The PhP file To use the value in the php file of your choise, you just have to add this little line somewhere:

$php_value = strtoupper($_POST['value']);