如何用从另一个php页面收到的字符替换数组中的字符

时间:2015-09-11 15:00:00

标签: php ajax

我遇到了通过Ajax从另一个页面接收字符来替换字符的问题。

href="show.php?q=1";

当用户点击此链接时,它会转到:

show.php

<?php
    session_start();
    $q = $_GET['q'];
    $query = mysql_query("SELECT mname FROM BClassroom WHERE slno='".$q."'");
    while($row = mysql_fetch_array($query)){
        $n = $name = strtoupper($row['mname']);
    }

    $_SESSION['name'] = $name;
    for($i = 0; $i < strlen($name); $i++){
        switch($name[$i]){
            case 'A':
                    $name[$i] = "A";
                  //  echo 'A'." ";
                    break;
            case 'E':
                    $name[$i] = "E";
                  //  echo 'E'." ";
                    break;
            case 'I':
                    $name[$i] = "I";
                   // echo 'I'." ";
                    break;
            case 'O':
                    $name[$i] = "O";
                  //  echo 'O'." ";
                    break;
            case 'U':
                    $name[$i] = "U";
                   // echo 'U'." ";
                    break;
            case ' ':
                    $name[$i] = "/";
                   // echo "/"." ";
                    break;
            default:
                    $name[$i] = "_";
                   // echo "_"." ";
                    break;
        }

    }
    echo "<p id='datatbl'>$name</p>";
?>

<fieldset>
<legend>START THE GUESS</legend>
<div>
    <?php
        $consonant = "BCDFGHJKLMNPQRSTVWXYZ";
          $len = strlen($consonant);
            for($j=0;$j<$len;$j++){         ?>  
                <button name='CharCheck' id="sahil" onclick=checkBollyChar(this.value) value='<?php echo $consonant[$j] ?>'><?php echo $consonant[$j] ?></button>
        <?php   }   
    ?>
    <p><span id="datatbl"></span></p>
</div>
<button onclick="showResult(this.value)" value="<?php echo $n; ?>">Show Result</button>
</fieldset>

<script>
   function checkBollyChar(str){
            if(str==""){
               document.getElementById("datatbl").innerHTML="";
               return;
            }else{
                  if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
                         xmlhttp = new XMLHttpRequest();
                            } else {
            // code for IE6, IE5
                       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                              }
               xmlhttp.onreadystatechange = function(){
                    if(xmlhttp.readyState == 4 && xmlhttp.status ==200){
                        document.getElementById("datatbl").innerHTML= xmlhttp.responseText;
                    }
               }
               xmlhttp.open("GET","handler.php?q="+str,true);
               xmlhttp.send();
            }
      buttonHide();
   }
</script>
<script>
   function showResult(str){
            if(str==""){
               document.getElementById("datatbl").innerHTML="";
               return;
            }else{
                  if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
                         xmlhttp = new XMLHttpRequest();
                            } else {
            // code for IE6, IE5
                       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                              }
               xmlhttp.onreadystatechange = function(){
                    if(xmlhttp.readyState == 4 && xmlhttp.status ==200){
                        document.getElementById("datatbl").innerHTML= xmlhttp.responseText;
                    }
               }
               xmlhttp.open("GET","Result.php?q="+str,true);
               xmlhttp.send();
            }
   }   
</script>

当用户点击按钮name="charCheck"时,如果该字符与该字词中的实际字符匹配,则应将"_"替换为该字符。

对于"JURASSIC",它仅显示"_ U _ A _ _ I _ _"个元音,因此当用户点击按钮"J"时,首先应将"_"替换为"J"。< / p>

感谢。

0 个答案:

没有答案