2017-03-22 7 views
0

PHPでライブラリプロジェクトを開発中&私はデータベースに3つのテーブルを作成しています:library_books、student_db & issued_books。データベースから別のselectの値を取得します

issue.phpという名前のページがあり、管理者が学生に書籍を発行するために作成されています。ページには、Book_id、Book_title、Author、Class(I〜XIIの学生のクラス)、Student_name & Student_id(テキストタイプが無効)などの列があります。

私は私が達成したいものを、library_booksデータベース自体から

をすべてブック関連の詳細を取得していますが、ユーザがクラスのフィールド値を選択したときに、ページが自動的に学生のリストを取得する必要ありそのクラスに登録されている名前&このリストからStudent_nameを選択すると、Student_idが自動的にStudent_idフィールドに取得されます。私は適切に選択オプションを使用してDIV /入力にDBから一部のデータを移入するためには、何が必要理解していれば

ここだが、コードで、plzは見

<select required class='styled-select green semi-square' style='width: 15%; margin-left: 5px;' id='Class' name='st_Class'> 
    <option selected='true' disabled='disabled' value=''>Select Class</option> 
    <option>I</option> 
    <option>II</option> 
    <option>III</option> 
    <option>IV</option> 
    <option>V</option> 
    <option>VI</option> 
    <option>VII</option> 
    <option>VIII</option> 
    <option>IX</option> 
    <option>X</option> 
    <option>XI</option> 
    <option>XII</option> 
    </select> 
    <select required class="styled-select green semi-square" style="width: 15%" id="st_name" name="st_name"> 

    <option selected='true' disabled='disabled' value=''>Select Student</option> 
<?php 
    $query2 = mysqli_query($conn,"SELECT Student_name FROM Student_db WHERE Class='st_Class'"); 
while($row1 = mysqli_fetch_array($query2,MYSQLI_NUM)) 
{ 
    $st_name = $row1[6]; 
    $st_id = $row1[5]; 
} 
?> 
    <option><?php echo(isset($_POST['st_name'])&&($_POST['st_name']=='1'));?></option> 
    </select> 
    <input type="text" id="st_id" value="<?php echo $st_id?>" name="st_id" disabled/> 
+1

そして正確にあなたの問題は何ですか?または、これはちょうど "コーディング要求"ですか? –

+0

@Alex_Odenthal私はすでに達成したいことを言いました、plzはもう一度それを読んで、ありがとう –

+0

Ok。しかし、あなたはこのフォーラムの条件を正確に読んでください。これは「他の人たち、私の仕事をしてくださいね!」と求める場所ではありません。あなたは問題を示し、エラーまたは非常に正確にあなたが欲しいものとこれまで達成したことを説明します。今すぐあなたはいくつかのコードを投稿し、このコードが好きなことを世界に伝えます。 –

答えて

0

を取る、あなたが持つことができます見てくださいjQuery change function。ストレートドキュメントから:

あなたの場合には選択

に変更イベントをアタッチし、あなたはIDの(またはIDの名前からを持って選択リスト、それが動作から生徒データを取得することができますいずれかの方法は、限り名前はあなたのDB内で一意のIDにリンクされているとして)

私はあなたのために間違いなくない書き込みコードでしょう、あなたにパスを表示:)

ページの先頭部分にjQueryライブラリを含めることを忘れないでください:)

例あなたがで遊ぶことができます。

- >選択リスト(ここでは、ハードコードされ、それを作るために、PHPのクエリから来る必要がありますシンプル)

<select id="st_id"> 
<option value=""> -- select --</option> 
<option value="1"> Name 1</option> 
<option value="2"> Name 2</option> 
<option value="3"> Name 3</option> 
</select> 
<p>RESPONSE : <input type="text" name="response" id="response" value="" /></p> 

- > jQueryの一部: - > PHPの一部

$(document).ready(function() { // to make sure it's all loaded before use 

$("#st_id").on('change', function() { // #st_id is the ID of your select 
      var id = $(this).val(); // to catch the value of the selected option 
      $.ajax({ 
      url: "select_get_data.php", // page that will catch/handle the ID and retrieve data from DB accordingly 
      method: "POST", 
      data: "id="+id, // the selected ID 
      success: function(resp){ // if PHP sends correct answer 
      $("#response").val(''+resp+''); // #response is the ID of the div/input where data response will be populated 
      }, 
      error: function (request, status, error) { 
      alert(request.responseText); 
      } 
      }); 
     }); 
}); 

このことができます

<?php 
error_reporting(E_ALL); ini_set('display_errors', 1); // to handle errors if any 

$id = $_POST['id']; // from AJAX -> here, you must perfom some checking 
// never ever trust data from users !!! 

echo"Name is MYNAME with ID #$id"; // this will be the response handled in JS by the line 
// success: function(resp){ // your echo will be known as 'resp' 

// of course, you need to write all checking and query instead of just echo'ing 
// but that's the way it works... 
?> 

希望...

関連する問題