为什么将文件移动到文件夹后,ajax无法获取我的php文件?

huangapple go评论62阅读模式
英文:

Why can't ajax get my php file when I move the file into a folder?

问题

目前,我有一个看起来像这样的index.php页面:
为什么将文件移动到文件夹后,ajax无法获取我的php文件?

这是我的代码:

index.php:

<div style="width: 90%; margin:0 auto;">
    <form class="mb-3" style="width: 150px;">
        <label for="floor">Floor:</label>
        <select class="form-select" name="floor" id="floorSelect" onkeyup="filterByFloor()">
            <option value="" selected>All Floors</option>
            <?php
            $sql = "SELECT * FROM d_floor";
            $floors = mysqli_query($conn, $sql);
            while ($floor = mysqli_fetch_array($floors, MYSQLI_ASSOC)):
                ;
                ?>
                <option value="<?php echo $floor['fname']; ?>">
                    <?php echo $floor['fname']; ?>
                </option>
            <?php endwhile; ?>
        </select>
    </form>
</div>

<div id="displayTable" class="mt-3" style="width: 90%; margin:0 auto;"></div>

<script src="tables/table.js"></script>

<script>
function filterByFloor() {
    var selectJob = document.getElementById("floorSelect"),
        table = document.getElementById("displayTable"),
        tr = table.getElementsByTagName("tr");

    for (var i = 1; i < tr.length; i++) {

        floor = tr[i].getElementsByTagName("td")[9]; // use [0] as the first `td`
        console.log(floor);

        if (floor && floor.innerHTML.indexOf(selectJob.value) > -1) {
            tr[i].style.display = "";
        } else {
            tr[i].style.display = "none";
        }

    }
}

// listen to select changes
document.querySelector('#floorSelect').addEventListener('change', function (e) {
    filterByFloor();
});
</script>

table.js:

//display open table
function displayOpen() {
    var displayOpen = "true";
    $.ajax({
        url: "open.php",
        type: 'post',
        data: {
            displaySend: displayOpen
        },
        success: function (data, status) {
            $('#displayTable').html(data);
        }
    })
}

open.php:

<h2>OPEN</h2>
<?php
// date_default_timezone_set('Asia/Jakarta');
// $currentDateTime = date('Y-m-d H:i:s');
// echo $currentDateTime;
include 'database/connect.php';

if (isset($_POST['displaySend'])) {
    $table = '<table class="table table-sm table-bordered table-striped table-hover">
                <thead class="text-center align-middle">
                    <tr>
                        <th scope="col">NO</th>
                        <th scope="col">DETAILS</th>
                        <th scope="col">MODEL</th>
                        <th scope="col">PIC</th>
                        <th scope="col">OPEN DATE</th>
                        <th scope="col">TARGET DATE</th>
                        <th colspan="3" scope="col">REMARKS</th>
                        <th scope="col">FLOOR</th>
                        <th scope="col">ACTION</th>
                    </tr>
                </thead>
                <tbody class="table-group-divider" style="font-size:0.9rem;">';
    $sql = "SELECT * FROM d_mom WHERE status='OPEN' ORDER BY opendate DESC";
    $result = mysqli_query($conn, $sql);
    $number = 1;
    while ($row = mysqli_fetch_assoc($result)) {
        $id = $row['momid'];
        $details = $row['momdetails'];
        $model = $row['model'];
        $pic = $row['pic'];
        $opendate = $row['opendate'];
        $targetdate = $row['targetdate'];
        $remarks = $row['remarks'];
        $updatedate = $row['updatedate'];
        $updateby = $row['updateby'];
        $floor = $row['floor'];
        // $status = $row['status'];
        $table .= '
                    <tr>
                    <td scope="row" class="text-center">' . $number . '</td>
                    <td>' . $details . '</td>
                    <td class="text-center">' . $model . '</td>
                    <td class="text-center">' . $pic . '</td>
                    <td class="text-center">' . $opendate . '</td>
                    <td class="text-center">' . $targetdate . '</td>
                    <td>' . $remarks . '</td>
                    <td class="text-center">Updated on: ' . $updatedate . '</td>
                    <td>By: ' . $updateby . '</td>
                    <td class="text-center">' . $floor . '</td>
                    <td class="text-center">
                        <div class="dropdown">
                            <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                                Action
                            </button>
                            <ul class="dropdown-menu">
                                <li><a class="dropdown-item" href="#">Update</a></li>
                                <li><a class="dropdown-item" href="#" onclick="updateToUM(' . $id . ')">Under Monitoring</a></li>
                                <li><a class="dropdown-item" href="#" onclick="updateToClosed(' . $id . ')">Closed</a></li>
                                <li><a class="dropdown-item" href="#" onclick="deleteMoM(' . $id . ')">Delete</a></li>
                            </ul>
                        </div>
                    </td>
                </tr>';
        $number++;
    }
    $table .= '</tbody>
               </table>';
    echo $table;
}

