javascript - 返回信息结果到多个输入字段

标签 javascript php jquery ajax

我正在尝试创建一个简单的 Web 元素,根据请求,使用 php/ajax/jquery 检索网页的页面标题/元描述。

我已经在某种程度上工作了,尽管我不确定如何在 PHP 中准备返回的信息并成功,以便 $title 出现在输入字段中,$description 出现在单独的输入字段中。

目前它只是返回到一个回显 block 。

HTML

<a href="javascript:retrievepageinformation()" >Action request</a>

<div>The URL</div>
<input name="theaddress" type="text" id="theaddress"  value="http://">

<div>The result</div>
<input name="title" id="title"  value="" />
<input name="description" id="description"  value="" />

Javascript

function retrievepageinformation () {

$("#title").val('Retrieving..');
$("#description").val('Retrieving..');

var dataqueryurl = $("#theaddress").val();

$.ajax({  
type: "POST",  
url: "request-information.php",  
data: "dataqueryurl="+ dataqueryurl,  

success: function(dataresult){  
    $("#title").ajaxComplete(function(){ 
    $(this).val(dataresult);
    });
 } 

}); 

} 

PHP(请求信息.php)

if(isSet($_POST['dataqueryurl'])) {


function file_get_contents_curl($url)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

$dataqueryurl = $_POST['dataqueryurl'];
$html = file_get_contents_curl($dataqueryurl);

//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');

// Get and display what you need:
$title = $nodes->item(0)->nodeValue;

$metas = $doc->getElementsByTagName('meta');

for ($i = 0; $i < $metas->length; $i++)
{
    $meta = $metas->item($i);
    if($meta->getAttribute('name') == 'description')
        $description = $meta->getAttribute('content');
}


// Data to pass back to input field 'Title'
echo $title;
// Data to pass back to input field 'Description'
echo  $description;


}

最佳答案

要从 request-information.php 传回信息,您可以使用数组json_encode的概念。

函数 file_get_contents_curl($url) { $ch=curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$data = curl_exec($ch);
curl_close($ch);

return $data;

}

$dataqueryurl = $_POST['dataqueryurl'];

$html = file_get_contents_curl($dataqueryurl);

//解析从这里开始:

$doc = new DOMDocument();

@$doc->loadHTML($html);

$nodes = $doc->getElementsByTagName('title');

//获取并显示您需要的内容:

$title = $nodes->item(0)->nodeValue;

$metas = $doc->getElementsByTagName('meta');

for ($i = 0; $i < $metas->length; $i++)

{

$meta = $metas->item($i);

if($meta->getAttribute('name') == 'description'){

    $description = $meta->getAttribute('content');

}

}

$result = array('title'=>$title, 'desc'=>$description);

回显 json_encode($结果);

}

要在 JavaScript 中获取此信息,您可以在 ajax 的成功回调中使用 $.parseJSON()

函数检索页面信息(){

$("#title").val('Retrieving..');
$("#description").val('Retrieving..');

var dataqueryurl = $("#theaddress").val();

$.ajax({  
type: "POST",  
url: "request-information.php",  
data: "dataqueryurl="+ dataqueryurl,  

success: function(dataresult){  
    dataresult = $.parseJSON(dataresult);
    $("#title").val(dataresult.title);
    $("#description").val(dataresult.desc);
 } 

}); 

}

关于javascript - 返回信息结果到多个输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28229946/

相关文章:

javascript - 将 id 值传递给 ajax 页面

php websocket问题

javascript - 用字符串调用 JavaScript 函数?

javascript 查找匿名引用

php - Laravel - 注册不在数据库中保存表单响应

php - 全局覆盖 var_dump?

javascript - 在现有的ajax代码中添加图像上传功能

ajax - 我的 javascript/jquery 代码有什么问题吗?

php - 打印没有附加内容(URL、日期等)的 HTML 页面

php - FB如何创建FBML?