How do you do that by pressing enter on an html input, a php file is executed?

0

How to do so that pressing the ENTER key in <input type="text" placeholder="Enviar mensaje..." name="msg" id="msg"> is executed:

<?php
 $con = mysqli_connect("localhost", "root", "", "chat");
if(isset($_POST['???']))
{
    $name = $_POST['userName'];
    $msg = $_POST['userMsg'];
    $query = "INSERT INTO chat SET name= '$name', msg='$msg'";
    $run = mysqli_query($con, $query);
 if($run)
 {
    echo "<embed loop='false' src='chat.wav' hidden='true' autoplay='true'/>";
 }
}
?>
    
asked by Equipo X54 20.10.2018 в 03:55
source

1 answer

0

because there are 2 ways, the first would be through a form send the data to the php file:

<form action="tuarchivo.php" method="post">
<input type="text" placeholder="Enviar mensaje..."  name="msg" id="msg">
<input type="submit" value="Enviar">
</form>

The second way you still use a form, but these are done using Jquery-Ajax:

<form id="form_enviar" method="post">
<input type="text" placeholder="Enviar mensaje..."  name="msg" id="msg">
</form>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
    $(document).ready(function(){
        $("#form_enviar").submit(function(event) {
            //Evita que la pagina se recargue
            event.preventDefault();
            $.ajax({
                url:'url_a_tu_archivo.php',
                type: 'POST',
                data: $("#form_enviar").serialize(),
                success: function (response) {
                    console.log(response);
                }
            });
        });
    });
</script>
    
answered by 20.10.2018 / 06:37
source