?>

使用上面的代码,我能够显示如图片所示的表格。但是,当我将open.php放入tables文件夹中时,表格将不再显示。我尝试将URL更改为tables/open.php/tables/open.php../tables/open.phpmom_tracking/tables/open.php,但都没有起作用。

英文:

Currently I have an index.php page that looks like this:
为什么将文件移动到文件夹后,ajax无法获取我的php文件?

This is my code:

index.php:

&lt;div style=&quot;width: 90%; margin:0 auto;&quot;&gt;
&lt;form class=&quot;mb-3&quot; style=&quot;width: 150px;&quot;&gt;
&lt;label for=&quot;floor&quot;&gt;Floor:&lt;/label&gt;
&lt;select class=&quot;form-select&quot; name=&quot;floor&quot; id=&quot;floorSelect&quot; onkeyup=&quot;filterByFLoor()&quot;&gt;
&lt;option value=&quot;&quot; selected&gt;All Floors&lt;/option&gt;
&lt;?php
$sql = &quot;SELECT * FROM d_floor&quot;;
$floors = mysqli_query($conn, $sql);
while ($floor = mysqli_fetch_array($floors, MYSQLI_ASSOC)):
;
?&gt;
&lt;option value=&quot;&lt;?php echo $floor[&#39;fname&#39;]; ?&gt;&quot;&gt;
&lt;?php echo $floor[&#39;fname&#39;]; ?&gt;
&lt;/option&gt;
&lt;?php endwhile; ?&gt;
&lt;/select&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;div id=&quot;displayTable&quot; class=&quot;mt-3&quot; style=&quot;width: 90%; margin:0 auto;&quot;&gt;&lt;/div&gt;
&lt;script src=&quot;tables/table.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
function filterByFloor() {
var selectJob = document.getElementById(&quot;floorSelect&quot;),
table = document.getElementById(&quot;displayTable&quot;),
tr = table.getElementsByTagName(&quot;tr&quot;);
for (var i = 1; i &lt; tr.length; i++) {
floor = tr[i].getElementsByTagName(&quot;td&quot;)[9]; // use [0] as the first `td`
console.log(floor);
if (floor &amp;&amp; floor.innerHTML.indexOf(selectJob.value) &gt; -1) {
tr[i].style.display = &quot;&quot;;
} else {
tr[i].style.display = &quot;none&quot;;
}
}
}
// listen to select changes
document.querySelector(&#39;#floorSelect&#39;).addEventListener(&#39;change&#39;, function (e) {
filterByFloor();
});
&lt;/script&gt;

table.js:

//display open table
function displayOpen() {
var displayOpen = &quot;true&quot;;
$.ajax({
url: &quot;open.php&quot;,
type: &#39;post&#39;,
data: {
displaySend: displayOpen
},
success: function (data, status) {
$(&#39;#displayTable&#39;).html(data);
}
})
}

open.php:

&lt;h2&gt;OPEN&lt;/h2&gt;
&lt;?php
// date_default_timezone_set(&#39;Asia/Jakarta&#39;);
// $currentDateTime = date(&#39;Y-m-d H:i:s&#39;);
// echo $currentDateTime;
include &#39;database/connect.php&#39;;
if (isset($_POST[&#39;displaySend&#39;])) {
$table = &#39;&lt;table class=&quot;table table-sm table-bordered table-striped table-hover&quot;&gt;
&lt;thead class=&quot;text-center align-middle&quot;&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot;&gt;NO&lt;/th&gt;
&lt;th scope=&quot;col&quot;&gt;DETAILS&lt;/th&gt;
&lt;th scope=&quot;col&quot;&gt;MODEL&lt;/th&gt;
&lt;th scope=&quot;col&quot;&gt;PIC&lt;/th&gt;
&lt;th scope=&quot;col&quot;&gt;OPEN DATE&lt;/th&gt;
&lt;th scope=&quot;col&quot;&gt;TARGET DATE&lt;/th&gt;
&lt;th colspan=&quot;3&quot; scope=&quot;col&quot;&gt;REMARKS&lt;/th&gt;
&lt;th scope=&quot;col&quot;&gt;FLOOR&lt;/th&gt;
&lt;th scope=&quot;col&quot;&gt;ACTION&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody class=&quot;table-group-divider&quot; style=&quot;font-size:0.9rem;&quot;&gt;&#39;;
$sql = &quot;SELECT * FROM d_mom WHERE status=&#39;OPEN&#39; ORDER BY opendate DESC&quot;;
$result = mysqli_query($conn, $sql);
$number = 1;
while ($row = mysqli_fetch_assoc($result)) {
$id = $row[&#39;momid&#39;];
$details = $row[&#39;momdetails&#39;];
$model = $row[&#39;model&#39;];
$pic = $row[&#39;pic&#39;];
$opendate = $row[&#39;opendate&#39;];
$targetdate = $row[&#39;targetdate&#39;];
$remarks = $row[&#39;remarks&#39;];
$updatedate = $row[&#39;updatedate&#39;];
$updateby = $row[&#39;updateby&#39;];
$floor = $row[&#39;floor&#39;];
// $status = $row[&#39;status&#39;];
$table .= &#39;
&lt;tr&gt;
&lt;td scope=&quot;row&quot; class=&quot;text-center&quot;&gt;&#39; . $number . &#39;&lt;/th&gt;
&lt;td&gt;&#39; . $details . &#39;&lt;/td&gt;
&lt;td class=&quot;text-center&quot;&gt;&#39; . $model . &#39;&lt;/td&gt;
&lt;td class=&quot;text-center&quot;&gt;&#39; . $pic . &#39;&lt;/td&gt;
&lt;td class=&quot;text-center&quot;&gt;&#39; . $opendate . &#39;&lt;/td&gt;
&lt;td class=&quot;text-center&quot;&gt;&#39; . $targetdate . &#39;&lt;/td&gt;
&lt;td&gt;&#39; . $remarks . &#39;&lt;/td&gt;
&lt;td class=&quot;text-center&quot;&gt;Updated on: &#39; . $updatedate . &#39;&lt;/td&gt;
&lt;td&gt;By: &#39; . $updateby . &#39;&lt;/td&gt;
&lt;td class=&quot;text-center&quot;&gt;&#39; . $floor . &#39;&lt;/td&gt;
&lt;td class=&quot;text-center&quot;&gt;
&lt;div class=&quot;dropdown&quot;&gt;
&lt;button class=&quot;btn btn-secondary dropdown-toggle&quot; type=&quot;button&quot; data-bs-toggle=&quot;dropdown&quot; aria-expanded=&quot;false&quot;&gt;
Action
&lt;/button&gt;
&lt;ul class=&quot;dropdown-menu&quot;&gt;
&lt;li&gt;&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;Update&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot; onclick=&quot;updateToUM(&#39; . $id . &#39;)&quot;&gt;Under Monitoring&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot; onclick=&quot;updateToClosed(&#39; . $id . &#39;)&quot;&gt;Closed&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot; onclick=&quot;deleteMoM(&#39; . $id . &#39;)&quot;&gt;Delete&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;&#39;;
$number++;
}
$table .= &#39;&lt;/tbody&gt;
&lt;/table&gt;&#39;;
echo $table;
}
?&gt;

With the code above I'm able to display the table as pictured. But when I put open.php inside the tables folder, it will no longer display the table. I tried changing the url to tables/open.php, /tables/open.php, ../tables/open.php, mom_tracking/tables/open.php, but nothing works.

答案1

得分: 1

你是否将所有相关部分移动到新的 tables 文件夹中?例如,在 open.php 中,你有以下代码行:

include 'database/connect.php';

在新位置中,database 文件夹是否与 open.php 同级存在?

然后,在 table.js 中,你需要调整以下代码行:

url: "open.php",

你可以使用完整的 URL 或绝对路径,例如:

url: "/tables/open.php",

或者

url: "https://yoursite.com/wherever/tables/open.php",

如果你在浏览器中打开这个页面,就像你的 JSON(AJAX)请求一样,它是否输出了预期的结果?是否出现任何错误?

英文:

Did you move all relevant parts into the new tables folder? For instance, within open.php you have the line:

include &#39;database/connect.php&#39;;

Does the database directory exist in the new location alongside open.php?

Then where you adjust this line in table.js:

    url: &quot;open.php&quot;,

To use either the full URL, or absolute path, ie.

    url: &quot;/tables/open.php&quot;,

or

    url: &quot;https://yoursite.com/wherever/tables/open.php&quot;,

If you open this page in the browser as your JSON (AJAX) request would, does it output the expected result? There's no error?

huangapple
  • 本文由 发表于 2023年1月9日 11:44:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定