AJAX不起作用(调用php并返回结果)

时间:2018-06-26 06:50:08

标签: javascript php ajax

我正试图使一个复选框调用php代码,然后取回要打印在textarea中的结果。

现在,问题在于php文件无法在textarea上打印结果,或者Ajax无法正常工作。我确信MySQL代码没有错。

这是form.php代码:

    <!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>

这是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 个答案:

答案 0 :(得分:0)

设置textArea中的innerHTML(而不是create or replace PROCEDURE SimpleProcedure(P_ID VARCHAR2, P_INFO OUT CLOB) AS V_PROJ_ID VARCHAR2(50); V_PROJ_NAME NVARCHAR2(1000); V_APP_ID VARCHAR2(50); V_APP_STATUS VARCHAR2(50); v_SA VARCHAR2(30000); CURSOR SUB_ATTACH_CURSOR(APP_ID VARCHAR2) IS SELECT S.SUBMISSION_ID, SA.FILE_ID, SA.STATUS_CODE, E.FILENAME_TEXT ... BEGIN SELECT P.PROJECT_ID, P.NAME_TEXT, A.APP_ID, A.STATUS_CODE ... P_INFO := 'Information: Proj id: ' || V_PROJ_ID || ', Proj name: ' || V_PROJ_NAME || ', App id: ' || V_APP_ID || ', App status: ' || V_APP_STATUS || ','; FOR SA IN SUB_ATTACH_CURSOR(V_APP_ID) LOOP v_SA := 'Submission id: ' || SA.SUBMISSION_ID || ',file id: ' || SA.FILE_ID || ', file name: ' || SA.FILENAME_TEXT || ', file Status: ' || SA.STATUS_CODE || ' '; DBMS_LOB.APPEND(P_INFO, v_SA); END LOOP; END SimpleProcedure;

答案 1 :(得分:0)

像这样改变。它将起作用。您不能将变量(“变量”)替换为值 并且您需要使用值声明变量。

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


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

}
</script>

答案 2 :(得分:0)

我想完成

以我的形式:

        $(document).ready(function() {

        var checkboxElem = document.getElementById("subscribers").checked;
  if (checkboxElem === true) {
  //Read databank for Results

                var resp = $("#response");
                $.ajax({
                           type: 'POST',
                url: 'assets/php/readSubscriber.php',
                success: function(data) {
                     document.getElementById('to').value = data;

                }
                });



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


         });



function doalert() {
  var checkboxElem = document.getElementById("subscribers").checked;
  if (checkboxElem === true) {
  //Read databank for Results

                var resp = $("#response");
                $.ajax({
                           type: 'POST',
                url: 'assets/php/readSubscriber.php',
                success: function(data) {
                     document.getElementById('to').value = data;

                }
                });



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

}

在调用的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);
        $jsonData = array();
        while ($row = $result->fetch_assoc()) {
            $jsonData[] = $row['phone'];

        }
        array_walk_recursive($jsonData, function($key, $value)
        {
            echo $key . ', ';
        });
    } else {

    }


}
?>