Php 在HTML中显示结果
我正在编写代码,以获取数据并将其输出到HTML中。我在PHP之外的html源代码中显示数据时遇到问题,因为我收到一个错误: 警告:第150行/home/username/public_html/check_score.php中为foreach()提供的参数无效 这是第150行:Php 在HTML中显示结果,php,html,Php,Html,我正在编写代码,以获取数据并将其输出到HTML中。我在PHP之外的html源代码中显示数据时遇到问题,因为我收到一个错误: 警告:第150行/home/username/public_html/check_score.php中为foreach()提供的参数无效 这是第150行: foreach($score->rules AS $rule) { echo '<div name="test-row" style="margin-top:-1px"> <div n
foreach($score->rules AS $rule) {
echo '<div name="test-row" style="margin-top:-1px">
<div name="description" style="float: left; width:470px;">' . $rule->description . '</div>';
echo '<div name="scores" style="float: right">' . $rule->score . '</div>';
echo '</div><br><hr>';
}
当我在HTML中输入$score后,结果将显示为空
昨天工作很好,但今天不工作了。我真的不明白为什么变量$score
在HTML中显示为空,而我可以毫无问题地用PHP获取数据。我试图在谷歌上找到答案,但在任何地方都找不到
您能帮助我如何使用变量$score
在HTML中显示数据吗
谢谢。您的
$score
变量在表单提交时填写,因此您不能允许在模式视图中表单发布后填写$score
变量
你能试试下面的代码而不是你的代码吗
已更新
我的答案被更改为正确答案
curl.php
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['submit'])
{
sleep(1);
$ch = curl_init('http://example.com/check_score');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$message = "hey chris";
$header = "Delivered-To: example@gmail.com
Received: by 2002:a0c:938a:0:0:0:0:0 with SMTP id f10csp5715121qvf;
Sun, 2 Dec 2018 06:07:45 -0800 (PST)
the long list of the header goes here...
Hey rob,
How you doing?";
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('email' => $header, 'options'=>'long')));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$response = curl_exec($ch);
} else {
$response = json_encode(array('success' => false));
}
echo $response;
exit;
?>
form.html
<form action="" method="post">
<div class="container">
<div class="form-group">
<button name="btn1" id="btn1" type="submit" class="btn btn-primary btn-block button-size">Check for score
</button>
</div>
</div>
</form>
<script type="text/javascript">
$('form').on('submit', function(e){
e.preventDefault(0);
$.ajax({
url: 'curl.php',
data: {
submit: true
},
success: function(data) {
$('.modal-body').html('');
var data = JSON.parse(data);
console.log(data);
if(data.success){
$('#myModal').modal('show');
$('.modal-body').append('<p><b>SpamScore points: <span id="bbb"></span>' + data.score + '</b></p><hr>');
$.each(data.rules, function(key, value){
$('.modal-body').append('<div name="test-row" style="margin-top:-1px"><div name="description" style="float: left; width:470px;">' + value.description + '</div><div name="scores" style="float: right">' + value.score + '</div></div><br><hr>');
});
}
},
type: 'POST'
});
});
</script>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content" style="height:600px;">
<div class="modal-header" style="margin-bottom: 10px;">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">SpamScore</h3>
</div>
<div class="modal-body" style="height: 77%;"></div><br>
<div class="modal-footer"></div>
</div>
</div>
</div>
检查分数
$('form')。关于('submit',函数(e){
e、 预防性违约(0);
$.ajax({
url:'curl.php',
数据:{
提交:正确
},
成功:功能(数据){
$('.modal body').html('');
var data=JSON.parse(数据);
控制台日志(数据);
if(data.success){
$('myModal').modal('show');
$('.modal body').append('spamcore points:'+data.score+'
');
$.each(数据、规则、函数(键、值){
$('.modal body').append(''+value.description+'+value.score+'
');
});
}
},
类型:“POST”
});
});
&时代;
SpamScore
我无法测试上述代码,但这必须是工作。我刚刚注意到了一些问题。我不确定它是否能修复错误,但这可能是一个起点。循环中的第一个回显行没有尾随分号:
foreach($score->rules AS $rule) {
echo '<div name="test-row" style="margin-top:-1px">
<div name="description" style="float: left; width:470px;">' . $rule->description . '</div>';
echo '<div name="scores" style="float: right">' . $rule->score . '</div>';
echo '</div><br><hr>';
}
foreach($score->rules AS$rule){
回声'
“.$rule->description.”;
回显“.$rule->score.”;
回声“
”;
}
试一试:
foreach($score->rules AS $rule) {
echo '<div name="test-row" style="margin-top:-1px">;
<div name="description" style="float: left; width:470px;">' . $rule->description . '</div>';
echo '<div name="scores" style="float: right">' . $rule->score . '</div>';
echo '</div><br><hr>';
}
foreach($score->rules AS$rule){
回声';
“.$rule->description.”;
回显“.$rule->score.”;
回声“
”;
}
只是澄清一下,现在您确实成功地从您的cURL请求中检索到了解码的JSON?@Ryan是的,我这样做了。非常感谢您,但是当我单击submit按钮时,模式没有显示,因此它只能在PHP之外的
之前工作。有什么想法吗?我想你只是复制,通过并运行了它。你认为修好了吗?是的,它没有工作,因为它有一些打字错误。为什么我不认为我能修复它们。我修复了html表单,它可以正常工作。非常感谢,但这与缺少分号无关,因为我使用的是与同一行的echo'
$规则->说明'代码>如您所见。我尝试使用echo$rule->description代码>和我仍然得到相同的错误。知道吗?啊,误读了那句话。很抱歉。当您在循环中回显此内容时会得到什么:回显“”;var_dump($规则);回音“”;?当我尝试echo';var_dump($规则);回声'代码>在循环之前显示为NULL。当我尝试循环时,它显示为空。你知道吗?如果你试图在循环外访问$rule,它总是空的,因为它在循环外是不可访问的。在循环内的var_dump声明中,尝试将$rule更改为$scope->rules,并报告得到的结果。
foreach($score->rules AS $rule) {
echo '<div name="test-row" style="margin-top:-1px">
<div name="description" style="float: left; width:470px;">' . $rule->description . '</div>';
echo '<div name="scores" style="float: right">' . $rule->score . '</div>';
echo '</div><br><hr>';
}
foreach($score->rules AS $rule) {
echo '<div name="test-row" style="margin-top:-1px">;
<div name="description" style="float: left; width:470px;">' . $rule->description . '</div>';
echo '<div name="scores" style="float: right">' . $rule->score . '</div>';
echo '</div><br><hr>';
}