2012-06-09 78 views
0

我想爲我創建一些幫助工具。因爲我創建了一個交易agregator,我需要將xpath放入我的數據庫,但是需要創建一些工具來幫助我進行驗證。Xpath和HTML表單動作發佈php

我有一個HTML文件,我需要投入的XPath,然後用我的行動去generatingxpath.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網頁... AJAX ???或者其他的東西?

什麼是我的問題的最佳解決方案? 如何解決這個問題?

+0

這個東西會很有趣,並會幫助很多人 –

+0

所以當我點擊預覽時我需要從一些URL中提取文本,我輸入到基於xpath的文本文件中 –

+0

非常對不起,我的英語,是非常糟糕的,但我努力改變,很快 –

回答

0

在HTML和PHP desription /說明不匹配。嘗試改變

<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 $就加載從generatingxpath.php文本無需重新加載頁面。 包括jQuery的,你可以得到它here和使用<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/

+0

但如何做到這一點。請在一些在線代碼編輯器中給我鞋子! –

+0

我更新了我的答案,希望您能使用它。 – Sebr

+0

確定.............我預訂購儘量 –