2012-09-20 6 views
2

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”정의되지 않은 인덱스는

내가 PHP를 배우고 난 양식 데이터를 제출 포스트 방법을 사용하고 난 그 두 가지 오류 얻을 이유를 알 수 없습니다

Notice: Undefined index: search_for in \xampp\htdocs\samir\indexes.php on line 5

Notice: Undefined index: replace_with in \xampp\htdocs\samir\indexes.php on line 6

if(isset($_POST['user_input']) && isset($_POST['search_for']) && isset($_POST['replace_with']) 
    && !empty($_POST['user_input']) && !empty($_POST['search_for']) && !empty($_POST['replace_with'])) 
    echo $user_input = $_POST['user_input']; 
    echo $search_for = $_POST['search_for']; 
    echo $replace_with = $_POST['replace_with']; 
+2

일부 코드를 어쩌면? –

+1

코드를 보여 주시겠습니까? –

+0

우리는 당신의 코드를 볼 필요가있다 !!! – simonlchilds

답변

1

$ _POST 또는 $ _GET을 사용하여 양식에서 변수를 검색하는 경우이 오류가 발생할 수 있습니다.

Notice : 정의되지 않은 색인의 ' 테이블 '에서'PHP 파일의 경로가 실행되고 있습니다 '현재 줄'

이 오류는 PHP 오류보고 설정 때문에 나타납니다. 일반적으로 변수가 올바르게 설정되지 않은 경우 나타납니다. 이 문제를 처리하는 방법은 두 가지가 있습니다.

1. 사용하기 전에 $_POST['action'] 또는 $GET['action']이 설정되어 있는지 확인하십시오. 예를 들어 :

if (!isset($_POST['action'])) {//your pure stuff }  

if (!isset($_GET['action'])) {//your pure stuff } 

2. 억제 공지 사항 경고

주의 경고는 php.ini 파일에서 error_reporting를 변수를 변경하여 억제 할 수있다. error_reporting은 모든 사항에 대한 제외한 오류 및 코딩 표준 경고 표시하도록 설정할 수 있습니다 : error_reporting은 = E_ALL &을 ~ E_NOTICE

<?php error_reporting (E_ALL^E_NOTICE); ?> 

를하지만 내 개인적인 제안은이 방법


에게 대신 사용의 경고를 해결하다

업데이트 질문 대답

당신은 그래서에서와 같이 하나 개의 라인 고려할 것 {} 경우 뒤의 괄호를 둘러싸는 추가하지 않은 F 본체와 2 층과 3 에코의 결과가 참 또는 거짓 인 경우

그것은 당신과 같이 당신의 if 문 둘러싸는 괄호를 추가하는 것을 잊었다

if(isset($_POST['user_input']) && isset($_POST['search_for']) 
    && isset($_POST['replace_with']) && !empty($_POST['user_input']) 
    && !empty($_POST['search_for']) && !empty($_POST['replace_with'])) { 
    echo $user_input = $_POST['user_input']; 
    echo $search_for = $_POST['search_for']; 
    echo $replace_with = $_POST['replace_with']; 
} 
3

수 있는지 여부 실행됩니다 :

if(isset($_POST['user_input']) && isset($_POST['search_for']) 
    && isset($_POST['replace_with']) && !empty($_POST['user_input']) 
    && !empty($_POST['search_for']) && !empty($_POST['replace_with'])) { 
    echo $user_input = $_POST['user_input']; 
    echo $search_for = $_POST['search_for']; 
    echo $replace_with = $_POST['replace_with']; 
} 

위의 코드는 원하는대로 수행해야합니다.

+1

'if'문 뒤에 나오는 첫 번째 명령 만 조건부로 처리되므로, 'if'의 결과가 true인지 false인지에 관계없이 두 번째 및 세 번째 에코가 실행됩니다. – Basic

+0

감사합니다. 매우 도움이되었습니다 – user1481225

+0

당신은 오신 것을 환영합니다! :-) – Nelson

1

양식을 게시하기 전에 변수를 사용하는 경우 (예 : $var = $_POST['var']; ) 오류가 반환됩니다.

전송 버튼이 눌러져 있는지 확인하는 것이 가장 좋습니다.

예 : 오류가 발생하지 않을 경우

if(isset($_POST['submit'])){ 
    //form has been posted 
} 

그럼, 당신이 사용하게 될 모든 포스트 변수가 설정되어 있는지 확인합니다.

예 :

$error = false; 
//['submit'] is the button used to post the form. 
if(isset($_POST['submit'])){ 
    $testVar = (isset($_POST['testVar']) && strlen($_POST['testVar']) > 0 ? $_POST : $error[] = 'Please enter something.'); 

    if(!$error){ 
     //Submit for 
    } 
    else{ 
     foreach($error as $e){ 
      echo $e; 
     } 
    } 
}