PHP echo获取值显示标记。

时间:2022-11-19 19:25:00

[The issue has resolved itself for some reason.]

(这个问题由于某种原因自行解决了。)

I've had this piece of code for quite a while now with no issues whatsoever. Today I started getting to work and got an uncaught syntax error, telling me there was a stray "<". I located it and it's a piece of javascript code that gets the id value of the post you are looking at (on the forum).

我有这段代码已经有一段时间了,没有任何问题。今天我开始工作,并得到一个未被发现的语法错误,告诉我有一个偶然的“<”。我找到了它,它是一段javascript代码,它获取您正在查看的文章的id值(在论坛上)。

The code is used for deleting a post using AJAX.

该代码用于使用AJAX删除帖子。

    $('.deletePost').click(function() {
        var delPost = confirm('Are you sure you want to delete this post?');

        if (delPost == true) {
            var xhttp = new XMLHttpRequest();
            var getId = <?php echo $_GET['id'] ?>

            xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    location.replace('index');
                }
            }
            xhttp.open('GET', 'deletepost.php?id=' + getId, true);
            xhttp.send();
        }

        return false;

    });

When loading up the actual website, the getId variable is <br >

加载实际网站时,getId变量为

This code is located in the 'footer.php' file, I use include to get it on every page on the website.

这段代码位于页脚。php的文件,我用include在网站的每个页面上。

First off I use jQuery click to execute this, so the syntax error shouldn't really be a problem on other pages, as I made sure to not use the .deletePost class on anything on any of the other files. Second, it also needs the user to click OK and make the variable true, so to me this just makes no sense.

首先,我使用jQuery click执行此操作,因此在其他页面上语法错误不应该是真正的问题,因为我确保在任何其他文件上都不使用. deletepost类。其次,它还需要用户点击OK并使变量为真,所以对我来说这毫无意义。

The biggest problem is that it makes all the other javascript code both above and below it useless.

最大的问题是,它使上面和下面的所有javascript代码都变得无用。

(I'm not sure if any more code is relevant and I didn't want to make this too long, so please let me know if I need to include anything else.)

(我不确定是否有更多的代码是相关的,我不想让它太长,所以请告诉我是否需要包含其他内容。)

ID in $_GET['id'] is generated like so:

$_GET中的ID [' ID ']生成如下:

`

$sql = '
        SELECT c.cat_name, c.cat_id, c.cat_description, t.topic_id, t.topic_by, t.topic_subject, t.topic_date
        FROM categories c LEFT JOIN topics t ON c.cat_id=t.topic_cat
        WHERE topic_date = ( SELECT MAX(t2.topic_date) FROM topics t2 WHERE t2.topic_cat = t.topic_cat ) AND c.cat_id<4
        ORDER BY t.topic_date DESC
        ';

    $result = mysqli_query($conn, $sql);

    if (!$result) {
        echo 'Could not display categories. Error: ';// . mysqli_error($conn);
    } else {
        if (mysqli_num_rows($result) == 0) {
            echo 'No categories found in the database.';
        } else {
            echo '
                <table>
                <h3>Latest topics in categories</h3>
                <tr>
                    <th>Category</th>
                    <th>Latest Topic</th>
                </tr>
            ';

            while ($row = mysqli_fetch_assoc($result)) {
                echo '<tr>
                    <td class="leftpart">
                        <h3><a href="category?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'] . '
                    </td>
                    <td class="rightpart">
                        <a href="topic?id=' . $row['topic_id'] . '">' . $row['topic_subject'] . '<br>By <b>' . $row['topic_by'] . '</b></a>
                    </td>
                </tr>
                ';
            }

            echo '</table>';
        }
    }`

This is the error. It appears in F12. PHP echo获取值显示标记。

这是错误的。它出现在F12。

1 个解决方案

#1


2  

You have to use quotes around the php statement like this. And make sure on the server side typecast the id value to integer

必须像这样在php语句周围使用引号。并确保在服务器端将id值类型转换为integer

var getId = "<?php echo $_GET['id']; ?>";

first make sure that the GET parameter gets the id value correctly, check on the inspect element in browser , If your Id populated correctly from database it should be like this (I don't know your Ids and values)

首先,确保GET参数正确地获取id值,检查浏览器中的inspect元素,如果您的id从数据库中正确地填充,应该是这样的(我不知道您的id和值)

<h3><a href="topic?id=1">correct cat name</a></h3>correct cat_description

And the GET parameter need a key "id" to get its value. The error saying that the "id" is not there at the URL as a GET parameter.

GET参数需要一个键“id”来获取它的值。说明“id”作为GET参数不在URL处的错误。

check the URL address also it should contain an "id" parameter

检查URL地址,它也应该包含一个“id”参数

If your id from the database is some sort of string. enclose the string inside urlencode($row['cat_id']) function.

如果数据库中的id是某种字符串。将字符串括在urlencode($row['cat_id']])函数中。

Other than that i don't see any errors. Pardon me if i misunderstood the question

除此之外,我没有看到任何错误。如果我误解了这个问题,请原谅

#1


2  

You have to use quotes around the php statement like this. And make sure on the server side typecast the id value to integer

必须像这样在php语句周围使用引号。并确保在服务器端将id值类型转换为integer

var getId = "<?php echo $_GET['id']; ?>";

first make sure that the GET parameter gets the id value correctly, check on the inspect element in browser , If your Id populated correctly from database it should be like this (I don't know your Ids and values)

首先,确保GET参数正确地获取id值,检查浏览器中的inspect元素,如果您的id从数据库中正确地填充,应该是这样的(我不知道您的id和值)

<h3><a href="topic?id=1">correct cat name</a></h3>correct cat_description

And the GET parameter need a key "id" to get its value. The error saying that the "id" is not there at the URL as a GET parameter.

GET参数需要一个键“id”来获取它的值。说明“id”作为GET参数不在URL处的错误。

check the URL address also it should contain an "id" parameter

检查URL地址,它也应该包含一个“id”参数

If your id from the database is some sort of string. enclose the string inside urlencode($row['cat_id']) function.

如果数据库中的id是某种字符串。将字符串括在urlencode($row['cat_id']])函数中。

Other than that i don't see any errors. Pardon me if i misunderstood the question

除此之外,我没有看到任何错误。如果我误解了这个问题,请原谅