0

下面的脚本获取 URL 列表中的元数据。

URL是在我的前端输入的,我设法将数据获取到另一个页面(此脚本),但现在不是将表格回显到脚本所在的同一页面上,而是我想将该数据反馈给我的前端并将其放在一个漂亮的桌子上供用户查看。

如何让 php 脚本在另一个页面上回显数据?

谢谢

瑞奇

<?php
ini_set('display_errors', 0);
ini_set( 'default_charset', 'UTF-8' );
error_reporting(E_ALL);
//ini_set( "display_errors", 0);
function parseUrl($url){
    //Trim whitespace of the url to ensure proper checking.
    $url = trim($url);
    //Check if a protocol is specified at the beginning of the url. If it's not,     prepend 'http://'.
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
            $url = "http://" . $url;
    }
    //Check if '/' is present at the end of the url. If not, append '/'.
    if (substr($url, -1)!=="/"){
            $url .= "/";
    }
    //Return the processed url.
    return $url;
}
//If the form was submitted
if(isset($_POST['siteurl'])){
    //Put every new line as a new entry in the array
    $urls = explode("\n",trim($_POST["siteurl"]));
    //Iterate through urls
    foreach ($urls as $url) {
            //Parse the url to add 'http://' at the beginning or '/' at the end if not     already there, to avoid errors with the get_meta_tags function
            $url = parseUrl($url);
            //Get the meta data for each url
            $tags = get_meta_tags($url);
            //Check to see if the description tag was present and adjust output     accordingly
            $tags = NULL;
$tags = get_meta_tags($url);
if($tags)
echo "<tr><td>$url</td><td>" .$tags['description']. "</td></tr>";
else 
echo "<tr><td>$url</td><td>No Meta Description</td></tr>";
    }
}

?>

我认为最好为此使用 Ajax 吗?所以不会刷新

4

2 回答 2

1

您将不得不:

1 - 提交到frontend页面,而不是在该页面上包含此 PHP 代码。

2 - 使用 AJAX 发布表单,获取输出并将其放在frontend页面上的某个位置。

就个人而言,我会使用第一种方法。它更容易实施。

于 2012-04-25T10:55:40.997 回答
1

我更喜欢 ajax 方法,因为它更干净。

重要的是$.ajax();echo json_encode()

文档

php 手册json_encode()- h​​ttp: //php.net/manual/en/function.json-encode.php

jquery 手册$.ajax();- h​​ttp: //api.jquery.com/jQuery.ajax/

响应代码列表 - http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

示例代码

没有看到你的 HTML 我在这里猜.. 但这应该让你开始使用 ajax 的正确路径。

表单html

<form action="<?= $_SERVER['PHP_SELF']; ?>" method="POST">
    <input type="text" name="siteUrl" id="siteUrl">
    <input type="submit" name="submit" value="submit" class="form-submit">
</form>

示例容器

在您的情况下,这是一个表,只需将表 ID 设置为 example-container

阿贾克斯

这需要您使用jquery库。如果您在additon中使用另一个称为数据表的库,您可以简化很多jquery附加<tr>

// On the click of the form-submit button.
$('.form-submit').click(function(){
  $.ajax({
    // What data type do we expect back?
    dataType: "json",
    // What do we do when we get data back
    success: function(d){
      alert(d);
      // inject it back into a table called example-container
      // go through all of the items in d and append 
      // them to the table.
      for (var i = d.length - 1; i >= 0; i--) {
        $('#example-container').append("<tr><td>"+d[i].url+"</td><td>"+d[i].description+"</td></tr>");
      };

    },
    // What do we do when we get an error back
    error: function(d){
      // This will show an alert for each error message that exist
      // in the $message array further down.
      for (var i = d.length - 1; i >= 0; i--) {
        alert(d[i].url+": "+d[i].message);
      };
    }
  });
 // make sure to have this, otherwise you'll refresh the page.
 return false;
});

修改php函数

<?php
//If the form was submitted
if(isset($_POST['siteurl'])){
    //Put every new line as a new entry in the array
    $urls = explode("\n",trim($_POST["siteurl"]));
    //Iterate through urls
    foreach ($urls as $url) {
        //Parse the url to add 'http://' at the beginning or '/' at the end if not     already there, to avoid errors with the get_meta_tags function
        $url = parseUrl($url);
        //Get the meta data for each url
        $tags[] = get_meta_tags($url);
     }
    if($tags):
        echo json_encode($tags);
    else:
        $message[] = array(
            'url' => $url,
            'message' => 'No Meta Description'
        );
                    // This sets the header code to 400
                    // This is what tells ajax that there was an error
                    // See my link for a full ref of the codes avail
                    http_response_code(400);
        echo json_encode($message);
    endif;
}
于 2012-04-25T13:27:33.720 回答