将表单数据发布到PHP脚本,然后再次发回结果

时间:2012-04-25 10:07:49

标签: php html

以下脚本在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吗?所以它不刷新

2 个答案:

答案 0 :(得分:1)

您必须:

1 - 提交到frontend页面,包括该页面上的此PHP代码。

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

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

答案 1 :(得分:1)

我更喜欢ajax方法,因为它更清洁..

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

文档

json_encode() - http://php.net/manual/en/function.json-encode.php

的php手册

$.ajax(); - http://api.jquery.com/jQuery.ajax/

的jquery手册

回复代码列表 - 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

AJAX

这要求你使用jquery库..如果在additon中使用另一个名为data tables的库,你可以简化<tr>附加的jquery

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