在同一文件中获取PHP变量中选择框的HTML值

My HTML code creates select options

<select name ="sortBy" id="sortBy" onchange="getValue(this)">   
    <option value=""></option>
    <option value="total">Total Medals</option>
    <option value="gold">Gold Medal</option>
    <option value="silver">Silver Medal</option>
    <option value="bronze">Bronze Medal</option>
</select> 

My attempt at using Ajax to read the value from the select box

function getValue(obj){

   $("#sortBy").on( 'click', function () {
    $.ajax({
        type: 'post',
        url: 'main.php',
        data: { source1: obj.value },
        success: function( data ) {
            console.log( data );
        }
    });
});

}

My PHP code that creates a variable

$sortBy   = $_POST['source1'];
echo $sortBy;

However I get an error message saying source1 is an undefined index in the PHP code. I assume it isn't getting the variable from JS.

All of these are on the same document (main.php). I'd like the PHP variable to changed depending on the variable gotten in JS. How can I fix this? Thanks How can I fix this?

查看全部
dongyan3562
dongyan3562
2017/03/23 08:10
  • php
  • html
  • ajax
  • javascript
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复