我有一个php文件,从中点击按钮时save_match()
运行:
function save_match() {
...
$.post('url2', {elements: elements});
...
}
并运行另一个php文件,其中包含以下内容:
<?php
$tok = 1024;
if($_POST['elements'])
{
echo '<script>foo("'.$tok.'") </script>';
...
?>
<html>
<body>
<script type="text/javascript">
function foo(p1) {
//alert(p1);
document.write("|"+p1+"|");
}
</script>
</body>
</html>
我这样做的原因是因为echo
我什么也看不见。那不是现在我看到了更多。
我在新标签页中也打开了url2
。但是,在我按下url1中的按钮并转到第二个url的选项卡后,没有任何改变。我刷新它,但仍然没有改变!
我该怎么办?我需要这个来进行调试。
答案 0 :(得分:1)
您可以通过以下方式查看令牌的价值。
将您的代码更改为以下内容。
<?php
if (isset($_POST['elements'] && $_POST['elements']))
echo $tok;
.....
将您的javascript更新为:
function save_match() {
...
$.post('url2', {elements: elements}, function($token) {
console.log($token); // will print your token in javascript console
});
...
}
答案 1 :(得分:0)
<html>
<body>
<?php
$tok = 1024;
if(isset($_POST['elements']))
{
echo $tok;
}
?>
</body>
</html>
这应该有效。确保设置了doctype和所有其余部分,并且还设置了POST中的变量元素。也许if函数总是失败,因为没有元素设置。
对于你想要做的事情,你真的不需要javascript。
答案 2 :(得分:0)
等待异步操作的函数示例:
function save_match(){
var somethingToReturn, pushTo = [];
$.post('url2.php', {saveMatch:1, simple:'column_value_here', anotherProp:'Example'}, function(phpComesBackHere){
$.each(phpComesBackHere, function(i, o){
pushTo.push(o.object_property);
});
somethingToReturn = pushTo;
});
while(1){
if(somethingToReturn){
return somethingToReturn;
}
}
}
在url2.php上就像:
<?php
include 'secure_database_connection.php'; $db = db(); // function db is returning `new mysqli('host', 'username', 'password', 'database');`
if(isset($_POST['saveMatch'])){
if(+$_POST['saveMatch'] === 1){
if(isset($_POST['simple'], $_POST['anotherProp'])){
if($qry = $db->query("SELECT * FROM table_name WHERE column={$_POST['simple']}")){
if($qry->num_rows > 0){
while($r = $qry->fetch_object()){
$returnArray[] = array('object_property' => $r->your_property_here, 'anotherProp' => $r->and_another_property);
}
echo json_encode($returnArray);
}
$qry->free();
}
}
else{
// simple and/or anotherProp hack
}
}
else{
// saveMatch hack
}
}
elseif(isset($_POST['somethingElse'])){
// this could be separate $.post on same page
}
$db->close();
?>