使用php ajax进行多选表单

时间:2014-06-09 17:24:22

标签: javascript php ajax

嗨,我在php ajax上遇到麻烦,在select字段中显示结果。我想要的是当我在选择字段中选择选项时,它应该加载ajax php并显示第二个选择字段,然后我在第二个选择字段中选择一个选项,然后它还应该加载ajax php并显示第三个选择字段。但是我无法加载第三个选择字段,我的代码中是否有任何错误?对不起,我还是ajax php的新手。

这是我的php代码:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MIHRM Customer Information System</title>
<script>
        function showUser(str) {
          if (str=="") {
            document.getElementById("txtHint").innerHTML="";
            return;
          }
          if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
          } else { // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
          xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
              document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
            }
          }
          xmlhttp.open("GET","getuser1.php?q="+str,true);
          xmlhttp.send();
        }
    </script>
    <script>
        function showUser1(str1) {
          if (str1=="") {
            document.getElementById("txtHint1").innerHTML="";
            return;
          }
          if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
          } else { // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
          xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
              document.getElementById("txtHint1").innerHTML=xmlhttp.responseText;
            }
          }
          xmlhttp.open("GET","getuser1-1.php?z="+str1,true);
          xmlhttp.send();
        }
    </script>

    </head>
<div class="rightpanel">

            <ul class="breadcrumbs">
                <li><a href="dashboard.html"><i class="iconfa-home"></i></a> <span class="separator"></span></li>
                <li><a href="">Generate Excel Report</a> <span class="separator"></span></li>
                <li>List by Position & State</li>
            </ul>

            <div class="pageheader">
                <!--
                <form action="results.html" method="post" class="searchbar">
                    <input type="text" name="keyword" placeholder="To search type and hit enter..." />
                </form>
                -->
                <div class="pageicon"><span class="iconfa-pencil"></span></div>
                <div class="pagetitle">
                    <h5>Generate Excel</h5>
                    <h1>Generate List of Contact by Position & State</h1>
                </div>
            </div><!--pageheader-->

            <div class="maincontent">
                <div class="maincontentinner">


                <div class="widgetbox box-inverse">
                    <h4 class="widgettitle">Contact List by State</h4>
                    <div class="widgetcontent wc1">
                        <form id="form1" class="stdform" method="post" action="$phpself" >
                                <br/>

                                <div class="control-group">
                                        <label class="control-label" for="typeOfUser">Type of Data</label>
                                    <div class="controls"><text style="color:#D84A38;font-size:20px">* </text>
                                    <select name="typeOfUser" id="typeOfUser" class="required" onChange="showUser(this.value)">
                                    <option value="">Choose One</option>
                                    <option value="client">Client</option>
                                    <option value="prospects">Prospects</option>
                                </select>
                                    </div>
                                </div>

                                 <div id = "txtHint"></div>

                                 <div id = "txtHint1"></div>

                                <p class="stdformbutton">
                                        <button class="btn btn-primary">Generate Report</button>
                                </p>
                        </form>
                    </div><!--widgetcontent-->
                </div><!--widget-->
                .....

getuser1.php代码在这里:

<?php
$q = $_GET['q'];

$con = mysqli_connect('localhost','root','','csci311');
if (!$con) {
  die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"ajax_demo");
$sql="SELECT distinct position FROM $q";
$result = mysqli_query($con,$sql);


echo '<div class="control-group">';
echo '<label class="control-label" for="position">Available position</label>';
echo '<div class="controls"><text style="color:#D84A38;font-size:20px">* </text>';
echo '<select name="position" id="position" class="input-large" onChange="showUser1()>';
echo '<option value="">Choose One</option>';
            while($row = mysqli_fetch_array($result)) {
            echo '<option value="'.$row["position"].'">'.$row["position"].'</option>';
            }
        echo '</select>';
    echo '</div>';
echo '</div>';



mysqli_close($con);
?> 

这里的getuser1-1.php代码:

<?php

    $z = $_GET['z'];

    $con = mysqli_connect('localhost','root','','csci311');
    if (!$con) {
      die('Could not connect: ' . mysqli_error($con));
    }

    mysqli_select_db($con,"ajax_demo");
    $sql="SELECT distinct state FROM $z";
    $result = mysqli_query($con,$sql);


    echo '<div class="control-group">';
    echo '<label class="control-label" for="state">Available State</label>';
    echo '<div class="controls"><text style="color:#D84A38;font-size:20px">* </text>';
    echo '<select name="state" id="state" class="input-large" >';
    echo '<option value="">Choose One</option>';
                while($row = mysqli_fetch_array($result)) {
                echo '<option value="'.$row["state"].'">'.$row["state"].'</option>';
                }
            echo '</select>';
        echo '</div>';
    echo '</div>';



    mysqli_close($con);
    ?> 

1 个答案:

答案 0 :(得分:0)

在你的echo语句中,每个&#34;把它放在它面前。所以它显示了\&#34;。

&#34;是PHP中的保留字符,因此PHP要知道您希望它成为字符串的一部分,您必须在其前面放置\。

处理此问题的另一种方法是交换使用单引号和双引号的位置。使用HTML中的单引号和PHP的双引号。

我还建议获得高质量的IDE。我使用Netbeans。还有很多其他好的。您可以将此代码放在任何体面的IDE中,它会告诉您问题所在。

echo '<div class=\"control-group\">';
echo '<label class=\"control-label\" for=\"position\">Available position</label>';
echo '<div class=\"controls\"><text style=\"color:#D84A38;font-size:20px\">* </text>';
echo '<select name=\"position\" id=\"position\" class=\"input-large\" onChange=\"showUser1()>';
echo '<option value=\"\">Choose One</option>';
            while($row = mysqli_fetch_array($result)) {
            echo '<option value=\"'.$row["position"].'\">'.$row["position"].'</option>';
            }
        echo '</select>';
    echo '</div>';
echo '</div>';