如何在php中传递Xpath路径表达式中的值

时间:2013-12-12 11:21:31

标签: php xpath

我在xml中有如下

<delivery_codes>
        <postal_code>
            <pin>400103</pin>
            <pin>400104</pin>
            <pin>400105</pin>
        </postal_code>
 </delivery_codes>

我希望得到输入值作为密码并检查密码是否存在于xml中。这里是PHP代码。

<form name="form1" action="default.php">
<table class="tableborder" align=center bgcolor="#f0f0f2">
    <tr>
  <td style="padding-bottom: 10px;" colspan="2" class="heading1"><b>Check Cash On Delivery Service Availability</b></td>
</tr>
    <tr>
      <td height="30">Enter Your Postal Pincode:</td>
      <td ><input type="text" name="pincode" style="width:15em;">
      </td>
    </tr>

    <tr align=center>

    <td><input name="submit" type="submit" value="Check Availablity" class="submit"></td>

    </tr>
  </table>
</form>

和default.php如下

 <?php 

$Pincode =  $_GET['pincode'];
$doc = new DOMDocument;
$doc->load( 'pincodes.xml');
$xpath = new DOMXPath($doc);



$query = '//postal_code/pin[.="$Pincode"]';

$entries = $xpath->query($query);


if ( $entries->length )
{


  echo 'Cash on Delivery Available!!! Place your order now!!';

}
else
{

echo 'Sorry!!! Cash on Delivery Service unavailable to your area. But we can serve you when you make advance payment';
}

?>

每次都将其他部分作为输出,并帮助我如何在下面的路径表达式中传递值。期待着实现我的任务的方法。 。

$query = '//postal_code/pin[.="$Pincode"]';

1 个答案:

答案 0 :(得分:1)

更改您的

$query = '//postal_code/pin[.="$Pincode"]';

致:

$query = '//postal_code/pin[.="'.$Pincode.'"]';

$query = "//postal_code/pin[.='$Pincode']";