1

I would like to assign input box field value as a php variable in a same page when any keypress/keyup event is happen. The input box is as following,

<input type="search" id="searchbox" class="inputbox-findtag" name="keyword" placeholder="Search tag...">

I would like to assign the input box field value with live(i.e, on keyup/keypress/...) as a php variable in a same page that is look like,

$field_value = [input box value]

To do this I have already used jQuery-Ajax that is look like below,

$("#searchbox").on('keyup',function () {
    var key = $(this).val();

    $.ajax({
        url:'fetch.php',
        type:'GET',
        data:'keyword='+key,

        success:function (data) {
          $("#rsql").html(data);
        }
    });

});

And the fetch.php file is below,

 if($_GET['keyword'] && !empty($_GET['keyword']))
 {
      $keyword = $_GET['keyword'];
      echo $keyword;
 }

I have already got the input box value by keyup. But how can I assign this value as a php variable $field_value that I have declared previous.

If you have any other idea to get live input box field value in same page to assign a php variable then you can share it.

Optimus Prime
  • 308
  • 6
  • 22

1 Answers1

0

As Per Assign jquery value to php variable

You simply cannot do that, you need to understand the difference between client/server side programming, you cannot assign Javascript value to PHP variable, yea but you can assign PHP value to your javascript.

You can use cookies to achieve this.

In Javascript:

<script type="text/javascript">
    document.cookie = "var1=1";
</script>

And in PHP

<?php 
   $phpVar =  $_COOKIE['var1'];
   echo $phpVar;
?>
Community
  • 1
  • 1
LuFFy
  • 8,799
  • 10
  • 41
  • 59