简体   繁体   English

在PHP中使用Ajax发布数据

[英]Post data using ajax in php

Hello guys i want to post data using ajax under PHP cods. 大家好,我想在PHP编码下使用Ajax发布数据。 Other page use this data to start a loop. 其他页面使用此数据开始循环。 so ajax hold till loop will finish this cods not working for me. 因此,ajax保持直到循环将完成此鳕鱼对我不起作用。

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>';

Place this in ur php file 将此放置在您的php文件中

  <?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 这个php标签外

 <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>
  <script>
  var postid = '<?php echo $postid ?>';
  $.ajax({
    url: "http://example.com/example.php",
    type: "POST",
    data: {
      "id":postid
    },
    success: function(data){
     console.log(data);
******* write code *******
});      
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM