0

我有一个带有表单的 PHP 文件。该表单具有地区、国家和州下拉菜单。我的 XML 文件包含所有地区、国家和州。我正在尝试使用 XML 信息填写下拉菜单 Region、Country 和 State。这不会显示 XML 文件中的任何内容。我认为我的 PHP 文件没有引用我的 XML 文件,我是 XML 新手。

<?xml version="1.0" encoding="utf-8"?>
<regions>
<region>
    <region-name>North America</region-name>
    <countries>
        <country>
            <na-country-name>Canada</na-country-name>
            <states>
                <state>
                    <ca-state>Alberta</ca-state>
                </state>
                <state>
                    <ca-state>British Columbia</ca-state>
                </state>
                <state>
                    <ca-state>Manitoba</ca-state>
                </state> ...

我的 PHP 文件,我希望 Region、Country 和 State 引用 XML 文件。

    <body>
      <form name="leadform" method=post action="send_data.php">
      <input type="hidden" name="create" value="lead">
      <?php
    if(!isset($_POST['lastname']))
        $_POST['lastname'] = "";
    if(!isset($_POST['firstname']))
        $_POST['firstname'] = "";
    if(!isset($_POST['email']))
        $_POST['email'] = "";
    if(!isset($_POST['phone']))
        $_POST['phone'] = "";
    if(!isset($_POST['company']))
        $_POST['company'] = "";
    if(!isset($_POST['companyurl']))
        $_POST['companyurl'] = "";
    if(!isset($_POST['description']))
        $_POST['description'] = "";

    ?>
    <table cellspacing="0" cellpadding="5" border="0" id="perfect" width="100%" align="center">
        <tr><th>Last Name:<font color="#CC0000"><sup>*</sup></font> </th>
              <td><input type=text name="lastname" size=30 value="<?php echo $_POST['lastname']?>"></td>
        </tr>
        <tr><th>First Name:<font color="#CC0000"><sup>*</sup></font> </th>
              <td><input type=text name="firstname" size=30 value="<?php echo $_POST['firstname']?>"></td>
        </tr>
        <tr><th>Your E-Mail:<font color="#CC0000"><sup>*</sup></font></th>
              <td><input type=text name="email" size=30 value="<?php echo $_POST['email']?>"></td>
        </tr>
        <tr><th>Phone:<font color="#CC0000"><sup>*</sup></font></th>
              <td><input type=text name="phone" size=30 value="<?php echo $_POST['phone']?>"></td>
        </tr>
        <tr><th>Company:<font color="#CC0000"><sup>*</sup></font></th>
              <td><input type=text name="company" size=30 value="<?php echo $_POST['company']?>"></td>
        </tr>
        <tr><th>Company URL:</th>
              <td><input type=text name="companyurl" size=30 value="<?php echo $_POST['companyurl']?>"></td>
        </tr>
        <tr><th>Region:<font color="#CC0000"><sup>*</sup></font></th>
              <td>
                    <select name="region" id="region" style="font-family:Arial, Helvetica, sans-serif; font-size:11px; color:#000000;" >
                        <option value="">None</option>
                       </select> 
              </td>
        </tr>
        <tr><th>Country:<font color="#CC0000"><sup>*</sup></font></th>
              <td>
              <select name="country" id="country" style="font-family:Arial, Helvetica, sans-serif; font-size:11px; color:#000000;" >
              </SELECT>

              </td>
        </tr>
        <tr><th>State/Province:<font color"#CC0000"><sup>*</sup></font></th>
            <td>
                    <select name="state" id="state" size="1" style="font-family:Arial, Helvetica, sans-serif; font-size:11px; color:#000000;">

                    </select>
            </td>
        </tr>
        <tr><th>Product Category:<font color="#CC0000"><sup>*</sup></font></th>
            <td>
                    <select name="productcategory" style="font-family:Arial, Helvetica, sans-serif; font-size:11px; color:#000000;">
                        <option value="blank">blank</option>
                    </select>
             </td>
        </tr>
        <tr><th>Subject:<font color="#CC0000"><sup>*</sup></font></th>
              <td><input type=text name=subject size=38 value="<?php echo $_POST['companyurl']?>"></td>
        </tr>
        <tr><th>Message:</th>
            <td><textarea name=description rows=2 cols=29><?php echo $_POST['description']?></textarea></td>
        </tr>

   <tr><td colspan="2" align="center"> 
      <input type="button" name="Submit" value="Submit" class=click onClick="ValidateForm();"></td></tr>
   </table>
</form>
</body>
</html>

我正在使用的脚本:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">

    $(document).ready(function() {
$.ajax({
    type: "GET",
    url: "/registration.xml",
    dataType: "xml",
    success: function(xml){

        $("#region").html("<option>Please Select a Region</option>");
        $("#country").html("<option>Please Select Country</option>");
        $("#state").html("<option value='Please Select State'>Please Select State/Province</option>");

        $(xml).find('region').each(function(){

            $("#region").append("<option>"+$(this).text()+"</option>");

        });
        $("#region").change(function(){
            $("#country").html("<option>Please Select Country</option>");
            $("#state").html("<option value='Please Select State'>Please Select State/Province</option>");

            if ($("#region").val() == "North America")
            {
                $(xml).find('na-country-name').each(function(){
                    $("#country").append("<option>"+$(this).text()+"</option>");
                });
            }
            else if ($("#region").val() == "Asia Pacific")
            {   
                $(xml).find('apac-country-name').each(function(){
                    $("#country").append($('<option></option').attr("value", this.id).text(this.name));
                });
            }
            if ($("#region").val() == "Latin America")
            {   
                $(xml).find('latm-country-name').each(function(){
                    $("#country").append($('<option></option').attr("value", this.id).text(this.name));
                });
            }
            if ($("#region").val() == "EMEA")
            {   
                $(xml).find('emea-country-name').each(function(){
                    $("#country").append($('<option></option').attr("value", this.id).text(this.name));
                });
            }
        });

        $("#country").change(function() {
            $("#state").html("<option value='Please Select State'>Please Select State/Province</option>");
            //if ($('#reseller_company_region').val() != "Asia Pacific" || $('#reseller_company_region').val() != "Latin America" )
            //  $('#reseller_area_served').html("<option value='Please Select Area'>Please Select Area(s)</option>");


            if ($("#country").val() == "United States")
            {
                $(xml).find('usa-state').each(function(){
                    $("#state").append("<option>"+$(this).text()+"</option>");
                });
            }
            else if ($("#country").val() == "Canada")
            {   
                $(xml).find('ca-state').each(function(){
                    $("#state").append($('<option></option').attr("value", this.id).text(this.name));
                });
            }
            else
            {
                $("#state").html("<option>Not Applicable</option>");
            }

        });
    }
});

});

加载页面时我没有看到任何 XML 选项?我是引用 xml 的新手。任何输入将不胜感激。谢谢。

4

0 回答 0