2017-03-01 7 views
1
<?php 

    echo '<form action="index3.php "method="get"> 
     Name: <input type="text" name="fname"/> 
     <br/> 
     Surname: <input type="text" name="fsur"/> 
     <br/> 
     Phone: <input type="number" name="phone"/> 
     <br/> 
     Email: <input type="email" name="email"/> 
     <input type="submit" value="Go">   
    </form>'; 

    $name = $_GET["fname"]; 
    $surname = $_GET["fsur"]; 
    $phone = $_GET["phone"];   
    $email = $_GET["email"]; 

    echo "Hello " . $fname . " " . $fsur . ". You will soon get a confirmation email at: " . $email . " and a confirmation message at this number: " . $phone; 


    ?> 

나는 같은 PHP 파일에서 사용하기 위해 PHP 코드에서 echo 된 양식의 입력 (fname, fsur, phone, email)을 변수에 저장하려고 시도하고 있으며 아래와 같이 출력하려고합니다. 코드를 실행할 때 브라우저에서 이러한 오류가 발생합니다. 나는 파이어 폭스를 사용한다. 내가 할 수있는 방법이 있니? 하여 제출 버튼을 name="submit_form"if(isset($_GET['submit_form'])){을 주목하고 마지막 echo 문 대신 $fname$name을 넣어 $surname을 넣어 :입력 양식을 PHP로 가져 와서 같은 PHP 파일에서 사용할 수 있습니까?

+0

네 그러나 당신은 양식에서 일부 매개 변수는 isset 있는지 확인해야합니다. – webDev

+0

예,하지만 양식이있는 스크립트의 수명주기를 기억하십시오. 스크립트를 처음 실행할 때 사용자는 양식을 제출하지 않으므로 해당 $ _GET 변수 중 NONE이 존재하므로 코드에서 오류가 발생합니다. 그래서 당신은 어떻게 든 그것들을 사용하기 전에 존재하는지 테스트해야합니다. – RiggsFolly

+0

PHP에서 입력 값을 가져 오지 않으려면 양식을 보내야합니다. 그리고 동일한 파일에 코드를 쓰지 않으면이 파일 이름을 양식 작업 속성에 쓰지 않아야합니다 –

답변

2

이 예는 다음과 같이 바로 그것을 할 수 있습니다 할 -

<form action="" method="get"> 
    Name: <input type="text" name="fname"/> 
    <br/> 
    Surname: <input type="text" name="fsur"/> 
    <br/> 
    Phone: <input type="number" name="phone"/> 
    <br/> 
    Email: <input type="email" name="email"/> 
    <input type="submit" name="my_submit" value="Go">   
</form> 

<?php 
/* This check is needed to make sure that the form is submitted 
* Otherwise it'll produce error like - undefined index in $_GET 
*/ 
if(isset($_GET['my_submit'])) { 
    $name = $_GET["fname"]; 
    $surname = $_GET["fsur"]; 
    $phone = $_GET["phone"]; 
    $email = $_GET["email"]; 

    echo "Hello " . $name . " " . $surname . ". You will soon get a confirmation email at: " . $email . " and a confirmation message at this number: " . $phone; 
} 
?> 
+0

'! empty ($ _ GET)'메소드는 좋은 습관이 아닙니다. 이것이 test.php라고 가정하고,'http : //yourhost/test.php? test = 123'과 같은 매개 변수를 전달하여이 파일을 호출하면 고지를 얻게됩니다. 한번 시도해 보면 선명한 그림을 얻을 수 있습니다. 그 이유는 더 나은 양식 요소를 확인하고 일부 PHP 코드를 활성화하십시오. – webDev

+0

대단히 감사합니다! 그것은 완벽하게 작동합니다! –

+0

@Vpcont 문제를 해결할 수 없습니다. 다른 사람이 좋은 답변을 얻을 수 있도록 수정 사항을 승인하십시오. 나는 당신의 코드를 바 꾸었습니다 – webDev

1

는 형식 매개 변수

<?php 

     echo '<form action="" method="get"> 
      Name: <input type="text" name="fname"/> 
      <br/> 
      Surname: <input type="text" name="fsur"/> 
      <br/> 
      Phone: <input type="number" name="phone"/> 
      <br/> 
      Email: <input type="email" name="email"/> 
      <input type="submit" name="submit_form" value="Go">   
     </form>'; 

     if(isset($_GET['submit_form'])){ 
      $name = $_GET["fname"]; 
      $surname = $_GET["fsur"]; 
      $phone = $_GET["phone"];   
      $email = $_GET["email"]; 

      echo "Hello " . $name . " " . $surname . ". You will soon get a confirmation email at: " . $email . " and a confirmation message at this number: " . $phone; 
     } 

     ?> 

주를 확인할 수있는 방법을 참조하십시오.

에코를 사용하는 대신 태그 외부에서 HTML 양식을 직접 사용할 수 있습니다. 조치를 지정하지 않은, <form action="" method="get">

+0

대단히 감사합니다! 이 솔루션은 완벽하게 작동하며 PHP 코드 안에 있습니다! –