weixin_33735077 2017-01-09 11:14 采纳率: 0%
浏览 25

在PHP中使用Ajax发布数据

Hello guys i want to post data using ajax under PHP cods. Other page use this data to start a loop. so ajax hold till loop will finish this cods not working for me.

if(isset($_POST['submit'])) {
    $token = $_SESSION['token'];
    if(!isset($token)) {
        exit;
    }
    $postid = $_POST['id'];
    $postid = str_replace(substr($postid, 0, strpos($postid, '_')) . '_', '', $postid);
    if(isset($postid)) {
        if (time()- $times['time'] < $ttime) {
            header("Location: index.php?info=TimeLimit");
        } else{
            $sql1 = "REPLACE INTO cookie (ip,time,waktu) VALUES ( '$ip','$time','$waktu')";
            $result1 = mysqli_query($mysqli,$sql1) or die(mysqli_error());
            echo'<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
            <script>  
                var postid = '.$postid.';
                $.ajax({
                    type: "POST",
                    data: {id:postid},
                    url: "http://example.com/example.php",
                    success: function(data){
                        //data will contain the vote count echoed by the controller i.e.  
                        "yourVoteCount"
                        //then append the result where ever you want like
                        $("span#votes_number").html(data); 
                    }
                });
            </script>';
  • 写回答

2条回答 默认 最新

  • elliott.david 2017-01-09 11:22
    关注

    Place this in ur php file

      <?php
            if(isset($_POST['submit'])) {
              $token = $_SESSION['token'];
              if(!isset($token)){exit;}
              $postid = $_POST['id'];
              $postid = str_replace(substr($postid, 0, strpos($postid, '_')) . '_', '', $postid);
        ?>
    

    This outside php tag

     <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
          <script>
          var postid = '<?php echo $postid ?>';
          $.ajax({
            url: "http://example.com/ho_likes.php",
            type: "POST",
            data: {("id":postid)},
            success: function(data){
             $("span#votes_number").html(data);
            },
            error:function(data){
             alert(data);
           }
        });      
        </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度