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.

    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)