2012-03-09 10 views
0

私はIDを持つ質問データベースを作成しました。今私はajaxを使用して、最初の次の前の最後のボタンを使用してmysqlデータベースの質問に1つずつアクセスしています。どうやってするの。ここに私のコードです。mysqlから1つ1つずつ詳細にアクセスする方法First Previous Next Lastボタンとajax?

のindex.php

<html> 
<head> 
<script type="text/javascript"> 
var str; 
function showid(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","request.php?q="+str,true); 
xmlhttp.send(); 
} 
</script> 
</head> 
<body onLoad="showid(1)"> 

<br /> 
<div id="txtHint">Requested Data 
</div> 
<br /> 

<input type="button" value="Last" name="First" onClick="showid(1)" /> 
<input type="button" value="Previous" name="Previous" onClick="showid(2)" /> 
<input type="button" value=Next name="Next" onClick="showid(3)" /> 
<input type="button" value="First" name="Last" id="Last" onClick="showid(4)" /> 


</body> 
</html> 

要求ページ

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

$con = mysql_connect("localhost", "root", ""); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("dbase", $con); 

$sql="SELECT * FROM table WHERE id = '".$q."'"; 

$result = mysql_query($sql); 

while($row = mysql_fetch_array($result)) 
    { 
    $id=$row['id']; 
    } 
mysql_close($con); 



?> 
+0

<入力タイプ

+0

問題:エラーまたは応答が正しくありません。 ? – Engineer

+0

ありがとうございます。応答にはエラーはありません。しかし、私は、次の&前のボタンロジックに基づいてshowQuestion()関数に値を動的に渡したいと思います。 –

答えて

0

そこから現在の質問番号と仕事を設定するにはJavaScriptを使用してみてください。

<script type="text/javascript"> 
var str; 
var current_question = 1; 
function showQuestion(_direction) 
{ 
if(_direction == 'prev') current_question--; 
else if(_direction == 'next') current_question++; 
else if(_direction == 'first') current_question = 1; 
else if(_direction == 'last') current_question = ?; // use php to set this when producing page 
//... 
xmlhttp.open("GET","question.php?q="+current_question,true); 
xmlhttp.send(); 

は、そのように

<input type="button" value="|<" name="First" onClick="showQuestion('first')" /> 
<input type="button" value="<" name="Previous" onClick="showQuestion('prev')" /> 
<input type="button" value=">" name="Next" onClick="showQuestion('next')" /> 
<input type="button" value=">|" name="Last" id="Last" onClick="showQuestion('last')" /> 
+0

W ------多くのあなたに感謝します。それは本当に今働く! –

+0

@マフフズこれはあなたの質問を解決した場合、あなたは私の答えを完全またはupvoteとしてマークすることができます –

関連する問題