将数据从 AngularJS 发送到 PHP 以进行 PHP 中的 IF-语句,也许使用正确的语法



我正在尝试将数据从AngularJS发送到PHP

我正在从AngularJS controller.js发送数据。我正在通过PHP接收数据。数据对于其他变量来说很好。

但是,我不知道哪个部分是问题所在。

我的目标是让PHP识别$step_number并在PHP中运行 if 语句,但即使我发送了错误的数据,它也能工作!

这意味着,如果 PHP 中的$step_number'2' ,则 PHP 中的 if 语句应该不起作用,如下所示。但是,它运行了!

我认为这是编写代码的正确方法,因为其他数据正在传递给PHP

控制器.js

$http.post("../crud/projects_update.php",{
    step_number : '1', // This one, I am trying to send, but I don't know whether this is not being sent or the problem is occurred in PHP.
    project_id : $scope.project_data.project_id // This works in the PHP. It is well sent.
    project_title : $scope.project_data.project_title
}

.PHP

require_once 'connect.php';
$data = json_decode(file_get_contents("php://input"));
$step_number = $data->step_number; // This should be received from AngularJS and if the $step_number is not matched in the IF-statement, then the function in { } should not work.
if ($step_number = '1'){
    $project_id = $data->project_id;
    $project_title = $data->project_title;
    $conn->query("UPDATE `projects` SET
    `project_title` = '$project_title',WHERE `project_id` = $project_id") or die(mysqli_error());   
}

有两件事需要改变:-

1.将step_number : '1',更改为step_number : 1,

2.变更

if ($step_number = '1'){ //this is assignment not comparison

自:-

if ($step_number == 1){ // now it's comparison 

最新更新