$ .post之前的php加载 [英] php loading before $.post
问题描述
我有一个主页,我们将其称为第A页.我在第A页上有一个导航菜单,当单击某个项目时,该菜单从jQuery
执行$ .post.然后,当它完成时,将隐藏几个div并显示一个.
I have a main page we'll call it page A. I have a navigation menu on page A that executes $.post from jQuery
when an item is clicked. Then when it finishes it hides a few div's and shows one.
它显示的div有一个PHP
文件来扩大内容.这是将$ .post发送到的PHP
文件,该文件包含在A页上.
Well the div it shows has a PHP
file inflating the content. This is the same PHP
file it sends the $.post to and this file is included on page A.
因此,当页面加载导航和隐藏的div时,它还会加载PHP
文件而没有 $ .post数据.
So when the page loads the nav and hidden divs it also loads the PHP
file without the $.post data.
这是一些代码,我会进一步解释
Here is some code and I'll explain further
jQuery
$('#projects').click(function (e) {
$.trim(account_id);
$.post('core/functions/projects.php', {
account_id: account_id
})
.done(function(data) {
$('#scd').hide();
$('#icd').hide();
$('#pcd').fadeIn(1000);
})
.fail(function(jqXHR, status, error) {
alert(error);
});
});
PHP
<?php
if(empty($_POST) === false) {
$account_id = $_POST['account_id'];
try {
$query_projectInfo = $db->prepare("
SELECT projects.account_id,
projects.project_name,
.... //more code, not relevant
FROM projects
WHERE account_id = ?
");
$query_projectInfo->bindValue(1, $account_id, PDO::PARAM_STR);
$query_projectInfo->execute();
$count = $query_projectInfo->rowCount();
if ($count > 0) {
echo "<table class='contentTable'>";
echo "<th class='content_th'>" . "Job #" . "</th>";
echo "<th class='content_th'>" . "Project Name" . "</th>";
... //more code, not relevant
while ($row = $query_projectInfo->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td class='content_td'>" . "<a href='#'>" . $row['account_id'] . "</a>" . "</td>";
echo "<td class='content_td'>" . $row['project_name'] . "</td>";
.... //more code, not relevant
}
echo "</table>";
}
} catch(PDOException $e) {
die($e->getMessage());
}
} else {
echo 'could not load projects table';
}
?>
现在,当我在导航菜单上单击#project时,仅能看到else
语句无法加载项目表".我相信这是因为PHP
文件是在单击导航项之前加载的.看来,当我单击该项目并发送帖子时,就像PHP
页上所说:我已经加载了好友,我不会再这样做了".
Now all I ever see when I click #project on the nav menu is the else
statement of 'could not load projects table'. I believe this is because the PHP
file is loaded before the nav item is ever clicked. It seems like when I click the item and send the post it's like the PHP
page says "i already loaded buddy, I'm not doing that again".
所以我的问题是,如何使PHP
文件等待加载?我以为if else
条件会做到这一点,显然我是错的.
So my question is, how would I make the PHP
file wait to load? I thought that if else
condition would do it, obviously I was wrong.
P.S.如果我给$ account_id变量一个值,而不是等待发布,则div会随表格和所有信息一起膨胀.因此PHP
文件是可操作的.它只是不等待$ .post
P.S. If I give the $account_id variable a value rather than wait for post the div inflates with the table and all the info. So the PHP
file is operational. It just doesn't wait for that $.post
推荐答案
此检查将永远不会触发. empty($_POST) === false
This check will never fire. empty($_POST) === false
如果您只是想检查$_POST
是否有数据,只需使用isset($_POST)
If you're simply wanting to check if $_POST
has data, just use isset($_POST)
这篇关于$ .post之前的php加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!