Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.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 jQuery Ajax没有发布到PHP文件_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript jQuery Ajax没有发布到PHP文件

Javascript jQuery Ajax没有发布到PHP文件,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我是AJAX新手,所以我知道我的代码中可能会有一个愚蠢的错误,但无论如何我会继续 我创建了一个函数,单击按钮时调用该函数。该函数调用jquery的.ajax()方法。我将数据发送到一个名为“delete_post.php”的文件中 HTML: <button class="btn btn-primary" onclick="deletePost(<?php echo $_GET["id"]; ?>);">Yes</button> function delet

我是AJAX新手,所以我知道我的代码中可能会有一个愚蠢的错误,但无论如何我会继续

我创建了一个函数,单击按钮时调用该函数。该函数调用jquery的
.ajax()
方法。我将数据发送到一个名为“delete_post.php”的文件中

HTML:

<button class="btn btn-primary" onclick="deletePost(<?php echo $_GET["id"]; ?>);">Yes</button>
function deletePost(postid) {
    $.ajax({
        type: 'post',
        url: "delete_post.php?id="+postid,
        success: function(data) {
            if(data.error == true) console.log('error');
                    else console.log('problem?');
        }
    });
}
上面的代码正在调用
.ajax()
函数,但没有将“问题”记录到控制台中

以下是PHP文件:

<?php
require_once '...';
if(isset($_GET["id"])) {
    $id = $_GET["id"];
    echo "YEAH!";
} else {
    header("location: index.php");
}
?>


问题是什么?我如何解决它?

以下部分中有一个转义问题:

onclick="deletePost(<?php echo $_GET["id"]; ?>);"

以下部分有一个逃逸问题:

onclick="deletePost(<?php echo $_GET["id"]; ?>);"

这是您的工作代码,在AJAX中使用“POST”,在PHP文件中使用“GET”

trigger.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script>
function deletePost(postid) {
    $.ajax({
        type: 'GET',
        url: "http://localhost/overflow/delete_post.php?id="+postid,
        success: function(data) {
           console.log('success:'+data);
        }, error: function(xhr, status, error){
            console.log(xhr.responseText);
        }
    });
}
</script>
</head>

<body>   

<button class="btn btn-primary" onclick="deletePost(9); return false;">Yes</button>
</body>
</html>

无标题文件
函数deletePost(postid){
$.ajax({
键入:“GET”,
url:“http://localhost/overflow/delete_post.php?id=“+posted,
成功:功能(数据){
console.log('success:'+数据);
},错误:函数(xhr,状态,错误){
console.log(xhr.responseText);
}
});
}
对
delete_post.php

<?php
//require_once '...';
if(isset($_GET["id"])) {
    $id = $_GET["id"];
    echo "YEAH!";
     //do something
} else {
   // header("location: index.php");
     echo "not set";
}
?>


试试这个,让我们详细而清楚地知道你的问题。祝你好运

这是你的工作代码,在AJAX中你在做“POST”,在PHP文件中你在使用“GET”

trigger.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script>
function deletePost(postid) {
    $.ajax({
        type: 'GET',
        url: "http://localhost/overflow/delete_post.php?id="+postid,
        success: function(data) {
           console.log('success:'+data);
        }, error: function(xhr, status, error){
            console.log(xhr.responseText);
        }
    });
}
</script>
</head>

<body>   

<button class="btn btn-primary" onclick="deletePost(9); return false;">Yes</button>
</body>
</html>

无标题文件
函数deletePost(postid){
$.ajax({
键入:“GET”,
url:“http://localhost/overflow/delete_post.php?id=“+posted,
成功:功能(数据){
console.log('success:'+数据);
},错误:函数(xhr,状态,错误){
console.log(xhr.responseText);
}
});
}
对
delete_post.php

<?php
//require_once '...';
if(isset($_GET["id"])) {
    $id = $_GET["id"];
    echo "YEAH!";
     //do something
} else {
   // header("location: index.php");
     echo "not set";
}
?>


试试这个,让我们详细而清楚地知道你的问题。祝您好运

正如我们在聊天室中讨论的,您可以这样使用它:

JS:

PHP:


正如我们在聊天室中讨论的,您可以这样使用它:

JS:

PHP:



您是否尝试记录
数据
本身?不,数据不是JSON。@JakubMichálek如果直接转到
delete_post.php?id={yourID}
而不是使用AJAX,您会看到
是的还是不?您需要在PHP中使用
$data
,而不仅仅是
数据
。不要混合语言…:)您是否尝试记录
data
本身?不,数据不是JSON。@JakubMichálek如果直接进入
delete_post.php?id={yourID}
而不是使用AJAX,您会看到
是的还是不?您需要在PHP中使用
$data
,而不仅仅是
数据
。不要混合语言…:)不需要转义,内部的
是PHP格式,外部是HTML格式当然,这需要转义或使用单个引号。整个html是一个字符串,当onclick遇到匹配的双引号eno时,它将突然结束,输出将是
onclick=“deletePost(6);”
例如,那里没有额外的引号…不需要转义,内部的
是PHP,外部的是html。)当然,这需要转义或使用单个引号。整个html是一个字符串,onclick在遇到匹配的双引号时会突然结束。例如,输出为
onclick=“deletePost(6);”
,没有额外的引号。。。
 <?php 

    require_once '...'; 
    if(isset($_POST["id"])) { 
    $data['res'] = 'yes'; 
    echo json_encode($data); 
    } else { 
    header("location: index.php"); 
    } 
    ?>