使用angular.js和php提交数据后无法显示成功消息

时间:2015-10-01 09:02:18

标签: javascript php angularjs

使用Angular.js和PHP提交数据后,我无法显示成功消息。我正在使用提示提示来显示消息,但它显示输出为undefined。我正在解释下面的代码。

  

createSubjectData.php:

<?php
$course_name=stripslashes($_POST['course_name']);
$sub_name=stripslashes($_POST['sub_name']);
$semester=stripslashes($_POST['semester']);
$sub_short_name=stripslashes($_POST['sub_short_name']);
$con = mysql_connect('localhost', 'root', '******');
mysql_select_db('go_fasto', $con);
 $qry ='INSERT INTO db_subject (subject_name,short_name,semester,course_name) values ("' . $sub_name . '","' . $sub_short_name . '","' . $semester . '","' .$course_name . '")';
$qry_res = mysql_query($qry);
if ($qry_res){
        $result['msg'] = "subject has added successfully";
    } else {
         $result['msg'] =  "subject could not added ";
    }
$query='SELECT * from db_subject order by subject_id desc';
$res=mysql_query($query);
$result=mysql_fetch_array($res);
echo json_encode($result);
?>

我的控制器代码如下所示。

var subjectData={'course_name':$scope.courseName.value,'semester':$scope.semester.value,'sub_short_name':$scope.subject_short_name,'sub_name':$scope.subjectname};
                console.log('subject',subjectData)
                $.ajax({
                    method: 'POST',
                    url: "php/subject/createSubjectData.php",
                    data: subjectData,
                    dataType: 'json',
                    success: function(response){
                        $scope.$apply(function(){
                        alert(response.msg);
                        $scope.courseName=null;
                        $scope.semester=null;
                        $scope.subject_short_name=null;
                        $scope.subjectname=null;
                        //$scope.subjectData.push(response);
                        $scope.subjectData.unshift(response);
                        });
                    },
                    error: function(response){
                        alert(response.msg);
                    }
                })

请帮我解决此问题。

1 个答案:

答案 0 :(得分:1)

您完全覆盖了此行中的$result变量

$result=mysql_fetch_array($res);
之后

您已设置$result['msg']="...",因此msg会丢失。

<强>顺便说一句: 您的代码中存在SQL注入问题。在将输入变量添加到查询字符串或用户mysql prepared statements mysqli_stmt

之前,使用mysql_real_escape_string()来清理输入变量