如何管理Ajax成功功能?

时间:2017-07-30 08:32:27

标签: php json ajax

{{ range $paginator.Pages.ByParam "my-param" }}

使用上面的代码,我从表中获得一列。我需要两列。我想尝试下一步:

  if (isset($_POST["getCanvas"]) ) {  
               $projectName= mysqli_real_escape_string($db2, $_POST['whichProject']);
              $query = "SELECT objectsList FROM projectObjectstable  WHERE projectName='$projectName'";
            //  $query = "SELECT objectsList,backgroundImage FROM projectObjectstable  WHERE projectName='$projectName'";
               $jsonCanvas= mysqli_query($db2,$query);
                    $row = mysqli_fetch_row($jsonCanvas);
                    $myLine=$row['0'];
                    echo $myLine;
       }

为此我需要下一个解决方案。如何修改ajax success函数以在画布上获取两个变量(projectList和backgroundImage)?

          $query = "SELECT objectsList FROM projectObjectstable  WHERE projectName='$projectName'";
          $jsonCanvas= mysqli_query($db2,$query);
                $row = mysqli_fetch_row($jsonCanvas);
                $myLine=$row['0'];
                echo $myLine;
             $query2 = "SELECT backgroundImage FROM projectObjectstable  WHERE projectName='$projectName'";
             $jsonBackground= mysqli_query($db2,$query2);
                    $row2 = mysqli_fetch_row($jsonBackground);
                    $myLine2=$row['0'];
                    echo $myLine2;
       }

我们非常感谢您提供额外的信息。在浏览器中调试php代码有哪些选择,因为它可以用js做? 谢谢

1 个答案:

答案 0 :(得分:0)

如前所述,您需要使用json_encode函数将JSON格式的参数发送给JS。在调用echo之前,必须将变量放在数组中。如下所示。

         $query = "SELECT objectsList FROM projectObjectstable  
                   WHERE projectName='$projectName'";
         $_row= mysqli_query($db2,$query);
         $row = mysqli_fetch_row($_row);
         $jsonCanvas=$row['0'];

         $query2 = "SELECT backgroundImage FROM projectObjectstable  
                    WHERE projectName='.$projectName.'";
         $_row2= mysqli_query($db2,$query2);
         $row2 = mysqli_fetch_row($_row2);
         $jsonBackground=$row['0'];

         $to_json['jsonCanvas'] = $jsonCanvas;
         $to_json['jsonBackground'] = $jsonBackground;

         echo json_encode($to_json);
         exit();  

在您的JS中,您在成功函数中收集信息:

$.ajax({
  method:"POST",
  url: '/wp-content/themes/mypage3/PgetJson.php',
  data:  {
    "getCanvas":1,
    "whichProject":whichProjectToSave
    },
    datatype: "json",
    success: function(strdate){
      /*Here on data you will receive
      strdate['jsonCanvas'] & strdate['jsonBackground']
      */
      });
     }
 });

如果您调用同一个表并将相同的变量作为参数传递,我还建议您在一个查询中进行sql调用。