Xpath and html form action post php


Xpath and html form action post php

我正在尝试为我创建一些辅助工具。因为我创建了一个交易聚合器,我需要将 xpath 放入我的数据库中,但首先我需要创建一些工具来帮助我进行验证。

有一个 html 文件,我需要在其中放置 xpath,然后通过操作我转到生成 xpath.php 文件,我需要在其中查看我编写的 xpath 得到了什么。所以。。。

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<form name="form1" method="post" action="generatingxpath.php">
  <p>
    <label for="websource"></label>
    Web source:
    <input name="websource" type="text" id="websource" size="70">
  is:</p>
  <p>
    <label for="links"></label>
    Links:  
    <input name="links" type="text" id="links" size="77">
    OK!
  </p>
  <p>
    <label for="title"></label>
    Title:
    <input name="title" type="text" id="title" size="78">
  is:</p>
  <p>
    <label for="image"></label>
    Image:
    <input name="image" type="text" id="image" size="76">
  show:</p>
  <p>
    <label for="disscount"></label>
    Disscount:
    <input name="disscount" type="text" id="disscount" size="72">
  is:</p>
  <p>
    <label for="price"></label>
    Price:
    <input name="price" type="text" id="price" size="77">
  is:</p>
  <p>
    <label for="description"></label>
    Desription:
    <input name="desription" type="text" id="description" size="72">
  is:</p>
  <p>
    <label for="coordinates"></label>
    Coordinates:
    <input name="coordinates" type="text" id="coordinates" size="70">
  lat,lng is:</p>
  <p>
    <input type="submit" name="Submit" id="Submit" value="Preview">
or    
<input type="submit" name="Add to database" id="Add to database" value="Add To Database">
  </p>
  <p>&nbsp;</p>
</form>
</body>
</html>

我的PHP文件是:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>GENERATING XPATH</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>
<?php

function curl($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
}

libxml_use_internal_errors(true);

$websource = $_POST["websource"];
$links = $_POST["links"];
$title = $_POST["title"];
$image = $_POST["image"];
$disscount = $_POST["disscount"];
$price = $_POST["price"];
$description = $_POST["description"];
$coordinates = $_POST["coordinates"];

//start generating xpath script
$dom = new DOMDocument(); 
@$dom->loadHTMLFile($websource); 
$xpath = new DOMXPath($dom); 
$entries = $xpath->query($links);
$output = array(); 
$i = 1;
foreach($entries as $e) {   
  $dom2 = new DOMDocument(); 
  @$dom2->loadHTMLFile($websource . $e->textContent); 
  $xpath2 = new DOMXPath($dom2); 
  $data = array();
  $data['websource']= ($websource . $e->textContent);
  $data['title'] = trim($xpath2->query($title)->item(0)->textContent); 
  $data['description'] = trim($xpath2->query($description)->item(0)->textContent); 
  $data['image'] = trim($xpath2->query($image)->item(0)->textContent); 
  $string = $xpath2->query($coordintes)->item(1)->textContent; 
  preg_match_all('#(([0-9-]+){1,3}.([0-9]+))#is', $string, $matches); 
  $data['lat']=$matches[1];
  $data['lng']=$matches[2];
  $data['disscount'] = trim($xpath2->query($disscount)->item(0)->textContent);
    $data['price'] = trim($xpath2->query($price)->item(0)->textContent);

//print to see that all is fine or need to change some xpath direction
  echo $data['websource'];
  echo $data['links'];
  echo $data['title'];
  echo $data['image'];
  echo $data['disscount'];
  echo $data['price'];
  echo $data['lat'];
  echo $data['lng'];
  echo $data['description'];

    $output[] = $data; 

} 

?>
</body>
</html>

我首先在 67 行中遇到几个错误。 对于每个...等。

有没有一些简单的解决方案可以在同一页面上获得结果,因此使用 xPath 从某个网站获取文本但显示在 HTML 页面上......阿贾克斯???还是什么?

我的问题的最佳解决方案是什么?如何解决这个问题?

在 html 和 php 中描述/描述不匹配。尝试更改

<label for="description"></label>
Desription:
<input name="desription" type="text" id="description" size="72">

进入这个:

<label for="description"></label>
Desription:
<input name="description" type="text" id="description" size="72">

您可以使用jQuery $.ajax从generatingxpath加载文本.php而无需重新加载页面。包括jQuery,你可以在这里得到它并使用<script src="your/folder/jquery.js"></script>并加载文本:

$.post("generatingxpath.php", {var:$('#input').val()}, function(data) {
     $("#result").html(data);
});

如果你想使用jQuery,我建议阅读教程/文档 jQuery.com

像这样的东西 http://jsfiddle.net/VqPHJ/