简体   繁体   中英

Mysql query not updating db with .open request while using in javascript

I am trying to update a row where I want a checkbox value to update a table field in mysql. I dont want to use form as I dont want the page to refresh. I have pasted the code below and this is the 3rd consecutive day that I am stuck here.

 <script type="text/javascript">
        function chkit(uid, chk, sid) {
            chk = (chk==true ? "1" : "0");
            var url = "edit_sms_process.php?userid="+uid+"&chkYesNo="+chk+"&sid="+sid+"";
            if(window.XMLHttpRequest) {
                req = new XMLHttpRequest();
            } else if(window.ActiveXObject) {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            }
            // Use get instead of post.
            req.open("GET", url, true);

            req.onreadystatechange = function () {
                if (req.readyState === 4) {
                    if (req.status === 200) {
                        console.log(req.responseText);

                    } else {
                        console.log("Error", req.statusText);
                    }
                }
            };

            req.send(null);
        }
    </script>

My php page:

 <div class="form-group">

                                       <label class="control-label col-md-3 col-sm-3 col-xs-12"
                                               for="chk_<?php echo $row2['teacher_id']; ?>">SMS Send<span
                                                class="required">*</span>
                                        </label>

                                        <div class="slideThree">
                                            <input type="checkbox"
                                                   name="chk"
                                                   id="chk_<?php echo $row2['teacher_id']; ?>"
                                                   value="chk"
                                            onclick="chkit(<?php echo $row2['teacher_id']; ?>, this.checked, <?php echo $row2['teacher_schoolref_id'] ?>);"
                                                <?php echo ($row2['sms_send'] == 1) ? 'checked="checked"' : ''; ?> />
                                            <label for="chk_<?php echo $row2['teacher_id']; ?>"></label>

c

Now the chrome console shows this

edit_sms.php?sms=61:151 XHR finished loading: GET " http://localhost/cloud_school/cloud_school/dashboard_files/communication/edit_sms_process.php?userid=61&chkYesNo=0&sid=10 ".chkit @ edit_sms.php?sms=61:151onclick @ edit_sms.php?sms=61:383 edit_sms.php?sms=61:143 10success

// Get the variables.
$userid = $_GET['userid'];
$chkYesNo = $_GET['chkYesNo'];
$schoolcid= $_GET['sid'];

$sql = "UPDATE teachers SET sms_send = $chkYesNo WHERE teacher_id = $userid AND     teacher_schoolref_code = $schoolcid";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

if($result==1) {
 echo $result;
 echo $chkYesNo;
 echo "success";
}

Eventhough console shows as success, the db is not getting updated. I have included the db connect string in the php page and have not added here for the sake of simplicity.

Any help will be Greatly greatly appreciated!!!

Okay with the help of dod29, I was able to resolve the issue. I was using a wrong column name in the second instance changing which resolved the issue. Thank you dod29

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM