在php中使用ajax发布数据

时间:2017-01-09 11:14:40

标签: javascript php jquery ajax

大家好我想在PHP鳕鱼下使用ajax发布数据。其他页面使用此数据来启动循环。所以ajax hold to loop将完成这个鳕鱼不适合我。

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 个答案:

答案 0 :(得分:0)

将它放在你的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);
    ?>

这个外部的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>

答案 1 :(得分:0)

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