skip to Main Content

I have this function to read html contents

function patient_file(){
$html= '';
$html .= '<table style="width:100%; padding:0; margin:0; color:rgb(50,50,50); ">
<tr><td style="width:17%"><b style="color:rgb(150,150,150)"> Fullname</b> </td><td>  salum said juma</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> Age / sex</b> </td><td>  12 male </td></tr>
<tr><td><b style="color:rgb(150,150,150)"> Address</b> </td><td>  kariakoo dsm</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> File number </b> </td><td>  mn234</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> kitu package </b> </td><td>  scheme name</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> kitu number </b> </td><td>  1234567890</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> kiyutu</b> </td><td>  1919181716151</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> hihi in </b> </td><td> 12-03-2023</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> hihi out</b> </td><td>   12-03-2023  </td></tr>';
$html .= '</table>';

return $html;

}

then after i call the fuction and create json encode

$folio=$_POST['folio'];
$sqlRun = mysqli_query($conn2, "SELECT *  FROM m_folio where m_No='$folio'");
$row = mysqli_fetch_assoc($sqlRun);
$row['cardata'] = patient_file();
$json_array[] = $row;
$jasondata = json_encode($json_array);
 $payload = '{"entities": ' . $jasondata . '}';
echo $payload;

now the payload generated give error on jsonformatter
this is my output

{"entities": [{"cardata":"rn
Fullname</b> </td>    salum said juma</td></tr>rn
Age / sex</b> </td>  12 male </td></tr>rn
Address</b> </td> kariakoo dsm</td></tr>rn
File number </b> </td>    mn234</td></tr>rn
kitu package </b> </td>   scheme name</td></tr>rn
kitu number </b> </td>    1234567890</td></tr>rn
kiyutu</b> </td>  1919181716151</td></tr>rn
hihi in </b> </td>    12-03-2023</td></tr>rn
hihi out</b> </td>    12-03-2023t</td></tr></table>"}]} 

2

Answers


  1. Don’t create $payload using string operations. Use json_encode().

    $folio=$_POST['folio'];
    $stmt = mysqli_prepare($conn, "SELECT * FROM m_folio where m_No = ?"
    mysqli_stmt_bind_param($stmt, "i", $folio);
    mysqli_stmt_execute($stmt);
    $sqlRun = mysqli_stmt_get_result();
    $row = mysqli_fetch_assoc($sqlRun);
    $row['cardata'] = patient_file();
    $json_array[] = $row;
    $payload = json_encode(["entities" => $json_array])
    echo $payload;
    

    I’ve also changed the mysqli code to use a prepared statement to prevent SQL injection.

    Login or Signup to reply.
  2. Remove all line breaks from $html in patient_file() before returning.

    function patient_file(){
    
    $html= '';
    $html .= '<table style="width:100%; padding:0; margin:0; color:rgb(50,50,50); ">
    <tr><td style="width:17%"><b style="color:rgb(150,150,150)"> Fullname</b> </td><td>  salum said juma</td></tr>
    <tr><td><b style="color:rgb(150,150,150)"> Age / sex</b> </td><td>  12 male </td></tr>
    <tr><td><b style="color:rgb(150,150,150)"> Address</b> </td><td>  kariakoo dsm</td></tr>
    <tr><td><b style="color:rgb(150,150,150)"> File number </b> </td><td>  mn234</td></tr>
    <tr><td><b style="color:rgb(150,150,150)"> kitu package </b> </td><td>  scheme name</td></tr>
    <tr><td><b style="color:rgb(150,150,150)"> kitu number </b> </td><td>  1234567890</td></tr>
    <tr><td><b style="color:rgb(150,150,150)"> kiyutu</b> </td><td>  1919181716151</td></tr>
    <tr><td><b style="color:rgb(150,150,150)"> hihi in </b> </td><td> 12-03-2023</td></tr>
    <tr><td><b style="color:rgb(150,150,150)"> hihi out</b> </td><td>   12-03-2023  </td></tr>';
    $html .= '</table>';
    
    /** remove all line breaks **/
    $string = trim(preg_replace('/s+/', ' ', $html));
    
    return $string;
    
    }
    
    

    This will turn the HTML into one long string which javascript friendly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search