onclick -> mysql query -> javascript; same page

后端 未结 4 1262
自闭症患者
自闭症患者 2021-01-29 03:41

I need button to begin a mysql query to then insert the results into a javacript code block which is to be displayed on the same page that the button is on. mysql queries come f

相关标签:
4条回答
  • 2021-01-29 04:18

    here is how I used an onchange method to stimulate a MYSQL query and have the Highchart display the result. The major problem was that the returned JSON array was a string that needed to be converted into an INT. The resultArray variable is then used in the data: portion of the highChart.

    $(function(){
      $("#awayTeam").change(function(){ 
        $.ajax({
        type: "POST",    
        data: "away=" + $("#awayRunner").val(),
        dataType: "json",
        url: "/getCharts.php",
        success: function(response){
              var arrayLength = response.length;
              var resultArray = [];
              var i = 0;
              while(i<arrayLength){
                  resultArray[i] = parseInt(response[i]);
                  i++;
              }            
    

    In the PHP code, the array must be returned as JSON like this

    echo json_encode($awayRunner);

    0 讨论(0)
  • 2021-01-29 04:25

    For this purpose you need to learn ajax.This is used to make a request without reloading the page.so that you can make a background call to mysql

    your code will be something like that

    $("#submitbutton").live("click",function(){
    
    $.ajax({url:"yourfile"},data:{$(this).data}).done(function(data){
    //this data will in json form so decode this and use this in div 2
    var x =$.parseJSON(data);
    $("#div2").html(x.val());
    })
    })
    

    and "yourfile" is the main file which connect to server and make a database request

    0 讨论(0)
  • 2021-01-29 04:26

    You need to use a technology called AJAX. I'd recommend jQuery's .ajax() method. Trying to do raw XHR is painful at best.

    Here is how you'll want to structure your code:

    1. Load the page.
    2. User chooses an option.
    3. An onChange listener fires off an AJAX request
    4. The server receives and processes the request
    5. The server sends back a JSON array of options for the dependent select
    6. The client side AJAX sender gets the response back
    7. The client updates the select to have the values from the JSON array.

    Basically, HTTP is stateless, so once the page is loaded, it's done. You'll have to make successive requests to the server for dynamic data.

    0 讨论(0)
  • 2021-01-29 04:39

    Use AJAX,

    example

    $.ajax({
        type: "POST",
        url: "yourpage.php",
        data: "{}",
        success: function(result) {
            if(result == "true") {
                // do stuff you need like populate your div
                $("#one").html(result);               
            } else {
                alert("error");
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题