anakpanti anakpanti - 13 days ago 5
PHP Question

HTML - PHP - onclick() submit button to update value in SQL Server

I have a button on HTML:

<input class="endthischat" name="endthischat" type="submit"
id="endthischat" onclick="setuserlogout()" value="End This Chat" />


When the button clicked, it supposed to call a function to send a session "$chatwith" as parameter to update some value in database SQL Server.

Please also find the function below:

<?php
function setuserlogout(){
$query3 = "UPDATE users SET isActive = (?) WHERE user_email = (?)";
$var3 = array("0",$chatwith);

if($chatwith !=""){
sqlsrv_query($conn, $query3, $var3);
sqlsrv_close( $conn ); // Close the connection
}else{
$message = "wrong answer";
echo "<script type='text/javascript'>alert('$message');</script>";
}
}
?>


However, the button doesn't success in calling the function. Please kindly help me and thanks for your time.

Javascript:

<script type="text/javascript">
function setuserlogout(){
$("#endthischat").click(function(){
function() {setuserlogout()};
})
};
</script>

Answer

An untested example of how you could use ajax to send the request to the server and run the sql commands if applicable. Send a simple message in reqponse back to the ajax callback function.

<?php
    if( isset( $_POST['chatwith'] ) ){

        $chatwith = $_POST['chatwith'];


        if( !empty( $chatwith ) ){
            $sql = "UPDATE users SET isActive = ? WHERE user_email = ?";
            $params = array("0",$chatwith);

            sqlsrv_query($conn, $sql, $params);
            sqlsrv_close( $conn );

            $message='ok';
        } else {
            $message = "wrong answer";
        }

        /* send message to ajax callback function */
        exit( $message );
    }
?>

<!doctype html>
<html>
    <head>
        <title>simple ajax example</title>
        <script type='text/javascript' charset='utf-8'>

            function ajax(m,u,p,c,o){
                /*
                    Method,Url,Params,Callback,Options
                */
                var xhr=new XMLHttpRequest();
                xhr.onreadystatechange=function(){
                    if( xhr.readyState==4 && xhr.status==200 )c.call( this, xhr.response, o, xhr.getAllResponseHeaders() );
                };

                var params=[];
                for( var n in p )params.push(n+'='+p[n]);

                switch( m.toLowerCase() ){
                    case 'post': p=params.join('&'); break;
                    case 'get': u+='?'+params.join('&'); p=null; break;
                }

                xhr.open( m.toUpperCase(), u, true );
                xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                xhr.send( p );
            }


            function setuserlogout(){
                var chatwith=document.getElementById('chatwith');

                var method='post';
                var url=location.href;
                var params={ chatwith:chatwith.value };
                var callback=function(r,o,h){
                                                o.output.innerHTML=r;
                                            };
                var options={ output:document.getElementById('results') };

                /* send the ajax request to the same page ( or other ) */
                ajax.call( this, method, url, params,callback, options );
            }

        </script>
    </head>
    <body>
        <input type='text' name='chatwith' id='chatwith' />
        <input class="endthischat" name="endthischat" type="submit" id="endthischat" onclick="setuserlogout()" value="End This Chat" />
        <div id='results'></div>
    </body>
</html>