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

时间:2015-01-30 05:47:41

标签: javascript php jquery ajax

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

我有一点工作,虽然我不确定如何准备PHP中的返回信息和成功,因此$ title出现在输入字段中,$ description出现在单独的输入字段中。

目前它只返回一个回声块。

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 (request-information.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;


}

2 个答案:

答案 0 :(得分:1)

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

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 [&#39; dataqueryurl&#39;];

$ html = file_get_contents_curl($ dataqueryurl);

//解析从这里开始:

$ doc = new DOMDocument();

@ $ doc-&GT; loadHTML($ HTML);

$ nodes = $ doc-&gt; getElementsByTagName(&#39; title&#39;);

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

$ title = $ nodes-&gt; item(0) - &gt; nodeValue;

$ metas = $ doc-&gt; getElementsByTagName(&#39; meta&#39;);

for($ i = 0; $ i&lt; $ metas-&gt; length; $ i ++)

{

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

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

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

}

}

$ result = array(&#39; title&#39; =&gt; $ title,&#39; desc&#39; =&gt; $ description);

echo json_encode($ result);

}

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

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){  
    dataresult = $.parseJSON(dataresult);
    $("#title").val(dataresult.title);
    $("#description").val(dataresult.desc);
 } 

}); 

}

答案 1 :(得分:1)

  

在php方面

echo json_encode(array("title"=>"My title","description"=>"Test description"));
  

在ajax成功

success: function(dataresult){  

       $("#title").val(dataresult.title);
       $("#description").val(dataresult.description);

 } 

<强> FYI

json_encode