简体   繁体   中英

Activating php on javascript with keypress

i want to activate my php code with the use of an assigned key which is "q" is it possible to send php code by a keypress?

here is my code:

<?php 
    $name = "soma";
?>

<script>
    document.onkeydown = function(evt) {
        var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
        if (keyCode == 81) {

            //i want to put my php code here
            // <?php $sql = mysqli_query("INSERT INTO tb_x (`name`)VALUES('$name')");

        }
    }

</script>

you need to use jquery library then

if(keyCode == 81)
    {
    $.post("phpscript.php" , function(data){
        //you can put nothing here.
    }
}

and in phpscript.php

$name = "soma"
$sql = mysqli_query("INSERT INTO tb_x (`name`)VALUES('$name')")

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