2014-05-28 3 views
0

예를 들어 '나는 피아노를 치고 싶습니다.'라는 문장이 있습니다. 그것은 4 개의 요소로 분리되어 데이터베이스 (myphpadmin)에서 제거됩니다. 셔플 버튼을 누른 경우에만 어떻게 만들 수 있습니까? 그러면 셔플됩니다.데이터베이스의 onClick 셔플 버튼

<body> 
    <?php 
// Connect to database server 
mysql_connect("localhost", "root", "password") or die (mysql_error()); 

// Select database 
mysql_select_db("login") or die(mysql_error()); 

// Get data from the database depending on the value of the id in the URL 
$strSQL = "SELECT * FROM sentences WHERE id 
    ORDER BY RAND() LIMIT 1;"; 

//create an array with numbers 1-4 
$order = array(1,2,3,4); 

//shuffle them in random order 
shuffle($order); 

$rs = mysql_query($strSQL); 

// Loop the recordset $rs 
while($row = mysql_fetch_array($rs)) { 
    // Write the data of the person 
    //Display all the array values from 0-3 (array index starts from 0) 
    echo "<dt>Sentence:</dt><dd>" . $row[$order[0]] . " " . $row[$order[1]] . " " .      
    $row[$order[2]] . " " . $row[$order[3]] ."</dd>"; 

    } 
// Close the database connection 
mysql_close(); 
?> 

    <button onClick="Shuffle()">Scramble</button> 

    </body> 

답변

1

당신은 기본적으로 jQuery이 작업을 수행해야 할 것입니다.

jQuery.get()을 사용하여 별도의 PHP 스크립트에 대해 ajax 요청을 실행하십시오. 기본적으로 다음과 같이 코드를 설정 한 PHP 스크립트가 있습니다.

바로 가기 : sentence.php.

<?php 
// Connect to database server 
mysql_connect("localhost", "root", "password") or die (mysql_error()); 

// Select database 
mysql_select_db("login") or die(mysql_error()); 

// Get data from the database depending on the value of the id in the URL 
$strSQL = "SELECT * FROM sentences WHERE id 
    ORDER BY RAND() LIMIT 1;"; 

//create an array with numbers 1-4 
$order = array(1,2,3,4); 

//shuffle them in random order 
shuffle($order); 

$rs = mysql_query($strSQL); 

// Loop the recordset $rs 
while($row = mysql_fetch_array($rs)) { 
    // Write the data of the person 
    //Display all the array values from 0-3 (array index starts from 0) 
    echo "<dt>Sentence:</dt><dd>" . $row[$order[0]] . " " . $row[$order[1]] . " " .      
    $row[$order[2]] . " " . $row[$order[3]] ."</dd>"; 

    } 
// Close the database connection 
mysql_close(); 
?> 

지금 당신은 당신의 html 파일이있을 것이다, 호출 할 수 있습니다 당신의 script.js 당신이 jQuery를 실행 줄에서 index.html

<html> 
    <head> 
     <script src="https://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script> 
     <script src="script.js" type="text/javascript"></script> 
    </head> 

    <body> 
     <button id="showcontent">Scramble</button> 
     <div id="content"></div> 
    </body> 

</html> 

그.

$(document).ready(function() { 

    $(document).on('click', '#showcontent', function(event) { 
     event.preventDefault(); 

     $.get("sentence.php", function(data) { 
      $("div#content").html(data); 
     }); 
    }); 

}); 
+0

내가 시도했지만 배열이 HTML 페이지에 표시되지 않습니다. 셔플 버튼 만 있습니다. – user3678617

+0

@ user3678617 실제 파일에 연결되는 스크립트를 수정 했습니까? 웹 개발자 도구 콘솔에서 오류가 있는지 확인하십시오. – Darren

+0

오류가 발견되지 않고 스크립트를 수정하여 html 파일에 연결했습니다. 그러나 주인공은 셔플 버튼을 허용하지 않습니다. – user3678617