2012-08-02 85 views
1

我在表單提交後保留表單元素中的值。我得用它來很好地工作一個選擇框如下:使用PHP變量填充文本字段

<select name="BranchManager" class="formfield" id="BranchManager"onchange="document.forms[0].submit();SEinit();"><option value="">-- Select Manager --</option> 
     <?php 
      $area = $_POST['Area']; 
      if ($area); { 

       $BMquery = "SELECT DISTINCT Branch_Manager FROM Sales_Execs WHERE AREA = '$area' ". 
       "ORDER BY Branch_Manager"; 

       $BMresult = mysql_query($BMquery); 

       while($row = mysql_fetch_array($BMresult)) 
       { 
        echo "<option value=\"".$row['Branch_Manager']."\">".$row['Branch_Manager']."</option>\n "; 
       } 
      } 

     $branchmanager = $POST['BranchManager']; 
     ?> 

     <script type="text/javascript"> 
document.getElementById('BranchManager').value = <?php echo json_encode(trim($_POST['BranchManager']));?>; 
</script> 

工作正常(道歉,如果它是不乾淨/最有效的代碼,我做我的最好的!)第二天字段是一個文本字段,需要根據上面的分支經理名稱填寫。所以我用過:

<input name="BranchNum" type="text" class="formfield" id="BranchNum" size="3" maxlength="3" /> 
         <?php 

       $bm = $_POST['BranchManager']; 

       if ($bm); { 

       $BNumquery = "SELECT DISTINCT BRANCH_NUM FROM Sales_Execs WHERE Branch_Manager = '$bm' "; 

       $BNumresult = mysql_query($BNumquery); 

       } 

      $branchnum = $POST['BranchNum']; 
      ?> 

    <script type="text/javascript"> 
document.getElementById('BranchNum').value = <?php echo json_encode($BNumresult);?>; 
</script> 

哪一個不工作...我在哪裏錯了?

+3

[SQL Injection](http://en.wikipedia.org/wiki/SQL_injection)。 [閱讀它](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)。 – PeeHaa 2012-08-02 16:46:41

+0

@PeeHaa是對的,你應該停止使用'mysql_'函數並開始使用MySQLi或PDO。 – alfasin 2012-08-02 16:47:45

+0

請不要在新代碼中使用'mysql_ *'函數。他們不再被維護,社區已經開始[棄用流程](http://goo.gl/KJveJ)。請參閱[**紅框**](http://goo.gl/GPmFd)?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定,[本文](http://goo.gl/3gqF9)將有助於選擇。如果你關心學習,[這是一本很好的PDO教程](http://goo.gl/vFWnC)。 – PeeHaa 2012-08-02 16:48:03

回答

-1

你需要把價值= 「<回聲$ VARIABLENAME;?>」 輸入框裏面

+0

你爲什麼要把這個值放到一個json編碼的字符串中,然後讓javascript填寫表單域?爲什麼不按照我的答案直接填寫表單域? – SublymeRick 2012-08-02 16:47:59

-1

的原因是因爲:a)你不echo ING,和b)你必須在不同的echo現貨比選擇。您必須在輸入的value部分回顯。

<?php 
    $bm = mysql_real_escape_string($_POST['BranchManager']); 
    if ($bm) { 
     $BNumquery = "SELECT DISTINCT BRANCH_NUM FROM Sales_Execs WHERE Branch_Manager = '$bm' "; 
     $BNumresult = mysql_query($BNumquery); 
    } 
    $branchnum = $POST['BranchNum']; 
?> 
<input name="BranchNum" 
    type="text" 
    class="formfield" 
    id="BranchNum" 
    size="3" 
    maxlength="3" 
    value="<?php echo htmlspecialchars($branchnum); ?>" /> 

根據評論,他們是正確的;你不應該使用mysql_*。相反,看看PDO;儘管這不在你的問題的範圍之內。

+4

仍有SQL注入漏洞-1 – PeeHaa 2012-08-02 16:48:37

+0

停止使用'mysql_ *'函數。他們正在被棄用。請使用[PDO](http://php.net/manual/en/book.pdo.php)(自PHP 5.1起支持)或[mysqli](http://php.net/manual/en/book)。 mysqli.php)(自PHP 4.1起支持)。如果你不確定使用哪一個,[閱讀這篇文章](http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons)。 – Matt 2012-08-02 16:48:50

+0

你到底投票給我什麼? OP問了一個問題,我回答了。我不會鼓吹最佳做法而置之不理。 – wanovak 2012-08-02 16:49:29

0

爲什麼如果條件檢查後你有分號?

if ($bm); 

if ($area); 

這將永遠終止花括號中的語句,並不管是總是會得到執行在$ BM或價值$面積

+0

好評 - 但不是答案。 – alfasin 2012-08-02 17:15:12

0

您需要mydql_fetch功能,以檢索從$結果數據無關。

if($row = mysql_fetch_array($BNumresult)) 
    $branchNum = $row[BRANCH_NUM]; 

爲什麼在輸入標籤的size = 3時使用json_encode?