2015-04-21 54 views
0

試圖根據點擊事件從數據庫獲取信息並將數據加載到稍後將在表單中使用的變量中。這是代碼的一部分。已經使用PDO連接到銷售代表表格:pdo調用填充變量以便稍後使用

function fill_contact(evt) //display click event 
    { 
     var country_id = evt.target.id 
     document.getElementById('country_name').firstChild.data = country_id 

     <?php 
    $country = $_GET['country_name']; 
    try { 
    // Establish server connection and select database 
    $dbh = new PDO("mysql:host=$host;dbname=$database", $user, $password); 
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    } catch(PDOException $e){ 
    die("Sorry, we could not connect you to this database, please contact the Administrator to sort this out.". $e->getMessage());/** try open contact form here**/ 
    } 
    /** run prepare to select records **/  
    $stmt = $dbh->prepare("SELECT firstname, lastname, email, photo FROM reps WHERE country = :country"); 
    $stmt->bindParam(':country', $country, PDO::PARAM_STR); 
    $stmt->execute(); 
    ?> 

    <script language="javascript" type="text/javascript"> 
    function fill_contact(evt) //display click event 
    { 
    var country_id = evt.target.id 
    document.getElementById('country_name').firstChild.data = country_id 
    /* need to get a ajax http fetch statement here???*/ 
    } 

    function getDataFile(mapString , useData) 
    { 
    /** call getURL() if available **/ 
    if (window.getURL) { 
    getURL(mapString , useData); 
    } 
    else 
    /** call XMLHttpRequest() if available **/ 
    if (window.XMLHttpRequest) 
    { 
     function XMLHttpRequestCallback() 
     { 
     if (xmlRequest.readyState == 4) {  useData({success:xmlRequest.status,content:xmlRequest.responseText,contentType:xmlRequest.getResponseHeader("Content-Type")}) 
     } 
     } 
    var xmlRequest = null; 
     xmlRequest = new XMLHttpRequest(); 
     xmlRequest.open("GET",mapString,true); 
     xmlRequest.onreadystatechange = XMLHttpRequestCallback; 
     xmlRequest.send(null); 
     } 
    /** write an error message if either method is not available **/ 
    else { 
    alert("your browser/svg viewer neither supports window.getURL nor window.XMLHttpRequest!"); 
    } 
    } 
</script> 

<body> 
<div id="IndexPage_Left_top"> 
      <h1>Country: <span class="label" id="country_name"> </span> </h1> 
      <table > 
       <tbody>      
        <tr> 
        <td><h4>Regional Representative: </h4></td> 
        <td><h4><span class="label" id="firstname">$firtname</span> <span class="label" id="lastname">$lastname</span></h4></td><!--get Rep name from database based on country from svg to display--> 
        </tr> 
        <tr></tr> 
        <tr height="300px" align="top"> 
        <td ><h4> Photo:</h4></td><!--get Rep photo from database based on country from svg to display--> 
        <td><span class="photo" id="photo"></td> 
        </tr> 
        <tr> 
        <td></td> 
        <td><input style='margin-top: 20px;' type="submit" value="Contact me" id='jqxButton' /></td> 
        </tr><!-- if clicked, it opens a contact form below, the email address is hidden--> 

       </tbody> 
      </table>  
     </div> 
     <div id="IndexPage_Left_bottom"> 
     <h4>email form here<h4> 
     <p> email address is <span class="label" id="email">$email</span> </p><!-- if email is clicked, it opens a contact form here with email address from above --> 
     </div> 
    </div> 
    <div id="IndexPage_Right"> 

<svg id="map" version="1.1" rest of svg code  

這與我關於svg的其他問題有關。 (displaying data from sql from clicking on svg map)一旦我點擊某個svg,它就會填充用於顯示有關所點擊國家/地區的數據的變量。這些變量也用於填充聯繫表單。

它不想填充表格?

+0

您是否按照預期測試了Web服務的正確輸出。 – Codelord

+0

嘿。你應該在這裏複製/粘貼確切的錯誤(即使它對你來說似乎是一個簡單的語法錯誤。)另外,你提到你的其他問題?還有什麼問題?你能添加一個鏈接嗎?如您所說, –

回答

0

你的PHP代碼有錯誤

更改代碼$query = $dbh->prepare('SELECT firstname, lastname, email FROM reps WHERE 'country_id' = :country');

$query = $dbh->prepare('SELECT firstname, lastname, email FROM reps WHERE country_id = :country'); 
+0

更改了代碼。仍然收到語法錯誤。我可能會有點慢,現在已經編程了3個月。寶寶步驟plse。 – ArtPur

0

所以,問題是出在那些線,這是他們應該如何看:

$query = $dbh->prepare('SELECT firstname, lastname, email FROM reps WHERE country = :country_id'); 
$query->bindValue(':country_id', $country, PDO::PARAM_INT); 

一端,參數被命名爲country_id,但是您將該值綁定到參數country。 PDO很棒,但它還不能做心靈感應。

我徹頭徹尾喜歡使用位置參數,清除所有這容易出錯的手寫:

$query = $dbh->prepare('SELECT firstname, lastname, email FROM reps WHERE country = ?'); 
$query->execute(array($country)); 

你總是可以傳遞一個陣列到​​方法與待綁定中的值。使用位置參數時,數組必須是包含值的0索引數組,其值與您的查詢中的順序相匹配。您也可以使用命名參數來完成此操作,而無需與訂單匹配:

$query->execute(array(':country_id'=>$country)); 

請注意,您無法將兩者混合在一起。如果您使用命名參數準備查詢,則必須全部使用命名參數,並將其綁定,如果您使用位置佔位符,則它們必須全部爲位置佔位符,並且必須如此綁定。

+1

謝謝,這幫了很大的忙! – ArtPur

+0

我的榮幸。另外,我剛剛注意到你仍在使用'mysql_ *'。我建議你閱讀[this](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers)。事實上,如果您的問題中發佈的代碼完全運行,那麼我會很驚訝,因爲似乎有一些語法錯誤。如果我可以提供另一個幫助學習的建議,請在每個php腳本的頂部添加'ini_set('display_errors',1); error_reporting(E_ALL)''的習慣。它可能真的幫助你 –

+0

我用你的建議,並閱讀更多。我終於搞定了:)現在的問題是編寫(學習如何)使用XMLHttpRequest並在click事件中使用數據庫中的數據。我的代碼現在 – ArtPur