douyi3632 2014-01-31 09:44
浏览 34
已采纳

使用SQL填充选项列表

So I'm currently in the process of trying to populate a select option list with some SQL using ajax and php. I've tried various different pieces of code however I still can't seem to crack it. Here is the ajax from the actual page its self...

$.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 +
});

As far as I'm concerned I did a similar exercise except I was populating the options list with another table, I've made the two pieces of ajax and php identical and it still doesnt seem to want to work. Here is the php from the ajax page....

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;

}

The select option list has an ID on it of 'Countys' just to give a heads up. Any help would be greatly appreciated guys.

Cheers

  • 写回答

1条回答 默认 最新

  • duana1021 2014-01-31 09:50
    关注

    Replace below line in your ajax code for adding html dymically

    countyStr = countyStr + "<option value='" + data.dataArray[i].County + "'>" + "Please Select" + data.dataArray[i].County + "</option>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类