dslpofp041310584 2015-07-23 10:59
浏览 83
已采纳

使用AJAX刷新div

Index.php

...
<form id="calculator_form" name="form" action="" method="get" >
  <input name="one" type="text">
  <input name="two" type="text">
  <input type="submit">
</form>
...
<!-- refresh area -->
<?php if(isset($_GET["one"])) { ?>
<div>
  <?php echo $_GET["one"] . " " . $_GET["two"]; ?>
</div>
<?php } ?>
<------------------->

I would like to submit the form and reload the refresh area indicated above. I know this can be achieved by using AJAX but I'm not quite sure how.

I have tried putting the refresh area in a separate ajax.php file and using JQuery but it didn't work;

$(document).ready(function() { 
   $("#calculator_form").submit(function(event) {
      event.preventDefault();
      $("#divtoappend").load("ajax.php", data);
   })
}) 

I've also tried using $.get() but to no avail.

I'm able to send the data back and forth to a seperate php page but I'm stuck trying to achieve what I am looking for.

EDIT:

The code that I posted was quickly written and the syntax isn't the issue in question, I'm merely wondering how I can refresh a <div> under the form so that it will once again do the if(isset($_GET["one"])) check and print the updated php variables.

EDIT 2:

My code is now as follows:
Index.php

...
<form id="calculator_form" name="form" action="" method="get" >
  <input name="one" type="text">
  <input name="two" type="text">
  <input type="submit">
</form>
...
<div id="append">
  <!-- where I want the ajax response to show -->
</div>
...

ajax.php

<?php if(isset($_GET["one"])) { ?>
<div>
   <?php echo $_GET["one"] . " " . 4_GET["two"]; ?>
</div>
<!-- assume there's n number of divs -->
<?php } ?>

Now I want the ajax.php div to append to the #append div in index.php. There has to be a better way than altering the ajax.php and using echo:

ajax.php (with echo)

 <?php 
 if(isset($_GET["one"])) { 
    echo "<div>". $_GET["one"] . " " . $_GET["two"] . "</div>";
 } 
 ?>

So, as ajax.php could be very large, is there a better solution than just echoing data from ajax.php to index.php?

  • 写回答

4条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 yolov5摄像头识别只能检测第一帧图像
      • ¥15 有没有可以下载钉钉群文件视频的软件
      • ¥15 如何用LaTeX打出如下的表格
      • ¥15 程序填空补充完整的代码一共三题
      • ¥15 在MySQL中使用while建表,报错
      • ¥15 python导入pyautogui报错,网上也没有找到解决方法如何解决?
      • ¥15 关于IPSEC和NAT SERVER同时使用的情况下,该如何解决客户端访问服务器的问题
      • ¥15 eclipse项目在idea运行部署到云服务器
      • ¥100 sql server image类型转换
      • ¥15 关于matlab的问题