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.

    评论

报告相同问题?