[Solved] ajax update php variable from input box without submitting [closed]


Alright I get what your asking for now. The javascript below will make a call from your page to an external script (ajax/myscripthere.php) which will handle the SQL.

In the php script, we simply handle our SQL like we normally would if this was not an AJAX call.

Please accept answer if this is correct to your expectations 🙂

javascript:

<script>
    var xmlHttpReq = false;

    // Set ID Equal to your input box ID
    input_box_value = document.getElementById("inputboxvariable").value;

    if(window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    else if(window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    // Set filename equal to your PHP script
    self.xmlHttpReq.open('POST', "ajax/myscripthere.php", true);

    self.xmlHttpReq.onreadystatechange = function()
    {
        if(self.xmlHttpReq.readyState == 4)
        {
            // Update is complete
        }
    };

    self.xmlHttpReq.send("value="+input_box_value);
</script>

PHP:

<?php
if(isset($_POST['value']))
{
    $conn = new mysqli("host", "username", "password", "db_name")
                 or die ('Error: '.mysqli_connect__error());

    // Make Correct SQL Query (i dont know your DB)
    $query = "UPDATE table_name SET col=".$_POST['value']." WHERE xxxxxx;";

    $result = @$conn->query($query);

    $conn->close();
?>

solved ajax update php variable from input box without submitting [closed]