在HTML中显示结果

时间:2018-12-13 16:49:39

标签: 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 name="description" style="float: left; width:470px;">' . $rule->description . '</div>';

   echo '<div name="scores" style="float: right">' . $rule->score . '</div>';
   echo '</div><br><hr>';
}

这是完整的代码:

<?php

if(isset($_POST['btn1']))
{
    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);
    $score = json_decode($response);

    ?>
      <script type="text/javascript">
          $(document).ready(function() {
              $('#myModal').modal('show');
          });
      </script>
      <?php
}   
?>

<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>

<!-- 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">&times;</button>
                <h3 class="modal-title">SpamScore</h3>
            </div>
            <div class="modal-body" style="height: 77%;"> 
              <?php
                    echo '<p><b>SpamScore points: <span id="bbb"></span>' . $score->score . '</b></p><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>';
                    }
            ?>
            </div><br>
            <div class="modal-footer">

            </div>
        </div>
    </div>   
</div>

我已经在显示的PHP中检查了$ score的结果:

stdClass Object ( [success] => 1 [score] => 0.2 [rules] => Array ( [0] => 
stdClass Object ( [score] => 0.1 [description] => Message has a DKIM or DK 
signature, not necessarily valid ) [1] => stdClass Object ( [score] => 0.1 
[description] => DKIM or DK signature exists, but is not valid ) ) [report] 
=> pts rule description ---- ---------------------- ------------------------
-------------------------- 0.1 DKIM_SIGNED Message has a DKIM or DK 
signature, not necessarily valid 0.1 DKIM_INVALID DKIM or DK signature 
exists, but is not valid )

将$ score放入HTML中后,结果将显示为空。

昨天工作正常,但今天在这里不工作。当我能够毫无问题地使用PHP提取数据时,我真的不理解为什么变量$score在HTML中显示为空。我试图在Google上找到答案,但在任何地方都找不到。

您能帮我如何使用变量$score在HTML中显示数据吗?

谢谢。

2 个答案:

答案 0 :(得分:2)

我刚刚注意到这里的东西。我不确定是否可以解决该错误,但这可能是一个起点。循环中的第一条回显行没有尾随分号:

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>';
}

答案 1 :(得分:0)

您的$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">&times;</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>

我无法测试上述代码,但这必须可行。