你如何使用ajax将javascript变量输入到php函数中?

时间:2014-04-04 02:59:45

标签: javascript php jquery ajax

我在这做错了什么?一切正常,控制台没有错误,但也没有控制台日志说它成功了

带脚本的

索引文件:

    <script type="text/javascript">

       function upVote(picNum)
          {

              var pictureNumber = parseInt($("#" + picNum).attr("id"));


              $.ajax({
                 url: "upload/pics/changeVote.php",
                 data: {"picNum":pictureNumber},
                 type:'post',
                 dataType:'json',
                 success: function(output_string){
                    PictureNumber = output_string['picturenumber'];
                    alert(PictureNumber);
                }
              });
              var currentVote = parseInt($("#" + picNum).attr("value"));  
              alert("pictureNumber is " + pictureNumber + "and currentVote is " + currentVote); //here to help me, no functionality

              $newVote = currentVote + 1;

              alert($newVote); //here to help me
          }
   </script>

/upload/pics/changeVote.php

<?php

   $picNum = $_POST['picNum'];
function otherFileFunc($pic){
  $final = $pic + 1;
  return $final;
}
$outputnumber = function($picNum);
$array = ('picturenumber' => $outputnumber);
echo json_encode($array);

?>

1 个答案:

答案 0 :(得分:2)

错误 /upload/pics/changeVote.php

$outputnumber = function($picNum);

必须是:

$outputnumber = otherFileFunc($picNum);

您无法使用function(),您应该使用功能名称。