无法使用AJAX将数据传递到php文件

时间:2018-03-01 21:40:30

标签: javascript php ajax

我正在尝试将数据传回服务器,然后使用回复更新浏览器页面。 我的SELECT输入代码如下:

<select id ="MatchCaptain" name="MatchCaptain" onchange="findTeleNo(this.value)"
                        <?php 
                            $MC = $_SESSION["MatchCapt"];
                            player_load($MC);
                        ?>
                        >
                </select>

脚本代码如下;

<script>
    function findTeleNo(that){
    alert("I am an alert box!" + that);
    var xhttp;
    if (window.XMLHttpRequest) {
        // code for modern browsers
        xhttp = new XMLHttpRequest();
        } else {
        // code for old IE browsers
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("TeleNo").value = this.responseText;
        }
      }
    };
    xhttp.open("GET", "findTeleNo.php?q=" + that, true);
    xhttp.send();
</script>

脚本的目的是获取下拉列表中选择的值(变量“that”)并将其作为变量q提交给php文件。

PHP文件如下;

<?php
$MatchCaptain  = $_REQUEST["q"];
$teleNo = "";
  $db_handle = mysqli_connect(DB_SERVER, DB_USER, DB_PASS );
    $database = "matchmanagementDB";
    $db_found = mysqli_select_db($db_handle, $database);
    if ($db_found) {
    $SQL = "SELECT * FROM `playerstb` ORDER BY `Surname` ASC, `FirstName` ASC";
    $result = mysqli_query($db_handle, $SQL); 
    $ufullName = split_name($MatchCaptain);
    while ( $db_field = mysqli_fetch_assoc($result) ) {
        $uName = $db_field['FirstName'];
        $uName = trim($uName);
        $Surname = $db_field['Surname'];
        $Surname = trim($Surname);
        $fullName = $uName." ".$Surname;
        if ($fullName == $ufullName )
        {
            $teleNo = $db_field['TeleNo'];
            break;
        }
        }
}
echo $teleNo;

function split_name($name) {
    $name = trim($name);
    $last_name = (strpos($name, ' ') === false) ? '' : preg_replace('#.*\s([\w-]*)$#', '$1', $name);
    $first_name = trim( preg_replace('#'.$last_name.'#', '', $name ) );
    $ufullName = $first_name." ".$last_name;
    return $ufullName;
}
?>

php文件从url请求q变量并使其成为$ MatchCaptain。 这将是一个像Joe Bloggs这样的名字。下一段代码连接到MySQL表,以提取玩家名字姓氏和电话号码。将名字和姓氏连接起来形成全名,与$ MatchCaptain进行比较。匹配时,变量$ teleNo被设置为该玩家的电话号码。 echo语句重新运行脚本的值。 我想要更新的字段ID是;

<p><b>Telephone Number:&nbsp;</b> <span id="TeleNo"> <?php echo $_SESSION["TeleNo"]; ?></span></p>

脚本函数findTeleNo中的警告向我显示我已进入该函数但在此之后没有任何反应。 关于我如何开展这项工作的任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我已将脚本更改为

<script>
    function findTeleNo(that){
        var xhttp;
        if (window.XMLHttpRequest) {
            // code for modern browsers
            xhttp = new XMLHttpRequest();
            } else {
            // code for old IE browsers
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            xhttp.open("GET", "findTeleNo.php?q=" + encodeURIComponent(that), true);
            xhttp.send();
        xhttp.onreadystatechange = function() {
            if (xhttp.readyState === 4) {
                if (xhttp.status === 200) {
                    // OK
                    alert('response:'+xhttp.responseText);
                    document.getElementById("TeleNo").innerHTML = this.responseText;
                    // here you can use the result (cli.responseText)
                } else {
                    // not OK
                    alert('failure!');
                }
            }
        };
    };

</script>

alert('response:'+xhttp.responseText);显示的响应是正确的,代码行

document.getElementById("TeleNo").innerHTML = this.responseText;

会打印对网页的回复。

相关问题