2013-11-25 3 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

당신은 그냥이 같은 PHP 코드에서 if 문을 사용할 수 있습니다 .. 어떻게 텍스트 상자의 값을 가져 와서 DB에 삽입하는 나를 도울 수있는 것은 :

<?php 

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

?> 

는 그런 다음에 $ 유형을 삽입 할 수 있습니다 데이터베이스를 평소와 마찬가지로, 분명히 올바르게 데이터의 유효성을 검사하고 이스케이프 처리하는 것이 중요합니다.

0

당신은 내가 당신이 다른 페이지에 양식을 게시하고, MySQL을 사용한다고 가정

  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

로 값을 취할 수 있습니다.

양식에 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

데이터를 데이터베이스에 삽입하기 전에 유효성 검사, 위생 검사, 오류 검사 등을하고 싶습니다. 다른 사람이 데이터베이스를 공격하려고 시도하면 심각한 문제가 발생할 수 있습니다.

또한 다른 파일에 데이터베이스 연결 변수를 설정하여 포함 시키면 정보가 좀 더 안전합니다.

관련 문제