我有一個 JavaScript 函數,當用戶單擊一個按鈕時,它會在表單中添加另一行。我需要在此行中添加一個下拉菜單,以查詢 mySQL DB 以加載公司的數據。HTML:<input type="button" class="center tmid" value="Add New Line" id="add_btn" name="add_btn" onclick="newLine(<?php echo $rowId; ?>, 'timecard')">JavaScript:function newLine(a,page) { if (page == 'timecard') { //get total rows in the table and subtract the header row var rowCount = $('#tsTable tr').length; var i = rowCount - 1; var rowId = parseFloat(a)+parseFloat(i); var tcId = this.tcId.value; $.get('functions/getCompany.php', function(result){ var companyList = result; }); //create new row data var newRowData = '<tr><input name="tcdid[]" type="text" value="'+rowId+'"><td><input type="date" name="jobDate[]" id="date'+rowId+'" value=""></td><td><input type="text" class="input-short" name="hours[]" id="h'+rowId+'" value=""></td><td><input type="text" class="input-short" name="ot[]" id="ot'+rowId+'" value=""></td><td><input type="text" class="input-short" name="km[]" id="km'+rowId+'" value=""></td><td class="nopadding"><select name="company[]" id="company'+rowId+'"></td><td><input type="text" name="ticket[]" id="ticket'+rowId+'" value=""></td><td><input type="text" name="wo[]" id="wo'+rowId+'" value=""></td><td><input type="text" name="details[]" id="d'+rowId+'" value=""></td><td class="td-shorter" ><input type="button" value="X" name="delete" class="btnDelete" onclick="deleteTimeLine(this,'+ rowId +',0)"></td></tr>'; //append new row to bottom of the table $(newRowData).appendTo($("#tsTable tbody"));}PHP:function getCompany() { global $conn; $sqlC ="SELECT companyName FROM customers ORDER BY companyName ASC"; $resultC = $conn -> query($sqlC); if($resultC === false){ user_error("Query failed: ".$conn->error."<br />".$resultC); return false; }所以我想在 getCompany() 函數中添加填充了結果的公司下拉列表。我該怎么辦?任何幫助,將不勝感激。謝謝!
1 回答

拉莫斯之舞
TA貢獻1820條經驗 獲得超10個贊
您通常會在 javascript 端使用 AJAX 調用來執行此操作:
$.get('/path/to/file.php', function (result) {
console.log(result); // do something with result returned
}
然后在php方面:
<?php // /path/to/file.php
// perform your query, set variables
if ($success) {
echo json_encode([
'status' => 'success',
'html' => '<option value='.$companies['companyName'].'>'.$companies['companyName'].'</option>'
]);
}
echo json_encode([
'status' => 'error',
'message' => 'Something went wrong, handle this'
]);
- 1 回答
- 0 關注
- 99 瀏覽
添加回答
舉報
0/150
提交
取消