weixin_33721427 2018-09-14 09:04 采纳率: 0%
浏览 23

发送变量到PHP文件

In a WordPress post I'm trying to send data to a PHP file stored in the root folder of my website with this code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript">
console.log('hi');
var cen = document.getElementById("centro").value;
$.ajax({
        url: 'centroUser.php', 
        type: "POST",
        data: { 'cen': cen },
        success: function(data){
            console.log(data);
        }
}); 
</script>

centroUser.php:

<?php
   $uid = $_POST['cen'];
   echo($uid);   
?>

The problem is that I can't get it to work, the variable $uid doesn't get echoed and even the console.log('hi') doesn't get called. I'm new to AJAX so I don't really know what I'm doing wrong, I have tried looking for other answers but I couldn't find something that worked.

  • 写回答

1条回答 默认 最新

  • helloxielan 2018-09-14 09:08
    关注

    Your <script> tag has a src and a body.

    Try:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    
    console.log('hi');
    var cen = document.getElementById("centro").value;
    $.ajax({
            url: 'centroUser.php', 
            type: "POST",
            data: { 'cen': cen },
            success: function(data){
                console.log(data);
            }
    }); 
    </script>
    

    If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI - see here.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效