使用SQL填充选项列表

时间:2014-01-31 09:44:23

标签: php jquery html sql ajax

所以我目前正在尝试使用ajax和php填充一些带有SQL的选项列表。我尝试了各种不同的代码但是我似乎仍然无法破解它。这是实际页面中的ajax自己...

$.ajax ({
    url:'orderentry_ajax.php',
    cache:false,
    data: {'request': 'getCounty', 'County': County},
    dataType: 'json',
    async: false,
    success: function(data)
    {
         $('#errMsg').html(data.errMsg);
         if(data.numRecs>0)
         {
                //divStr = divStr + data.custName + data.contactName + data.contactNumber + data.contactEmail;
                countyStr = countyStr + "<select>";
                for (var i=0; i<data.dataArray.length; i++)
                {
                   countyStr = countyStr + 
                      "<option value='data.dataArray[i].County'>" +
                      "Please Select" + data.dataArray[i].County + "</option>";
                }
                countyStr = countyStr + "</select>";
                $('#Countys').html(countyStr);
         }
   }
   //countyStr =     countyStr + data.dataArray[i].County +
});

就我而言,我做了类似的练习,除了我用另一个表填充选项列表,我已经使两个ajax和php相同,它似乎仍然不想工作。这是来自ajax页面的php ....

if (trim($request) =='getCounty')
{
    //product update
    $County = $_REQUEST['County'];

    $errMsg = "";
    $con = mysqli_connect('127.0.0.1', 'root', 'c0mplex', 'HRDatabase');
    //Check if connect..
    if (mysqli_connect_errno($con))
    {
        $errMsg = 'Could not connect to Database.' . mysqli_connect_error();
    }
    else
    {
       // passed record for submit
       $qryStr = "SELECT * FROM county WHERE `county` = $County";
       //echo $qryStr;
       $result = mysqli_query($con, $qryStr);
       if (mysqli_error($con))
       {
          //echo (mysqli_error($con));
          $errFlg=1;
          $errMsg="Error during update, please try again. " . mysqli_error($con);
       }
       else
       {
           while ($row = mysqli_fetch_array($result))
           {
                $County = $row['county'];

                $rowing = array();
                $rowing['county'] = $County;
                $dataArray[] = $rowing;
            }
            $numRecs = mysqli_num_rows($result);
        }
    }
    mysqli_close($con);
    //to test error :
    // $errMsg="testing error";
    $info ->dataArray = $dataArray;
    $info ->numRecs = $numRecs;
    $info ->errMsg = $errMsg;
    $info ->County = $County;
    echo json_encode($info);
    //echo $msg;

}

选择选项列表上有一个“Countys”的ID,只是为了抬头。任何帮助都会非常感谢大家。

干杯

1 个答案:

答案 0 :(得分:0)

在ajax代码中替换以下行以动态添加html

countyStr = countyStr + "<option value='" + data.dataArray[i].County + "'>" + "Please Select" + data.dataArray[i].County + "</option>";