2012-06-11 56 views
0

我創建了一個交易agregator,但首先我需要將xpath放入數據庫,但在此之前,我需要在我的字段中輸入xpath和網站以查看我將得到的結果...因此某種類型的XPath驗證工具的:Xpath,php和jquery驗證腳本

這裏是我的類型的XPath代碼將不會忽略文本字段:

xpathadd.html

<!DOCTYPE HTML> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Untitled Document</title> 
<script src="http://code.jquery.com/jquery-latest.min.js" 
     type="text/javascript"></script> 

</head> 

<body> 
<script> 
addToDatabase = function() { 
    alert("addToDatabase called"); 
    $.post("generatingxpath.php", { 
     websource: $('#websource').val(), 
     links: $('#links').val(), 
     title: $('#title').val(), 
     image: $('#image').val(), 
     discount: $('#discount').val(), 
     price: $('#price').val(), 
     description: $('#description').val(), 
     coordinates: $('#coordinates').val() 
    }, function(data) { 
     $("#result").html(data); 
    }); 
};​ 
</script> 
<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="discount"></label> 
    Disscount: 
    <input name="discount" type="text" id="discount" 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="description" 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"> 
<a href="javascript:addToDatabase();">Add to database</a> 
    </p> 
    <p>&nbsp;</p> 
</form> 
    <div id="result"></div> 
</body> 
</html>​ 

和generatingxpath.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> 

但我只得到警告:無效的參數爲foreach()提供的/home/pluspon/public_html/test/generatingxpath.php在線67 ???爲什麼?問題在哪裏?

+0

http://test.pluspon.com/xpathadd.html –

回答

0

$_POST["links"]的值在DOM/XML中不匹配!

+0

$ _POST [「links」]存在厄運 –

+0

請在ideone.com上顯示我不明白你說什麼。 –