update database from html form using ajax

后端 未结 3 542
隐瞒了意图╮
隐瞒了意图╮ 2021-01-20 06:24

I would like some help with ajax. I would like to update a php file which will update a database. I have a form which send the selected check box to a php file which then up

3条回答
  •  耶瑟儿~
    2021-01-20 06:36

    Update: As well as fixing the dataString, stop the form from being submitted so that your function is used:

    The ajax call should handle returned data from updateDb.php.

    Update the php file to send data back to the server, revert to $_POST instead of $_GET and remove the header call at the bottom:

    if($result){
        $data['success'=>true, 'result'=>$result];
    
    } else {
        $data['success'=>false];
    }
    echo json_encode($data);
    // die(); // nothing needed after that
    

    Update the ajax call to handle the response and fix your dataString with '&' between params (This is why you are not getting your params properly).

    var dataString = 'boiler=' + boiler + '&niamh=' + niamh;
    
    // AJAX code to submit form.
    $.ajax({
    type: "POST",
    url: "updateDB.php",
    data: dataString,
    cache: false,
    success: function(data) {
        var json = $.parseJSON(data);
        if(json.success){
            // update the page elements and do something with data.results
            var results = data.results;
    
        } else {
            // alert("some error message")'
        }
    }
    });
    

    }

提交回复
热议问题