user4264796
user4264796

Reputation:

passing parameter to function

I want to pass the parameter to function. If I pass ID to parameter it is working. But I want to pass string value as parameter to function. I want to add escape sequence to the parameter. If I pass $fet['cf_id'] as parameter it is working. If I pass $fet['file_name'] value, it is not passed.

$file1=$fet['file_name'];
$ef=$fet['cf_id'];
$next1 = basename($file1);
echo '<td style="text-align:center;width:100px;"><img src="image/delete1.png" alt="delete" style="width:10px;height:10px" title="Remove" onclick="myFunction('.$fet['file_name'].');"></td></tr>';
function myFunction(cid) {   
    // alert(cid);
    var  rmvfile=cid;
    //display conformation box
    if (confirm("Are you sure you want to Delete the file?") == true) {
        if(cid!='')
        {
            $.ajax({
                type:'post',
                url:'delete_cli_file.php',
                data:{rmvfile: rmvfile},
                success:function(msg){
                    if (msg.length> 0) {
                        alert(msg);
                        location.reload();
                    }
                }
            });
        }
    }
}

Upvotes: 2

Views: 202

Answers (2)

Malay M
Malay M

Reputation: 1759

instead of

echo '<td style="text-align:center;width:100px;"><img src="image/delete1.png"
      alt="delete" style="width:10px;height:10px" title="Remove"
      onclick="myFunction('.$fet['file_name'].');"></td></tr>'; 

use

echo '<td style="text-align:center;width:100px;"><img src="image/delete1.png"
      alt="delete" style="width:10px;height:10px" title="Remove"
      onclick="myFunction(\''.$fet['file_name'].'\');"></td></tr>'; 

Upvotes: 0

Qaisar Satti
Qaisar Satti

Reputation: 2762

where you pass the text to javascript it must be in 'text'.

       $file1 =$fet['file_name'];
       $a     ="'";
       $ef    =$fet['cf_id'];
       $next1 = basename($file1);
       echo '<td style="text-align:center;width:100px;">
             <img src="image/delete1.png" alt="delete" 
              style="width:10px;height:10px" 
              title="Remove"
              onclick="myFunction('.$a.$fet['file_name'].$a.');">
             </td></tr>';

     function myFunction(cid)
     {   
        // alert(cid);
        var  rmvfile=cid;
        //display conformation box
        if (confirm("Are you sure you want to Delete the file?") == true)       
        {
         if(cid!='')
         {
           $.ajax({
           type:'post',
           url:'delete_cli_file.php',
           data:{rmvfile: rmvfile},
           success:function(msg)
           {
             if (msg.length> 0) 
             {
                alert(msg);
                location.reload();
             }
           }
      });
      }} }

Upvotes: 1

Related Questions