2015-03-02 99 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