Ajax Magento在退出时填充编辑框
这应该是一个非常简单的问题,但找不到如何做到这一点。在前端,我有一个带有一些编辑框或Ajax Magento在退出时填充编辑框,ajax,magento,jquery,magento-1.7,Ajax,Magento,Jquery,Magento 1.7,这应该是一个非常简单的问题,但找不到如何做到这一点。在前端,我有一个带有一些编辑框或输入文本字段的表单。在其中一个页面中,用户将写入/粘贴博客url。当用户退出框时,我想: 抓取这个url并解析/搜索/从该博客中获取与该url相关的标记(如果存在)。如果没有此类标签,则将使用通常的标题和相关标签 然后使用标记的值填充当前表单、标题、注释、说明、图像等的其他编辑字段 如何才能完成这些操作,AJAX也许?与jQuery一起使用?还是其他解决办法? 现在我已经搜索过了,下面是一些我想可能有用的链接:
输入文本
字段的表单。在其中一个页面中,用户将写入/粘贴博客url。当用户退出框时,我想:
标题
和相关标签XMLHttpRequest
从Ajax请求中得到的答案填充其他输入文件。以下是不同的文件,以防有人有不同的解决方案或建议。不显示所有代码,因为它太长
phtml文件:
<div id="blog_link_block">
<input class="input-text required-entry" onchange="showHint(this.value)"
name="blog_link" id="blog_link_field" type="text" style="width: 210px;"
value="" />
</div>
<div>
<label for="title_field"><?php echo $this->__('Title'); ?>
<span class="required">*</span>
</label><br />
<input class="input-text required-entry" name="title" id="title_field" type="text"
style="width: 450px;" value="" />
</div>
<script type="text/javascript">
function showHint(str)
{
<?php $block = Mage::getBlockSingleton('blogtest/product_view');
$temp = $block->getUrl('blogtest/blogtagsajax/index');?>
if (str.length==0)
{
document.getElementById("title_field").innerHTML = "";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("title_field").value = xmlhttp.responseText;
}
}
xmlhttp.open("GET","<?php echo $temp ?>?q="+str,true);
xmlhttp.send();
}
</script>
*
函数showHint(str)
{
如果(str.length==0)
{
document.getElementById(“title_字段”).innerHTML=“”;
返回;
}
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“title_字段”).value=xmlhttp.responseText;
}
}
open(“GET”、“?q=“+str,true”);
xmlhttp.send();
}
布局文件:
<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
<blogtest_blogtagsajax_index>
<reference name="root">
<remove name="root"/>
</reference>
<block type="blogtest/product_ajax" name="product.ajax" output="toHtml" />
</blogtest_blogtagsajax_index>
</layout>
控制器:
<?php
class Dts_Blogtest_BlogtagsajaxController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
$this->loadLayout();
$this->renderLayout();
}
}
要回答第一点,尝试使用库,但被卡住了
然后我发现这个和所有的问题都消失了。接受答案上的建议函数与og:
标记完美配合。这是我的修改版本:
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;
}
public function myFunc() {
// get the q parameter from URL
$q = $_GET["q"];
$html = self::file_get_contents_curl($q);
//parsing begins here:
$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') == 'og:title')
$title = $meta->getAttribute('content');
if($meta->getAttribute('name') == 'og:description')
$description = $meta->getAttribute('content');
if($meta->getAttribute('name') == 'keywords')
$keywords = $meta->getAttribute('content');
}
$title = trim($title);
if ($title == ""){
$response = "no title";
}
else{
$response = $title;
}
//output the response
return $response;
}
函数文件\u get\u contents\u curl($url)
{
$ch=curl_init();
curl_setopt($ch,CURLOPT_头,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
$data=curl\u exec($ch);
卷曲关闭($ch);
返回$data;
}
公共函数myFunc(){
//从URL获取q参数
$q=$_GET[“q”];
$html=self::file\u get\u contents\u curl($q);
//解析从这里开始:
$doc=新的DOMDocument();
@$doc->loadHTML($html);
$nodes=$doc->getElementsByTagName('title');
$title=$nodes->item(0)->nodeValue;
$metas=$doc->getElementsByTagName('meta');
对于($i=0;$i<$metas->length;$i++){
$meta=$metas->item($i);
如果($meta->getAttribute('name')=='og:title')
$title=$meta->getAttribute('content');
如果($meta->getAttribute('name')=='og:description')
$description=$meta->getAttribute('content');
如果($meta->getAttribute('name')=='keywords')
$keywords=$meta->getAttribute('content');
}
$title=修剪($title);
如果($title==“”){
$response=“无标题”;
}
否则{
$response=$title;
}
//输出响应
返回$response;
}
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;
}
public function myFunc() {
// get the q parameter from URL
$q = $_GET["q"];
$html = self::file_get_contents_curl($q);
//parsing begins here:
$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') == 'og:title')
$title = $meta->getAttribute('content');
if($meta->getAttribute('name') == 'og:description')
$description = $meta->getAttribute('content');
if($meta->getAttribute('name') == 'keywords')
$keywords = $meta->getAttribute('content');
}
$title = trim($title);
if ($title == ""){
$response = "no title";
}
else{
$response = $title;
}
//output the response
return $response;
}