0

我的选择标签会给我玩家的名字,然后查询将在后面运行,然后显示结果。我已经完成了一些教程,但找不到答案。我在这里添加我的 html 和 php 文件代码。

//html and script player.html file
    <script>
    <script type="text/javascript" src="jque`enter code here`ry.js">
    $(document).ready(function() {

        $('#player').on('change', function() {
           var qString = '{name: ' + $('#player option:selected').text() + '}';
           $.post('getplayer.php', qString, processResponse);
           // $('#resultsGoHere').html(qString);
        });

        function processResponse(data) {
            $('#resultsGoHere').html(data);
        }

    });
    </script>

    <form id="myForm">
    <select id="player" name="player" onchange="showPlayer(this.value)">
    <option value = "None">Choose One</option>
    <option value = "Zidane">Zidane</option>
    <option value = "Messi">Messi</option>
    <option value = "Cristiano Ronaldo">Cristiano Ronaldo</option>
    <option value = "Bergkemp">Bergkemp</option>
    </select></br></br>
    <!--<input type="submit" name="submit" value="Submit" />-->
    </form>

    //php part getplayer.php file
    <?php

        $name = $_POST['theDropdown'];  
        echo "$name";

        /*
        $db = mysql_connect("localhost","root","root");

        mysql_select_db("football");

        $select = "SELECT * FROM players";

        $result = mysql_query($select) or die(mysql_error());

        echo "<table border='1'>
        <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Position</th>
        <th>Club</th>
        <th>Nationality</th>
        <th>Date of birth</th>
        </tr>";

        while($row = mysql_fetch_array($result)){
            echo "<tr>";
            echo "<th>" . $row['id'] . "</td>";
            echo "<th>" . $row['name'] . "</td>";
            echo "<th>" . $row['pos'] . "</td>";
            echo "<th>" . $row['club'] . "</td>";
            echo "<th>" . $row['nationality'] . "</td>";
            echo "<th>" . $row['dob'] . "</td>";
            echo "</tr>";
        }   
            echo "</table>";

        mysql_close($db);
        */  
    ?>
4

2 回答 2

1

您的请求变量是name,但您访问theDropdown.

// this data object will give you a $_POST['name'] value, not $_POST['theDropdown']
var qString = '{name: ' + $('#player option:selected').text() + '}';
于 2013-02-09T08:08:24.347 回答
0

你可以做这样的事情

$(document).ready(function(){
  $("#player").change(function(){
    var dataStr = $(this).val();
    $.ajax({
      type='POST',
      data = 'name='+dataStr,
      url:'process.php',
      success:function(data) {
        alert(data);
      }
    });
  });
});

所以在 process.php 页面

echo $_POST['name'];
// Do Whatever You Want
于 2013-02-09T08:15:26.387 回答