Hi guys I need a help here,
I’m trying to use Ajax to update the column of the table in the database with combobox without a submit button, by the way it update the column finely but unfortunately the column data is erased to 0 when a webpage is refreshed.

May aim is to maintain the column’s data until is updated again.
Any help will be appreciated,

Here is the code

    <script>
        function showUser(str) {
            if (str == "") {
                document.getElementById("txtHint").innerHTML = "";
                return;
            } else {
                if (window.XMLHttpRequest) {

                    xmlhttp = new XMLHttpRequest();
                } else {

                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function() {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                    }
                }
                xmlhttp.open("GET","?q="+str,true);
                xmlhttp.send();
            }
        }
</script> 

<?php include("idadi.php"); ?>
<form style="height:0.5px;">
<label style="height:6px;">
<select name="Idadi" style="width:65px;" onchange="showUser(this.value)">
            <option value="0" <?php if($Idadi=="0") { echo "selected"; } ?>>0</option> 
            <option value="1" <?php if($Idadi=="1") { echo "selected"; } ?>>1</option> 
            <option value="2" <?php if($Idadi=="2") { echo "selected"; } ?>>2</option> 
            <option value="3" <?php if($Idadi=="3") { echo "selected"; } ?>>3</option> 
            <option value="4" <?php if($Idadi=="4") { echo "selected"; } ?>>4</option> 
            <option value="5" <?php if($Idadi=="5") { echo "selected"; } ?>>5</option> 
            <option value="6" <?php if($Idadi=="6") { echo "selected"; } ?>>6</option>  
  </select>
</form>

idadi.php file

<?php
include('dbconnection.php');
      $q = intval($_GET['q']); 
$idadiquery="update `itemreg` set `Idadi` = '$q'";  
$idadiresult = mysql_query($idadiquery);  
 ?>

Because you are including idadi.php along with your form, it would reset the value to "0" if the page is loaded without any $_GET data sent. I'm not an expert in writing AJAX requests without using a framework, but it looks like what you have might be loading the form page. If you want to leave it this way, then you would need to catch (in idadi.php) the instance where you haven't submitted any form data. Maybe something like if(isset($_GET['q'])){...update database...}. You could also choose to only include idadi.php if $_GET data has been sent.

When I've done things of this nature, I use an ajax request to only open the file doing the database update (idadi.php in your case) and not include it in the form page. But I would still recommend checking to see if there is any $_GET data sent.

Thank you very much for your help @grant
The problem now has been resolved, the issue was on here if(isset($_GET['q'])){...update database...} as you suggested, but two more things again,

Firstly, on the combbox selection I added some decimals 0.25, 0.5 and 0.75 but when I select any of the them the column is updated to 0 though the data type of column in the database is Double. So I don’t know where to fix so that it can accept decimals.

Secondly, I tried to catch up the id of an item so that when I select on combbox the update should only be done on a specific column-row of item instead of the whole column as you can see the modified codes here below, truly the id is caught on the html but when I make a selection no any updates is happening on the database
Thanks,,

 <script>
        function showUser(str) {
            if (str == "") {
                document.getElementById("txtHint").innerHTML = "";
                return;
            } else {
                if (window.XMLHttpRequest) {
                    xmlhttp = new XMLHttpRequest();
                } else {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function() {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                    }
                }
                xmlhttp.open("GET","?q="+str,true);
                xmlhttp.send();
            }
        }
</script> 
<?php include("idadi.php"); ?>
<form action="" method="get" style="height:1px;" >
<input name="Id" type="hidden" value="<?php echo $id; ?>" />
<label style="height:6px;">
<select name="Idadi" style="width:65px;" onchange="showUser(this.value)">
    <option value="0" <?php if($Idadi=="0") { echo "selected"; } ?>>0</option> 
    <option value="0.25" <?php if($Idadi=="0.25") { echo "selected"; } ?>>0.25</option> 
    <option value="0.5" <?php if($Idadi=="0.5") { echo "selected"; } ?>>0.5</option> 
    <option value="0.75" <?php if($Idadi=="0.75") { echo "selected"; } ?>>0.75</option> 
    <option value="1" <?php if($Idadi=="1") { echo "selected"; } ?>>1</option> 
    <option value="2" <?php if($Idadi=="2") { echo "selected"; } ?>>2</option> 
    <option value="3" <?php if($Idadi=="3") { echo "selected"; } ?>>3</option> 
    <option value="4" <?php if($Idadi=="4") { echo "selected"; } ?>>4</option> 
    <option value="5" <?php if($Idadi=="5") { echo "selected"; } ?>>5</option> 
    <option value="6" <?php if($Idadi=="6") { echo "selected"; } ?>>6</option>  
  </select>
</form>

idadi.php

<?php
include('dbconnection.php');
        $Id=$_GET['Id'];
        $q = intval($_GET['q']);

        if(isset($_GET['q'])){
            $idadiquery="update `itemreg` SET `Idadi`='$q' WHERE 'id'='$Id'";
            $idadiresult = mysql_query($idadiquery);
            }

?>

Regarding your first question. Do you have the column length set to 1? I'm not a database expert, and I haven't used Double as a column type before. I always use Decimal, but from what I've read, Double and Decimal are virtually the same except for precision. So the only reason I can think for it to be setting to 0 is if you have the column limited to one digit.

As for your second question, this line of code $Id=$_GET['Id']; isn't capturing anything because you haven't set $_GET['Id']. You need to pass that in the url with your javascript code.

Also, I noticed that you aren't escaping or filtering any of the values that you are capturing with the GET request. This is a very, very important thing to do! As your code stands now, it is vulnerable to attacks. You can read about the mysql_real_escape_string function here.. Take note, however, that using mysql_query() and mysql_real_escape_string() are removed in PHP 7.0.0. You should begin to familiarize yourself with MySQLi or PDO_MySQL. Read the warning when you follow the link above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.