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条回答 默认 最新

  • dpftppc9674 2015-07-24 05:01
    关注

    Now this can be done in many ways.. One of them is Following.. Try this:

    Index.php file

    <form method="get" id="calculator_form">
      <input name="one" type="text" id="one">
      <input name="two" type="text" id="two">
      <input type="submit" name="submit">
    </form>
    
    <div class="result"></div>
    
    <script type="text/javascript">
    $(document).ready(function(){
        $("#calculator_form").on('submit', function(event){
            event.preventDefault();
    
           var one = $('#one').val(); // Taking value of input one
           var two = $('#two').val(); // Taking value of input two
    
           $.get( "ajax.php", { one:one, two:two }). done( function( data ) {
    
              $('.result').html(data); // Printing result into result class div
            });
    
        });
    });
    </script>
    

    ajax.php

    <?php if(isset($_GET["one"])) { ?>
    <div>
      <?php echo $_GET["one"] . " " . $_GET["two"]; ?>
    </div>
    <?php } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值