user2574957
user2574957

Reputation: 39

Escaping special characters while changing the text color in PHP

In the STATUS column of my table there could be 3 value (POSTED, CANCEL, NOT YET POSTED)

each value has its assigned color. I did that using this code:

if($posted == 'posted')
                {
                $posted = "<b style='color:blue'>posted</b>";

                }
                elseif($posted == 'cancel')
                {
                $posted = "<b style='color:red'>cancel</b>";
                }
                else
                {
                $posted = "<b style='color:green'>not yet posted</b>";

                }

but I need to pass the values through javascript. The values cant be passed when it has the style codes. <b style='color:green'>

I tried to use mysql_real_escape_string but it looses the color and everything is passed to the parent window including the code style='color:green'> I there any way to escape the special characters.? without loosing the color style.? Pls help

the values came from my database. It is stored in here;

$db_array[] = array("serial_no" => "$serialno", "model" => "$model", "desc" => "$description", "series" => "$dr_srs", "refno" => "$dr_no", "custname"=> "$name", "client_code"=> "$client_code", "ctelno" => "$phone", "contpers" => "$name2", "brand" => "$brandname", "unit_type" => "$unit_type", "posted" => "$posted", "address" => "$address", "city" => "$city"); 

the array is shown in a table:

foreach($db_array as $key => $value)
            {
                if(($key % 2) == 0)
                {

                ?>
                        <tr class='hoverstate2' onclick="clicked('<?php echo $value['serial_no']; ?>','<?php echo $value['posted']; ?>','<?php echo $value['model']; ?>','<?php echo $value['desc']; ?>','<?php echo $value['series']; ?>','<?php echo $value['refno']; ?>','<?php echo $value['custname']; ?>','<?php echo $value['client_code']; ?>','<?php echo $value['ctelno']; ?>','<?php echo $value['contpers']; ?>','<?php echo $value['brand']; ?>','<?php echo $value['unit_type']; ?>','<?php echo $value['posted']; ?>','<?php echo $value['address']; ?>','<?php echo $value['city']; ?>')">

                        <td ><?php echo $value['serial_no']; ?></td>
                        <td ><?php echo $value['model']; ?> </td>
                        <td ><?php echo $value['desc']; ?></td>
                        <td ><?php echo $value['posted']; ?></td></tr>
                <?php
                }

when click it is pass through this javascript:

    function clicked(serial_no,posted,model,desc,series,refno,custname,client_code,ctelno,contpers,brand,unit_type,posted,address,city)

{
    var client_code = client_code;
    var serialno = serial_no;
    var posted = posted;
    var model = model;
    var desc = desc;
    var series = series;
    var refno = refno;
    var custname = custname;
    var ctelno = ctelno;
    var contpers = contpers;
    var brand = brand;
    var posted = posted;
    var address = address;
    var city = city;

}

Upvotes: 0

Views: 422

Answers (1)

Drixson Ose&#241;a
Drixson Ose&#241;a

Reputation: 3641

There are lots of way to solve your situation. Something like this:

In your css:

.posted{
  color:blue;
}

.cancel{
 color:red;
}

.default{
 color:green;
}

And in your PHP, instead of using if-else statement you might try to use switch statement. You can achieve it something like this:

switch($posted){
  case 'posted':
    $posted = "<b class='posted'>posted</b>";
    break;
  case 'cancel':
   $posted = "<b class='cancel'>cancel</b>";
    break;
  default:
   $posted = "<b class='default'>not yet posted</b>";
   break;
}

See I changed your style and put it into a class. Much cleaner and in your javascript you just need to call the class that you want.

Upvotes: 1

Related Questions