调用php函数按钮单击ajax并解析参数

时间:2016-12-25 16:58:09

标签: javascript php jquery ajax

我想用ajax解析参数......是不是很可能?如果是这样,我怎么能这样做呢?

HTML / JS

<script>
    function myAjax() {
       $.ajax({
          type: "POST",
          url: '/ajax.php',
          data:{action:'follow'},
          success: function() {
             console.log("called");
          }
      });
    }
</script>
<button type='button' name='follow' id='follow' onclick='myAjax()'>Follow @<?php echo $profile ?></button>

ajax.php:

session_start();
require_once 'init.php';
if($_POST['action'] == 'follow') {
    User::follow($conn, $_SESSION["name"], $profile); 
// $conn is a db connection, $profile is the param I would like to parse through
} else {
    echo 'no req';
} 

1 个答案:

答案 0 :(得分:0)

function myAjax(obj) { 
$.ajax({ 
   type: "POST",
    url: '/ajax.php', 
   data:obj,
    success: function() { console.log("called"); } 
   }); 
   }

像这样使用:

  myAjax({action:" follow",id:1234});