2016-05-03 7 views
0

セルフからの結果で更新されないのはなぜ。カーIDのユーザー入力を受け付けるHTMLページを作成し、ボタンをクリックすると、その自動車の情報がテキストボックスに表示されます。私は多くのドキュメンテーション、SO、ウェブを検索していて、現在の学習指針に基づいて私が探している方法を見つけることができませんでした。 PHPを使ってexplode()メソッドを使うか、ajax/jsonはもっとうまくいくのですか?私のテキストボックスは、私は各章のための方法を適用する方法を学ぶために使用している書籍の章の終わりに問題を使用してPHP自分自身を教えクエリ

私が検索をクリックすると、テキストボックスに何も入力されていないので、誰かが私が何をやっていないのか教えてくれます。ありがとう!

<?php 
$host = "localhost"; 
$dbname = "car_search_db"; 
$user = "pontiac"; 
$password = "Race_Day!"; 

try { 
    $db = new PDO("mysql:dbname=$dbname;host=$host",$user,$password); 

} 
catch (PDOException $e) { 
    $error_message = $e->getMessage(); 
    include ('error.php'); 
    exit(); 
} 

$car_ID = isset($_GET['car_ID']) ? $_POST['car_ID'] : ''; 
if($car_ID == "") 
    die("Invalid input"); 

$query = 'SELECT carID, make, model, year FROM fastCars 
      WHERE carID = :car_ID';   
$statement = $db->prepare($query); 
$statement->bindValue(':car_ID', $car_ID); 
$statement->execute(); 
$cars = $statement->fetchAll(PDO::FETCH_ASSOC); 

$json = json_encode($cars); 
echo $json; 
?> 

<!DOCTYPE html> 
<head> 
<title>Fast Car Page</title> 
<link rel="stylesheet" type="text/css" href="main.css"> 
</head> 
<body> 
<script type="text/javascript"> 
function queryCars() { 
    //get user input 
    var id = document.getElementById('car_ID').value; 

    //create a query string 
    var queryString = "?car_ID=" + car_ID; 

    //create XMLHTTP Request 
    var ajaxRequest; 
    try { 
     ajaxRequest = new XMLHttpRequest; 
    }catch(e) { 
     //IE Browsers 
     try { 
      ajaxRequest = new ActiveXobjectect("Msxml2.XMLHTTP"); 
      } catch (e) { 
       try{ 
        ajaxRequest = new ActiveXobjectect("Microsoft.XMLHTTP"); 
       } catch (e){ 
        // Something went wrong 
        alert("Browser not compatible"); 
        return false; 
       } 
      } 
    } 
    //create ajax function that processes server response 
    ajaxRequest.onreadystatechange = function() { 
     if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) { 
      var object = JSON.parse(xmlhttp.responseText); 
      document.getElementById('car_id').value = object[0].car_ID; 
      document.getElementById('make').value = object[1].make; 
      document.getElementById('model').value = object[2].model; 
      document.getElementById('year').value = object[3].year;   

     } 
    }; 
      //execute the query 
      xmlhttp.open("POST", "car_search.php" + queryString, true); 
      xmlhttp.send(); 
} 


</script> 
<header><h1>Fast Car Lookup - Search & Split</h1></header> 
<br> 
<h2>Search for a car info by ID</h2> 
<br> 
<form action="car_search.php" method="get"> 
<label>StudentID:</label> 
<input type="text" id="id" name="car_ID"> 
<br> 
<br> 
<input type="button" onclick="queryCars();" value="Search" id="get_info"> 

<h2>information will be displayed in the textboxes below for the car ID chosen:</h2> 
<br> 

<table id="outuput"> 
<tr> 
    <td><label>Car ID:</label></td> 
    <td><input type="text" id="car_ID" value="" readonly></td> 
</tr> 
<tr> 
    <td><label>Make:</label></td> 
    <td><input type="text" id="make" value="" readonly></td> 
</tr>  
<tr> 
    <td><label>Model:</label></td> 
    <td><input type="text" id="model" value="" readonly></td> 
</tr> 
<tr> 
    <td><label>Year:</label></td> 
    <td><input type="text" id="" value="" readonly></td> 
</tr> 
<br> 
</table> 
</form> 
<a href="index.htm">Back to first page</a> 
</body> 
</html> 
+0

'' mysql:dbname = $ dbname; host = $ host "'そこにスペースは入れてはいけません。やる '「のmysql:DBNAME = $ dbnameは、ホスト= $ホスト」マニュアルhttp://php.net/manual/en/pdo.connections.php –

+0

あなたもまた可能性があり、フォーム外の入力を持っているあたりとして'ここで問題を起こす。 –

+0

ここにコピー/貼り付けエラーがありました。私はあなたがそれらをチェックしていなかったので、何もすべて – allendks45

答えて

1

あなたはここで間違った変数をバインドされています

$statement->bindValue(':car_ID', $student_ID); 

$statement->bindValue(':car_ID', $car_ID); 

あなたはPHPでのポストを使用しているが、車のIDはクエリ文字列であるべきである - 必要がありますPHPでGETを使用している。

関連する問題