2017-03-26 22 views
0

我有我無法解決的錯誤。我有兩種形式的提交按鈕:輸入名稱以兩種不同形式更改,從而導致名稱解決錯誤

<form name="form_1" action=<?php echo $_SERVER['PHP_SELF'] ;?>> 
    <input type="text" name="search_1" value="<?php echo $_GET['search'] ; ?>"> 
    <input type="submit" value="Search by Name"> 
    </form> 

<form name="form_2" action=<?php echo $_SERVER['PHP_SELF'] ;?>> 
    <input type="text" name="search_2" value="<?php echo $_GET['search'] ; ?>"> 
    <input type="submit" value="Search by Message"> 
    </form> 

錯誤說:search_1是未定義的。它怎麼可能是未定義的? 順便說一句,如果我使兩個表單輸入名稱相同:search一切正常,但正常情況下,如果我把價值在一種形式,得到相同的輸入以其他形式也。 我做錯了什麼?

爲form_1 PHP代碼:

<?php 


    // Check if the form has been sent or if the page was accessed using the GET method. 
    if(filter_input(INPUT_SERVER, "REQUEST_METHOD") !== "GET") { 
    die(); 
    } 

    $search = filter_input(INPUT_GET, "search"); 

    // Check if the searh input has been correctly fill 
    if(!isset($search)) { 
    die(); 
    } 
    // Let's return here to prevent open an empty file 
    if(filesize($file) <= 0) { 
    return; 
    } 

    $openedFile = fopen($file, "r"); 

    // I was using the here document option, echo <<<END END; but it didn't work due to my text editor... 
    echo " 
    <table width=50% border=1> 
    <thead> 
     <tr> 
     <th>Name</th> 
     <th>Nick</th> 
     <th>Message</th> 
     </tr> 
    </thead> 
    <tbody> 
    "; 

    // Let's save in buffer the current line and then navigate through all of them 

    while(($buffer = fgets($openedFile, 4096)) !== false) { 


    $part = explode("|", $buffer); 
    // Here we check if the line has the searched word 
    if(strpos($part[0], $search) !== false && $search != '') { 

     echo " 
      <tr> 
      <td>" . $part[0] . "</td> 
      <td>" . $part[1] . "</td> 
      <td>" . $part[2] . "</td> 
      </tr> 
     "; 

    }//.if 
    }//.while 

    echo " 
    </tbody> 
    </table> 
    "; 
?> 
+0

一個提交HTML表單內按鈕只提交_that_形式,而不是其他的。 – arkascha

+0

你能顯示php代碼片段嗎? –

+0

@PartharajDeb這與php無關... – arkascha

回答

0

您從$_GET陣列式打印的價值,而不檢查,是在URL或不通過。

請注意,當您提交包含名稱search_1的第一個表單時,此時search_2不得在$_GET數組中設置,但是您不打算在輸入中打印該表單而不檢查。所以當你提交一個表單時,它會以另一種形式提供錯誤。

嘗試像這個 -

<form name="form_1" action=<?php echo $_SERVER['PHP_SELF'] ;?>> 
    <input type="text" name="search_1" value="<?php echo isset($_GET['search_1'])?$_GET['search_1']:"" ; ?>"> 
    <input type="submit" value="Search by Name"> 
</form> 

<form name="form_2" action=<?php echo $_SERVER['PHP_SELF'] ;?>> 
    <input type="text" name="search_2" value="<?php echo isset($_GET['search_2'])?$_GET['search_2']:"" ; ?>"> 
    <input type="submit" value="Search by Message"> 
</form> 
+0

現在沒有錯誤,但是如果我點擊第二個表單提交按鈕。第二種形式和form_1之前的所有內容都丟失了。我的意思是消失。 – NotsoPr0

+0

也許這是錯誤的,我在這兩種形式使用相同的PHP變量,但一個是seach_1和其他search_2? – NotsoPr0

+0

這是一個基本的概念,當你提交表單時,其他表單的任何字段都不會被提交。如果您需要,我更喜歡使用帶有search_1和search_2兩個輸入的單一表單。 –