2014-06-23 57 views
2

我不知道爲什麼我得到這個奇怪的錯誤!未定義的索引請求

PHP公告:未定義指數:REFID在/var/www/echo.php第5行

我得到的控制檯輸出,但不能呼應refId。我在這裏做錯了什麼嗎?

<?php 
    $rollUrl = 34; 
    $refId = $_POST['refId']; 
    echo $refId; 
?> 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script> 
    $.ajax({ 
     url:'echo.php', 
     type: 'POST', 
     data: { 'refId': "<?php echo $rollUrl ?>" }, 
     success: function(response){ 
      console.log('Getting response'); 
     } 
    }); 
</script> 
+0

如果這都是一個文件,那麼在發送POST之前'$ _POST'被引用。在這個塊周圍添加一個條件來檢查'if(count($ _ POST)> 0)' –

回答

0

發生這種情況是因爲未設置變量。使用isset

<?php 

$rollUrl=34; 
if(isset($_POST['refId'])) { 
$refId=$_POST['refId']; 
echo $refId; 
} 

?> 

更新: 您應該分配refId作爲name屬性的任何輸入欄重振來自用戶的輸入。

<input type="text" name="refId" /> 
+1

不,這沒有幫助..!它只有一個文件。 –

+0

檢查輸入字段的名稱並確保已將其設置爲'refId'。 –

+0

是啊...!我沒有任何輸入字段。它只是上面的代碼。 –

1

請參閱下面的代碼中的註釋:

<?php 
$rollUrl = 34; 

//Only try to process POST if there is something posted *and* refId exists 
if (count($_POST) > 0 && isset($_POST['refId'])) { 
    $refId = $_POST['refId']; 
    echo $refId; 
    //Exit after echoing out the refId so that the HTML below does not also get returned. 
    exit(); 
} 
?> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script> 
    $.ajax({ 
     url:'echo.php', 
     type: 'POST', 
     data: { 'refId': "<?php echo $rollUrl ?>" }, 
     success: function(response) { 
      //Updated log to show the actual response received. 
      console.log('Getting response of "' + response + '"'); 
     } 
    }); 
</script> 

這對我來說,當我沒有拋出任何錯誤和阿賈克斯正在執行測試工作。

+0

米勒..!我運行相同的代碼你建議..! Infact複製粘貼它..!但沒有得到迴應。 :( –

+0

如果是我編輯之前的版本,請再試一次,我在我的系統上有一個名爲diff的文件,如果它在之後,請確保您在控制檯或將其更改爲一個警報 –

+0

是啊..!我改變了文件名..!那是呃。得到回聲值?? –