update database from html form using ajax

后端 未结 3 516
隐瞒了意图╮
隐瞒了意图╮ 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:

    <form name="form" onsubmit="return false;">
        <input type="checkbox" id="boiler" name="boiler">
        <input type="checkbox" id="niamh" name="niamh">
        <button onclick="myFunction()">Update</button>
    </form>
    

    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")'
        }
    }
    });
    

    }

    0 讨论(0)
  • 2021-01-20 06:58

    document.getElementByName not a javascript function, try document.getElementById() instead

    You can do this

    <form name="form" onsubmit="myfunction()">
       <input type="checkbox" id="boiler" name="boiler">
       <input type="checkbox" id="niamh" name="niamh">
       <input type="submit" value="Update"/>
    </form>
    

    Javascript:

    function myFunction() {
    var boiler = document.getElementById("boiler").value;
    var niamh = document.getElementById("niamh").value;
    // Returns successful data submission message when the entered information is stored in database.
    
    // i dont practice using url string in ajax data as it can be compromised with a quote (') string, instead i am using json
    
    // AJAX code to submit form.
        $.ajax({
        type: "POST",
        url: "updateDB.php",
        data: {
           boiler: boiler,
           niamh: niamh
        },
        cache: false,
        }).done(function() {
            alert('success');
        }); // i do this because some jquery versions will deprecate the use of success callback
    }
    

    And you are getting to a post so change the $_GET in you php file to $_POST

    0 讨论(0)
  • 2021-01-20 06:59

    I just want some suggestion and first your html page code should like-

    <html>
    <head>
        <script src="jquery-3.1.0.min.js"></script>
    </head>
    
    <body>
    <form name="form" id="form_id">
    <input type="checkbox" id="boiler" name="boiler">
    <input type="checkbox" id="niamh" name="niamh">
    <button onclick="myFunction()">Update</button>
    </form>
    <script>
    function myFunction() {
       // it's like cumbersome while form becoming larger  so comment following three lines        
          // var boiler = document.getElementByName("boiler").value;
         // var niamh = document.getElementByName("niamh").value;
         // Returns successful data submission message when the entered information is stored in database.
        //var dataString = 'boiler=' + boiler + 'niamh=' + niamh;
    
    // AJAX code to submit form.
        $.ajax({
        // instead of type use method
        method: "POST",
        url: "updateDB.php",
        // instead  dataString i just serialize the form like below this serialize function bind all data into a string so no need to worry about url endcoding
        data: $('#form_id').serialize(),
        cache: false,
        success: function(responseText) {
            // you can see the result here
            console.log(responseText)
            alert("ok"); 
        }
        });
    }
    
    </script>
    </body>
    </html>
    

    Now i am turning to php code: You used two line of code right in php

    $boiler = (isset($_GET['boiler'])) ? 1 : 0;
    $niamh = (isset($_GET['niamh'])) ? 1 : 0;
    

    $_GET is used in get method and $_POST for post method, thus you are using post method in ajax and above line of code should be like

    $boiler = (isset($_POST['boiler'])) ? 1 : 0;
    $niamh = (isset($_POST['niamh'])) ? 1 : 0;
    
    0 讨论(0)
提交回复
热议问题