weixin_33674437 2020-04-10 18:08 采纳率: 0%
浏览 32

用ajax在php页面中写

I have an a.php page containing a variable x = 10; and a page b.php which contains var y = 10; my question is can i add x + y and write the result in b.php? knowing that the ajax code is in a.php

<?php
if (isset($_POST['y']) &&  isset($_POST['x']) ) {

    $y = 50;
    $x=$_POST['x'];

    echo  $x+$y  ;

}

?>



<script type="text/javascript">

$('button').on('click' , function(){
  $.post('b.php' , { x:10, y:10  } , function(data){
    $('div').html(data);
  } );
} );

</script>
  • 写回答

1条回答 默认 最新

  • weixin_33701617 2020-04-10 19:02
    关注

    in first sight i think that you should use java script ajax on your a.php for sending data to page b.php but that is not enough so for getting you result a+b=something you need to use something called java script concurrence and those technique allow you to listen for a period of time on the existence of a so i will try to give you the solution o some steps :

    Step 1: you've done your ajax sending data

    step 2: you need to go check on the java script concurrence you have set interval and set timeout so use one of them but you will need to use set interval cause it will be more efficient

    step 3 :use a Php condition called !empty()to check each period of time with the set Interval that a don't equal the default value if it's equal to that default value you should use clear Interval since keeping the execution of set Interval doesn't have any meaning and you will consume of the load of your page and your page will run slower

    step 4: you've done with everything and you can do your operation.

    i will let a short code for set Interval and you can find them on w3schools with execution example

    
            var myVar;
    
        function myFunction() {
          myVar = setInterval(alertFunc, 3000);
        }
    
        function alertFunc() {
          alert("Hello!");
        }
    
    

    for the clearInterval

    var myVar = setInterval(myTimer, 1000);
    
        function myTimer() {
          var d = new Date();
          var t = d.toLocaleTimeString();
          document.getElementById("demo").innerHTML = t;
        }
    
        function myStopFunction() {
          clearInterval(myVar);
        }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装