2014-10-16 54 views
-1

在提交表單時遇到未定義的變量問題。請幫助PHP中未定義變量,表單提交

有人可以告訴我我錯過了什麼嗎?

我打算調用函數'process_input'的表單提交,並在裏面的過程輸入我得到的ip/uid/pwd的細節。

     $.ajax({ 
           type : 'POST', 
           url : 'post_process.php', 
           data : { 
             func : 'process_input' 
           }, 
         }).done(function(data) { 

           // show the response 
           $("#response").html(data); 

PHP

if (isset ($_POST ['func']) && ! empty ($_POST ['func'])) { 
     $func = trim ($_POST ['func']); 
     switch ($func) { 
       case 'process_input' : 
         process_input(); 
         break; 
         .......... 
     } 
} 


function process_input() { 
     header ('Content-Type: text/html; charset=utf-8'); 

     if (isset ($_POST ['ip']) && ! empty ($_POST ['ip'])) { 
       $ip = trim ($_POST ["ip"]); 
     } 
     if (isset ($_POST ['uid']) && ! empty ($_POST ['uid'])) { 
       $uid = trim ($_POST ["uid"]); 
     } 
     if (isset ($_POST ['pwd']) && ! empty ($_POST ['pwd'])) { 
       $pwd = trim ($_POST ["pwd"]); 
     } 

     echo "$ip $uid $pwd <br>"; 
... 
} 

說明:未定義變量:ip的在上線41

所有變量($ip$uid$pwd)來作爲未定義

+0

你變得未定義哪個變量? – 2014-10-16 13:55:40

+0

$ ip,$ uid,$ pwd變量。 – angshuman 2014-10-16 13:56:43

+0

您是否收到任何錯誤?在打開'<?php tag error_reporting(E_ALL)'後立即將錯誤報告添加到文件頂部。 ini_set('display_errors',1);' – 2014-10-16 13:59:19

回答

0

問題在這條線上;

echo "$ip $uid $pwd <br>"; 

定義具有檢查上方默認值的變量。因爲可能會出現不是全部都設置好的情況。像這樣;

function process_input() { 

    $ip = ''; 
    $uid = ''; 
    $pwd = ''; 

    header ('Content-Type: text/html; charset=utf-8'); 

.... 
+0

試過這樣做。但是現在變量是空的。表單提交不起作用。我錯過了什麼? – angshuman 2014-10-16 14:02:51

+0

很明顯,變量不會在你的函數的if語句中被設置。嘗試var_dumping發佈數據。 – 2014-10-16 14:11:45