Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过AJAX将ID发送到另一个文件并在该文件中使用该ID_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 如何通过AJAX将ID发送到另一个文件并在该文件中使用该ID

Javascript 如何通过AJAX将ID发送到另一个文件并在该文件中使用该ID,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个HTML文件,其中包含如下各种字段(我将只显示一个示例,因为其余的都是相同的): 您需要指定数据的键: data: { 'id': id } 代码段中还存在语法错误: function getdata(id){ alert(id); $.ajax({ type: "POST", url: "activations.php", data: { 'id': id // < no need fo

我有一个HTML文件,其中包含如下各种字段(我将只显示一个示例,因为其余的都是相同的):


您需要指定数据的键:

data: { 'id': id }
代码段中还存在语法错误:

function getdata(id){
    alert(id);  
    $.ajax({
        type: "POST",
        url: "activations.php",
        data: {
            'id': id // < no need for semi-colon, properties are key->value based, 'key': value
        }
    }); // << no closing bracket and semi-colon
} // << close your function
始终检查它是否已设置:

if (isset($_POST["id"])) $id = $_POST["id"]; else exit;
回答OP的特定问题:

您需要打开到SQL server的PDO连接(例如MySQL)

您必须将ajax请求的数据类型设置为JSON,或者使用
$.parseJSON(数据)自行解析数据

您可以在向ajax添加成功函数时使用返回的数据:

function getdata(id){
    alert(id);  
    $.ajax({
        type: "POST",
        url: "activations.php",
        data: {
            'id': id // < no need for semi-colon, properties are key->value based, 'key': value
        },

        success: function(data) {
            var achievements = $.parseJSON(data);

            // use achievements variable
        }
    }); // << no closing bracket and semi-colon
} // << close your function
函数getdata(id){ 警报(id); $.ajax({ 类型:“POST”, url:“activations.php”, 数据:{ 'id':id/<不需要分号,属性基于键->值,'key':值 }, 成功:功能(数据){ var成就=$.parseJSON(数据); //使用成就变量 }
});//将
id;
更改为
'id':id
并且没有分号(

您有一些打字错误(Ajax调用中没有右括号、不需要的分号等),但主要需要将数据作为键值对提供。注意:只有当键名不是单个单词时才需要引号(没有空格或连字符/-etc):


函数getdata(id){
警报(id);
$.ajax({
类型:“POST”,
url:“activations.php”,
数据:
{
id:id
}
});
}

关于更多的事情,您需要关闭ajax
})关闭函数之前:)以及如何将activations.php的SQL结果返回到getdata()函数所在的HTML中?@MONZTAAA我回答了您的问题,但别忘了这不是为您编写代码,我会对代码进行解释,给我几分钟时间。@MONZTAAA另外,我使用PDO是因为mysql_*函数不受欢迎,不要使用mysql_*
$_POST["id"]
if (isset($_POST["id"])) $id = $_POST["id"]; else exit;
if (isset($_POST["id"])) $id = $_POST["id"]; else exit;

$db = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8', 
              'username', 
              'password'); // Open a connection to your database
$q = $db->prepare("SELECT * FROM achievements WHERE id=?"); // Get all achievements where id=$id
$q->execute(array($id)); // Run the query

$rows = $q->fetchAll(); // Fetch the query, associated array

echo json_encode($rows); // Convert the $rows to json, so javascript can read it
function getdata(id){
    alert(id);  
    $.ajax({
        type: "POST",
        url: "activations.php",
        data: {
            'id': id // < no need for semi-colon, properties are key->value based, 'key': value
        },

        success: function(data) {
            var achievements = $.parseJSON(data);

            // use achievements variable
        }
    }); // << no closing bracket and semi-colon
} // << close your function
<script type="text/javascript">
    function getdata(id){
    alert(id);  
        $.ajax({
           type: "POST",
           url: "activations.php",
           data: 
           {
              id: id
           }
        });
    }
</script>