Skip to content Skip to sidebar Skip to footer

Get Javascript Variable Value In Php Variable

I want to get the value of the selected box and save it in a PHP variable. I want to save and echo val variable. Please help

Solution 1:

use this code for use variable

<?php
  session_start();
  echo$_SESSION['php_value'];
  ?><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><script>functiongetValue(obj){
    var value = obj.value;
    $.ajax({
        type:"POST",
        url: 'edit.php',
        data: "val="+ value,
        dataType: 'text',
        async: false,
        cache: false,
        success: function ( result )  {
            window.location.reload();
        }
    });
}

</script><selectonchange="getValue(this)"><optionvalue="1"<?phpif($_SESSION['php_value'] == 1) echo'selected';?>>One</option><optionvalue="2"<?phpif($_SESSION['php_value'] == 2) echo'selected';?>>Two</option><optionvalue="3"<?phpif($_SESSION['php_value'] == 3) echo'selected';?>>Three</option><optionvalue="4"<?phpif($_SESSION['php_value'] == 4) echo'selected';?>>four</option></select>

then create edit.php file

<?php
 session_start();
 $_SESSION['php_value'] = $_REQUEST['val'];
?>

Solution 2:

Ajax can do this. Google it, and check out api.jquery.com and look at the ajax functions, .ajax(), .post(), .get(), .load(), etc.

As for your specific question, here is what you would do:

//Javascript file

$.post('my_ajax_receiver.php', 'val=' + $(this).val(), function(response) {
  alert(response);
  });

});

//PHP file my_ajax_receiver.php<?php$value = $_POST['val'];
   echo"$value";
?>

Post a Comment for "Get Javascript Variable Value In Php Variable"