开发者

How do I show the print with AJAX/jQuery?

开发者 https://www.devze.com 2022-12-29 08:51 出处:网络
So I\'m trying to understand this whole AJAX/jQuery thing. Right now, when I run this PHP script alone, I would have to wait and watch the wheel spin until it\'s done with the loop and then it will lo

So I'm trying to understand this whole AJAX/jQuery thing. Right now, when I run this PHP script alone, I would have to wait and watch the wheel spin until it's done with the loop and then it will load.

while ( $row = mysql_fetch_array($res) ) {
    postcode_to_storm( $row['Test'] );

    $dom = new DOMDocument();
    @$dom->开发者_如何学编程;loadHTML($result);
    $xPath = new DOMXPath($dom);

    $failInvite = 'Rejected';
    $findFalse = strpos($result, $failInvite);

    if ( $findFalse == true ) {
        $array[$i] = $row['Test'];
        $i++;
        echo $array[$i]};
    } 

}

Now, how do I use AJAX/jQuery to show echo $array[$i]}; everytime it is invoked instead of waiting for the whole process to complete?


The way AJAX works is that with the first request you write the basic HTML of your web page, including some javascript that calls back to the server and asks for more data. Depending on how you plan to send your data, it may make one or more requests after the page is rendered to get more data. Using AJAX will require that you rethink about how you're delivering your data. For example, you'll need one "script" to load the page, then another "script" to get the data -- of course, they could be the same, just with different parameters. I'll add a simple example to demonstrate since refactoring your example would require more understanding of your data and how it's delivered. This example is from w3schools.com.

HTML:

<script type="text/javascript">
   $(function() {
       $('#users').change(function() {
          // here's the AJAX bit 
          $.get( '/users/load.php?q=' + $(this).val(),
               function(html) {
                  $('#txtHint').html(html);
          });
       });
   });
</script>
</head>
<body>

<form>
<select name="users">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

PHP

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ajax_demo", $con);

$sql="SELECT * FROM user WHERE id = '" . mysql_real_escape_string( $q ) . "'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>


This is similar to COMET. Take a look at the differences here: http://ajaxian.com/archives/comet-a-new-approach-to-ajax-applications

Here's a post that talks about the difficulties in PHP in particular with a LAMP stack: http://www.phpclasses.org/blog/post/58-Responsive-AJAX-applications-with-COMET.html - may be a little dated as it was posted in 2006, but you get the point.

Typically when you talk about vanilla "ajax," you are talking about a client initiated request for some finite information (the http request completes), without an explicit page reload.


So, you want server to send every piece of data separately, once it's produced? I'm afraid, there is no such feature with http requests. You have to close it and then response is sent to the browser.

You may consider sending several requests, with some kind of pagination.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号