, 당신은 같은 일을 PHP 파일/행동이 필요 :
: 사전 제작 된 HTML 페이지에서
$return = array();
$q = mysql_query('SELECT * FROM ...');
while(($return[] = mysql_fetch_assoc($q)) !== false);
echo json_encode($return);
을, 당신은 데이터를 인쇄하려면 용기가 필요합니다
function getDB() {
$.getJSON(
'http://url.to/your/code.php',
{},
function(data) {
var renderedHTML = '';
/* parse the object representing PHP's $return, mainKey will be numerical keys */
for(var mainKey in data) {
/* one main loop iteration == one table row */
renderedHTML += '<tr>'
/* data[mainKey] is a row in DB, subKey will contain a name of a DB table column */
/* data[mainKey][subKey] will therefore contain the value of DB table column 'subKey' for the DB table row numbered 'mainKey' */
for(var subKey in data[mainKey]) {
renderedHTML += '<td>' + data[mainKey][subKey] + '</td>'
}
/* statically add another HTML table column for the buttons */
renderedHTML += '
<td>
<input type="button" name="b1" onclick="func1(\'arg1\')">
<input type="button" name="b2" onclick="func2(\'arg2\')">
</td>
';
renderedHTML += '</tr>'
}
/* insert the built table into the page */
$('#db-container').html('<table>' + renderedHTML + '</table>');
}
);
}
:
<div id="db-container"></div>
그런 다음 JS는 (당신이 아마 기본 JS 또는 기타 JS 도구를 사용 할 수 있지만 내가 단순하고 읽기 쉽도록 여기에 jQuery를 사용) 해당 페이지에 포함
희망이 도움이됩니다.
감사합니다. 저는 개인적으로 jQuery를 모르지만 당신의 접근 방식을 이해합니다. 아마 자바 스크립트에서 이것을 할 수 있습니다. 감사! – urbanspr1nter
당신을 진심으로 환영합니다! jQuery를 JS로 변환 할 때,'$ .getJSON'은 예상되는 리턴 타입'text/json'을 갖는'XmlHttpRequest' 일뿐입니다. 여러분이 선호하는 인코딩을 사용할 수는 있습니다. '$ ('element') .html (data)'는 document.getElementById ('element'). innerHTML = data'와 동일합니다. 그러나 'innerHTML'은 대부분의 브라우저에서 제대로 구현되지 않았으므로보다 안전한 DOM 메소드 인'createElement'와'appendChild' ([링크는 여기에 있습니다] (http://domscripting.com/blog))와 함께 사용하는 것이 좋습니다./display/99)를 사용하여 하위 트리를 삽입 할 때 DOM을 올바르게 다시 작성하도록 유도합니다. 행운을 빕니다! –