dtcd27183 2018-06-26 06:50
浏览 46

AJAX无法正常工作(调用php并获得结果)

I am trying to make a checkbox calls php code then get back the results to be printed in textarea.

Now, the problem is that the php file doesn't print the result on the textarea or maybe the Ajax not working. I am sure that there is nothing wrong with MySQL code.

this is form.php code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
      <script language="javascript" type="text/javascript">
         $(document).ready(function() {

          var checkbox = document.getElementById('subscribers').checked;

         //Check if checkbox is checked
         if (checkbox === true) {
         //Read databank for Results
          document.getElementById("to").value = "result  should be here.";
          $.ajax({
                type: 'POST',
                url: 'sms/readSubscriber.php',
                data: {'variable': dataPhp},
                complete: function(r){
                  var subscriberNumbers = r.responseText;
                   document.getElementById('to').innerHTML = subscriberNumbers;
               }
              });


         } else {
           document.getElementById("to").value = "";
         }


         }); 


         function doalert() {
         //Check if checkbox is checked
         var checkboxElem = document.getElementById("subscribers").checked;
         if (checkboxElem === true) {
         //Read databank for Results
          document.getElementById("to").value = "result  should be here.";
          $.ajax({
                type: 'POST',
                url: 'sms/readSubscriber.php',
                data: {'variable': dataPhp},
                complete: function(r){
                  var subscriberNumbers = r.responseText;
                   document.getElementById('to').innerHTML = subscriberNumbers;
               }
              });


         } else {
           document.getElementById("to").value = "";
         }

         }


      </script>
   </head>
   <body>
      <fieldset style="width:50%;margin:auto" dir=ltr>
         <form action="" method="POST">
            <table border="0" cellspacing="3" cellpadding="3">
               <tr>
                  <td>Your Balance</td>
                  <td><input type="text" class="form-control" name="Balance" size="20" disabled="disabled" value="<?php
                     echo $Credits;
                     ?>"></td>
               </tr>
               <tr>
                  <td>Mobile No.</td>
                  <td><textarea textarea class="form-control" id="to" name="Mobile" cols="30" rows="5"></textarea><br></td>
                  <td>
                     <div class="checkbox">
                        <label><input type="checkbox" value="subscribers"  onchange="doalert()" id="subscribers" checked>Subscribers</label>
                     </div>
                  </td>
               </tr>
               <tr>
                  <td>Message</td>
                  <td><textarea class="form-control" name="Text" cols="30" rows="5" required></textarea></td>
               </tr>
               <tr>
                  <td>&nbsp;</td>
                  <td><input type="submit" name="Go" value="Send SMS" /></td>
               </tr>
            </table>
         </form>
      </fieldset>
   </body>
</html>

and this is the readSubscriber.php

<?php
require_once('../db_functions.php');

// Connect to the database
$connection = db_connect();
//if connection fails, stop script execution
if (mysqli_connect_errno()) {
    echo "Error: " . $sql . "Connect failed: " . mysqli_connect_error();

} else {

    $query  = "SELECT * FROM phoneSubscribers";
    // Query the database
    $result = db_isExist($query);

    if ($result) {
        $result = db_fetch($query);
        echo json_encode($result);
    } else {
        echo "No result";
    }


}
?>
  • 写回答

3条回答 默认 最新

  • duanjianxi8439 2018-06-26 06:54
    关注

    Set value of the textArea (instead of innerHTML)

    评论

报告相同问题?

悬赏问题

  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)