最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get Javascript Variable value in PHP variable - Stack Overflow

programmeradmin0浏览0评论

    var val;
    $('select').on('change', function() {
        alert( this.value );
        val = this.value;
    })
    
    <?php
echo $variable = "<script>document.write(val)</script>";
?>
<select>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">four</option>

</select>

    var val;
    $('select').on('change', function() {
        alert( this.value );
        val = this.value;
    })
    
    <?php
echo $variable = "<script>document.write(val)</script>";
?>
<select>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">four</option>

</select>

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

Share Improve this question edited Mar 2, 2017 at 12:43 JonyD 1,3594 gold badges21 silver badges36 bronze badges asked Mar 2, 2017 at 12:20 Zaid ButtZaid Butt 351 gold badge1 silver badge7 bronze badges 5
  • read this stackoverflow./questions/14354147/… – Afsar Commented Mar 2, 2017 at 12:22
  • 4 Read this: What is the difference between client-side and server-side programming? – Jomoos Commented Mar 2, 2017 at 12:23
  • Then What should I do for getting selected value in a php variable – Zaid Butt Commented Mar 2, 2017 at 12:38
  • use ajax for this . php server side so need to use ajax for this condition – Shafiqul Islam Commented Mar 2, 2017 at 12:50
  • Possible duplicate of How to pass jQuery variables to PHP variable? – Zachary Craig Commented Mar 2, 2017 at 12:56
Add a ment  | 

2 Answers 2

Reset to default 1

use this code for use variable

  <?php
  session_start();
  echo $_SESSION['php_value'];
  ?>
  <script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script>
    function getValue(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>

<select onchange="getValue(this)">
<option value="1" <?php if($_SESSION['php_value'] == 1) echo 'selected';?>>One</option>
<option value="2" <?php if($_SESSION['php_value'] == 2) echo 'selected';?>>Two</option>
<option value="3" <?php if($_SESSION['php_value'] == 3) echo 'selected';?>>Three</option>
<option value="4" <?php if($_SESSION['php_value'] == 4) echo 'selected';?>>four</option>

</select>

then create edit.php file

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

Ajax can do this. Google it, and check out api.jquery. 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";
?>
发布评论

评论列表(0)

  1. 暂无评论