2013-02-09 184 views
0

我的选择标签会给我玩家的名字,然后Query会在后面运行,然后结果会显示出来。我已经通过一些教程,但无法找到答案。我在这里添加我的HTML和PHP文件代码。JQuery和Ajax不起作用

//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); 
     */ 
    ?> 
+1

好,这看起来相当混乱给我,能否请您先解决您的HTML标记?从“

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