2013-11-25 77 views
3

我有一個下拉列表,其中用戶選擇類型以及用戶在下拉列表中單擊其他位置時,必須顯示已完成的文本框。如何獲得基於用戶選擇創建的文本框的值

但現在我怎樣才能獲得文本框的值並插入到數據庫中。

這是我的腳本,當用戶選擇其他選項時顯示文本框。

<script type="text/javascript"> 
    function CheckColors(val) { 
     var element = document.getElementById('others'); 
     if (val == 'others') element.style.display = 'block'; 
     else element.style.display = 'none'; 
    } 
</script> 

<form name="f1" method="POST" action=""> 
    <select name="type" onchange='CheckColors(this.value);'> 
     <option value="1">1</option> 
     <option value="2">2</option> 
     <option value="others">others</option> 
    </select> 
    <input type="text" name="others" id="others" style='display:none' /> 
    <input type="submit" name="submit" /> 
</form> 

一切工作正常..除了從文本框中獲取價值。任何人可以幫助我如何獲得文本框的值,並插入到數據庫..

+0

你嘗試過什麼?似乎是從發佈的字段獲取值的基本情況... –

+0

您應該能夠使用$ _POST ['others']獲取文本框的值 – J2D8T

回答

1

你的HTML頁面a.html

<script type="text/javascript"> 
    function CheckColors(val) { 
     var element = document.getElementById('others'); 
     if (val == 'others') element.style.display = 'block'; 
     else element.style.display = 'none'; 
    } 
</script> 

<form name="f1" method="POST" action="b.php"> <!--post data to b.php--> 
    <select name="type" onchange='CheckColors(this.value);'> 
     <option value="1">1</option> 
     <option value="2">2</option> 
     <option value="others">others</option> 
    </select> 
    <input type="text" name="others" id="others" style='display:none' /> 
    <input type="submit" name="submit" /> 
</form> 

PHP頁面b.php

<?php 
     if(isset($_POST['submit'])) 
    { 

     $selectType=$_POST['type']; 
     $inputText=$_POST['others']; 

     $mysqli = new mysqli("localhost", "my_user", "my_password", "dbname"); 

     /* check connection */ 
     if (mysqli_connect_errno()) { 
      printf("Connect failed: %s\n", mysqli_connect_error()); 
      exit(); 
     } 

     $sql="insert into tablename (keyType,keyOther) values(?,?)"; 

     /* create a prepared statement */ 
     if ($stmt = $mysqli->prepare($sql)) { 

     /* bind parameters for markers */ 
     $stmt->bind_param("ds", $selectType,$inputText); 

     /* execute query */ 
     $stmt->execute(); 

     /* close statement */ 
     $stmt->close(); 
    } 

    /* close connection */ 
    $mysqli->close(); 
    }?> 
0

您可以只使用一個if語句在PHP代碼,像這樣:

<?php 

if ($_POST['type'] == 'others') { 
    $type = $_POST['others']; 
} else { 
    $type = $_POST['type']; 
} 

?> 

然後你就可以插入$型成數據庫就像通常那樣,顯然要小心地正確驗證和轉義數據。

0

你可以把價值

  firsrt check 

      if(isset($_POST["submit"])) 
       { 
       $other = $_POST["other"]; 

       then fire your sql insert query by making connection to database. 


       } 
     else 
      { 
      echo "post ! set"; 
      } 
0
<?php 
    if(isset($_POST['submit'])) 
{ 

    $others=$_POST['others']; 
    } 
?> 
0

我假設你張貼的形式到另一個網頁,並使用MySQL。

您的表單應該有action="page.php" Page.php是您提交給的頁面。

這裏是page.php文件可能是什麼:

<?php 

    $con=mysqli_connect("ipAddress","my_user","my_password","my_db"); 
    //your database connection 

    $other = $_POST['others']; 
    //The "others" is the name of the field your posting 

    mysqli_query($con,"INSERT INTO... fieldname = '$other'"); 
    // your SQL query goes here. 


?> 

而且,你要防止SQL注入,這裏是一個鏈接,讓你開始與。 http://www.veracode.com/security/sql-injection

在將數據插入數據庫之前,您想確保驗證,清理,檢查錯誤等。如果有人試圖攻擊你的數據庫,不這樣做可能會導致嚴重的問題。另外,您可能希望在另一個文件中設置數據庫連接變量並將其包含在內,這樣您的信息就會更安全一些。

相關問題