Skip to content
Advertisement

Properly use single and double quatation in PHP

I have my PHP code like below and its work fine

<tr onclick="window.location='../info/<?php echo $ouid;?>';" class="tbl_rated_orders">

Now I am loading more data on press load more button via ajax

so I am trying like this

$tableData.= '<tr class="tbl_rated_orders_buyer" onclick="window.location='.'"../info/'.$ouid.';">
                                    
                                        <td>'.$date_rated.'</td>
                                        <td><img src="../../global_assets/uploads/users/'.$image.'" width="35" class="rounded-pill" alt="">
                                        <span class="ml-2">'.$rated_row["full_name"].'</span>
                                        </td>
                                        
                                        <td>'.$cancelled_by_txt.'</td>
                                        <td><i class="icon-paperplane text-dark mr-1"></i>'.$str1.'</td>
                                    </tr>';

But now tr html code looks like this

<tr class="tbl_rated_orders_buyer" onclick="window.location=" ..="" info="" zxp2ibhfnu;"="">

so its not correct code for window location, its need like this

<tr onclick="window.location='../info/RQ5KWBIY6M';" class="tbl_cancelled_orders">

I am new in PHP and trying from half hour to make it working but not getting idea how I can do it, Let me know if anyone here can help me for do the same. Thanks!

Advertisement

Answer

You will need to escape some single quotes for the single quotes used in the javascript in the onclick attribute.

$ouid = "asd0f8a08f0";
$date_rated = "12/03/2021";
$image = "north_pole.jpg";
$rated_row["full_name"] = "Mighty Mouse";
$cancelled_by_txt = "grinch";
$str1 = "Paper Plane";
$tableData = "";

$tableData.= '<tr class="tbl_rated_orders_buyer" onclick="window.location='.''../info/'.$ouid.';'">
                                    
                                        <td>'.$date_rated.'</td>
                                        <td><img src="../../global_assets/uploads/users/'.$image.'" width="35" class="rounded-pill" alt="">
                                        <span class="ml-2">'.$rated_row["full_name"].'</span>
                                        </td>
                                        
                                        <td>'.$cancelled_by_txt.'</td>
                                        <td><i class="icon-paperplane text-dark mr-1"></i>'.$str1.'</td>
                                    </tr>';

Output:

<tr class="tbl_rated_orders_buyer" onclick="window.location='../info/asd0f8a08f0;'">
                                
                                    <td>12/03/2021</td>
                                    <td><img src="../../global_assets/uploads/users/north_pole.jpg" width="35" class="rounded-pill" alt="">
                                    <span class="ml-2">Mighty Mouse</span>
                                    </td>
                                    
                                    <td>grinch</td>
                                    <td><i class="icon-paperplane text-dark mr-1"></i>Paper Plane</td>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement