Hyosoka Poipo Hyosoka Poipo - 5 months ago 11
Javascript Question

Get value from input text in php without pressing Enter

Hay I'm new to php and I have made php code like this :

<?php
session_start();
echo 'Hellow Hisoka';
?>
<form name="form" method="post">
<div class="col-sm-3">
<label class:>Nama :</label>
</div>
<div class="col-sm-9">
<input type="text" name="nama_tamu" id='nama_tamu' class="form-control" placeholder="Nama Lengkap">
<?php
$myValue = $_POST['nama_tamu'];
?>
</div>
<br/><br/>
<?php
echo $myValue;
?>
</form>


When I want to show the echo message, I need to hit
enter
on my keyboard first in order to get the value of
$_POST['nama_tamu'];
. My question is can I get the value of
nama_tamu
input without pressing
enter
, or maybe without using
POST
or
GET
?

Answer

For this purpose you should use .keyup function/event. Following are the snippet :

 $(document).ready(function () {

        $("#nama_tamu").keyup(function(){
        
        $("#enterdata").html($("#nama_tamu").val());
        });

    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<?php
    session_start();
    echo 'Hellow Hisoka';
?>
<form name="form" method="post">
    <div class="col-sm-3">
        <label class:>Nama :</label>
    </div>
    <div class="col-sm-9">
        <input type="text" name="nama_tamu" id='nama_tamu' class="form-control" placeholder="Nama Lengkap">
        <?php
            $myValue = $_POST['nama_tamu'];     
        ?>
     <br> You press following character:<div id="enterdata"> </div>
    </div> 
    <br/><br/>
    <?php
        echo $myValue;
    ?>
</form>