2015-03-02 96 views
-1

我終於得到了一個表來發表使用Ajax的PHP文件,但是我一直遇到以下錯誤數據:接收未定義指數PHP的錯誤,即使是沒有錯誤

「通知:未定義指數: COURSE_TITLE在/Applications/XAMPP/xamppfiles/htdocs/insights/ManageCourses_UpdateSubmit.php上線26

說明:未定義指數:course_code在/Applications/XAMPP/xamppfiles/htdocs/insights/ManageCourses_UpdateSubmit.php第27行 NULL NULL 記錄更新「

我發現是奇怪的,因爲它更新記錄罰款時,我檢查MySQL表,當我刷新頁面的更新值顯示。

按鈕被點擊時觸發AJAX腳本是:

<script> 
function myCall() { 
    var request = $.ajax({ 
     url: "ManageCourses_UpdateSubmit.php", 
     type: "GET",    
     dataType: "html" 
    }); 

    var data = $('#updateForm').serialize(); 
    $.post('ManageCourses_UpdateSubmit.php', data); 

    request.done(function(msg) { 
     $("#updateForm").html(msg);   
    }); 

    request.fail(function(jqXHR, textStatus) { 
     alert("Request failed: " + textStatus); 
    }); 
} 
</script> 

我注意到,當我在AJAX代碼中刪除這一點,我沒有得到該錯誤消息不過,我需要的頁面刷新一旦該值已更新。

request.done(function(msg) { 
     $("#updateForm").html(msg);   
    }); 

    request.fail(function(jqXHR, textStatus) { 
     alert("Request failed: " + textStatus); 
    }); 

對此感到遺憾忘了提交主文件

的ManageCourses_UpdateSubmit.php文件是:

<?php 

include "db_conx.php"; 

try 
{ 
    $db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password); 

    $db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

$sql = $db_conx->prepare("UPDATE course_details SET course_title = :course_title 
    WHERE course_code = :course_code"); 

$course_title = $_POST['course_title']; 
$course_code = $_POST['course_code']; 
echo var_dump($course_title)."<br>"; 
echo var_dump($course_code)."<br>"; 

$sql->bindParam(':course_title', $course_title, PDO::PARAM_STR); 
$sql->bindParam(':course_code', $course_code, PDO::PARAM_STR); 


/*** execute the prepared statement ***/ 
$sql->execute(); 

/*** success message ***/ 
$message ='record updated'; 
} 
catch(Exception $e) 
{ 
    $message = 'Message: ' .$e->getMessage(); 
} 


?> 
<html> 
<head> 
<title>Update Course</title> 
</head> 
<body> 
<p><?php echo $message; ?> 
</body> 
</html> 

什麼想法?

+0

[PHP:「注意:未定義變量」和「通知:未定義的索引」]的可能重複(http://stackoverflow.com/questions/ 4261133/php-notice-undefined-variable-and-notice-undefined-index) – Rizier123 2015-03-02 13:58:11

+1

因此修復未定義的索引。 – 2015-03-02 13:58:16

+0

向我們展示'ManageCourses_UpdateSubmit.php'中的代碼 – Alex 2015-03-02 13:59:11

回答

1

您提出了2個請求。 1st是一個GET請求,所以沒有設置POST變量。沒有必要爲這個請求,發佈請求也將返回一個響應,這樣你就可以使用:

function myCall() { 

    var data = $('#updateForm').serialize(); 
    $.post('ManageCourses_UpdateSubmit.php', data, function(response){ 
     //display message 
     $("#updateForm").html(response); 
     //'soft'reload parent page, after a delay to show message 
     setTimeout(function(){ 
      window.location = window.location; 
     },1000); 


    }).fail(function(jqXHR, textStatus) { 
     alert("Request failed: " + textStatus); 
    }); 
} 

另外請注意,你不想響應包含<head><body>標籤,因爲它是被添加到現有的頁面,因此ManageCourses_UpdateSubmit.php應該結束這樣的:

catch(Exception $e) 
{ 
    $message = 'Message: ' .$e->getMessage(); 
} 

die($message); 
//nothing else after this 
+0

你是美好的。它的效果很好。在調用php之後有沒有辦法刷新頁面? – user90210 2015-03-02 14:08:49

+0

是的,但爲什麼你需要刷新頁面? – Steve 2015-03-02 14:09:47

+0

基本上有一個模式(彈出窗口),當用戶希望編輯表格中的行信息時出現。所以我試圖用ajax觸發對php文件的調用並返回結果,然後更新表格所在的主html頁面,以便用戶可以看到更新的表格行信息。 – user90210 2015-03-02 14:12:44