简体   繁体   中英

Reload PHP code after Ajax

i am still learning here folks, so excuse my code if its messy. I have 3 separate pages as follow:
AccountView.php---Where the data is shown:

                      <div class="panel-body">
                        <div id="opentasks">
                        <!-- view Open tasks -->
                            <?php $task= new acct; echo $task->accountActiveTasks()?>
                        </div>
                        <!-- end view open tasks -->
                        <!-- start adding quick task -->
                          <div class="chat-form">
                             <!--  <form method="post" action="addTask.php"> -->
                              <div class="input-cont ">
                                  <input type="text" class="form-control col-lg-12" placeholder="Add a quick Task..." name ="quicktask" id="quicktask">
                              </div>
                              <div class="form-group">
                                  <div class="pull-right chat-features">
                                      <a href="javascript:;">
                                          <i class="icon-camera"></i>
                                      </a>
                                      <a href="javascript:;">
                                          <i class="icon-link"></i>
                                      </a>
                                      <input type="submit" class="btn btn-danger" name="AddTask" value="Add" onclick="add_quick_task()">


                                      <input type="hidden" name="acct" id="acct" value="<?php echo $_REQUEST['acctname']?>">
                                      <input type="hidden" name="user" id="user" value="<?php $username = $_SESSION['username']; echo $username?>"> 
                                  </div>
                              </div>

tasks.js --> graps one input from AccountView.php

function add_quick_task(){
            var xmlhttp;
            if (window.XMLHttpRequest) {

                xmlhttp = new XMLHttpRequest();
            }else{

                xmlhttp = new ActiveXObject("microsodt.XMLHTTP");
            }

        var acct = document.getElementById('acct').value;
        var quicktask = document.getElementById('quicktask').value;
        // var taskstatus = 'Active';
        var user = document.getElementById('user').value;

        xmlhttp.onreadystatechange = function(){

            if (xmlhttp.readyState==4) {
                document.getElementById('panel-body').innerHTML = xmlhttp.responseText;

            }   
        }
            url = "addTask.php?acct="+acct+"&quicktask="+quicktask+"&user="+user;
            xmlhttp.open("GET", url, true);
            xmlhttp.send();

        }

account_functions.php ---Where the PHP function gets the data from mysql

     public function accountActiveTasks(){
      $varacctname = $_REQUEST['acctname'];
      $varViewActiveTasks = mysql_query("SELECT * from tasks WHERE taskresource='$varacctname' && taskstatus='Active'");

      while ($rows= mysql_fetch_array($varViewActiveTasks)) {
           $this->accttask = $rows['tasktitle'];
           $this->acctTaskStatus = $rows['taskstatus'];
           $this->acctTaskOwner = $rows['taskowner'];

                              echo "<div class=\"timeline-messages\">

                          <div class=\"msg-time-chat\">
                              <a href=\"#\" class=\"message-img\"><img class=\"avatar\" src=\"img/chat-avatar.jpg\" alt=\"\"></a>
                              <div class=\"message-body msg-in\">
                                  <span class=\"arrow\"></span>
                                  <div class=\"text\">
                                      <p class=\"attribution\"><a href=\"#\">$this->acctTaskOwner at 1:55pm, 13th April 2013</a></p>
                                      <p> $this->accttask</p>
                                  </div>
                              </div>
                          </div>
                          <!-- /comment -->
                      </div>";
      }

My goal is that every time the tasks.js runs, the php code in AccountView.php is refreshed. sort of like appending the data. Currently the data gets pushed to the mysql table but i have to refresh to see it. I would like for it to show automatically on success.

What is panel-body that is a class.In the response you are adding response to it i think your intention is to have id="panel-body"

If you want to append the response to the "panel-body" Div you may use this jquery statement

           if (xmlhttp.readyState==4) {
            $('#panel-body').append(xmlhttp.responseText);

        }  

Try it

window.location.reload();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM