weixin_33733810 2013-12-09 10:15 采纳率: 0%
浏览 97

在PHP中执行echo命令

I have an encoded object containing array of layers which is sent through an ajax call to some php file say myfile.php. The php file simply decodes this object and pushes the layers name into the database. Now i want to show layerName in front end which is currently pushing to the database.

My ajax request is as follows:

$.ajax(
 {
  type: 'POST',
  url: "funcs.php",
  data: 
  {
   targetAction : 'getProjectAssetId',
   MetaData: allFiles,
  },
  success: function(data)
  {
    console.log("data: "+data);
  },
//dataType: "json"
}); 

Funcs.php encoded object handling is as follows:

if(isset($_POST['MetaData']))
{

    $project= explode("ProjectTag",$_POST['MetaData']);  
    echo "
Project: ".$project[0]."
";

    $projectName=explode("Bounds: ",$project[0]);
    $projectBounds=$projectName[1];
    $projectName=explode("ProjectName:",$projectName[0]);
    $projectName=$projectName[1];
    echo "ProjectName: ".($projectName)."
"."ProjectBounds: ".$projectBounds."
";
    $bound=json_decode($projectBounds,true);
    // Hard coded groupID and no reference ID
    $projectId= getProjectAssetId($projectName,25,'',$bound,''); // inserting Project Name in DB with hardcoded groupID and no reference
    echo "
 ProjectId: ".$projectId."
";
    $files=explode(",oneFileTag,",$project[1]);


    for($fileCounter=0;$fileCounter<count($files);$fileCounter++)
     {
         $message='Adding Layer: '.$files[$fileCounter];
        echo'<scripttype="text/javascript">alert(\'asd\');notification("'.$message.'",2,"loading"); </script>';

        $Layer=explode("LayerFieldTag",$desFileFields[0]);
        $LayerName=explode("LayerName:",$Layer[0]);
        $LayerName=$LayerName[1]; 
        echo "


LayerName: ".($LayerName)."
";

        $typeId= getLayerAssetType($LayerName,1);       
        $layerId=getLayerAssetId($LayerName,$projectId);

        echo "
typeid: ".$typeId."
";
        echo "
Layerid: ".$layerId."
";

    }

I want to execute javascript function in for loop, I mean I want to show all filenames in a dialog which is written in js files. The issue i am facing is it would be called by an echo but this echo would be caught on success function of ajax request. I want to execute this echo command.

  • 写回答

1条回答 默认 最新

  • weixin_33747129 2013-12-19 15:47
    关注

    You have your JS callback logic in the wrong place. As a rule, avoid writing Javascript logic in with PHP.

    Instead, you should return a JSON array from your PHP file, then move the for loop to the JS success callback, and loop through the JSON array response there.

    For example, something like...

    dataType: 'json',
    success: function(files) {
        for (var i in files) {
            alert('asd');
            notification('Adding Layer: ' + files[i], 2, 'loading');
        }
    }
    

    Based on looking at your JS and PHP I would HIGHLY recommend spending more time learning best practices and coding style guidelines before proceeding too much further. Take the time and care to learn how to become a skilled developer.

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