0

I have a scenario which i have to use this way of appending lines into table The javascript isn't accepting any input type='text' , for unknown reason, If i put a normal text variable, it is working .I think there is a problem in single quotes / double quotes

  <?php
    for($counter=0;$row_work_entry=mysqli_fetch_array($run_work_entry);$counter++){
        $work_entry_days=$row_work_entry[0];
        $work_entry_type_id=$row_work_entry[1];
        $totalhours=date('h.i',strtotime($row_work_entry[2]));
        $name=$row_work_entry[3];
        $code=$row_work_entry[4];
        $workingdaystablelines.="<tr><td><input type='text'/></td><td>".$code."   </td><td>".$work_entry_days."</td><td>".$totalhours."</td></tr>";
     }

    ?>

<script>
    $(document).ready(function(){
         var workingdaystablelines ='<?php echo $workingdaystablelines;?>';
         $('#worked_days_table').append(workingdaystablelines);
    });
</script>
5
  • what error you get? is there any logs? Commented Oct 1, 2020 at 8:47
  • 2
    Yes, of course there is a problem with the quotes. But why are you using JS for this in the first place, why do you not place the PHP output into the correct element directly? Commented Oct 1, 2020 at 8:48
  • the only reason I could imagine is if this table is 3rd party and generated elsewhere post doc ready. As for the initial question yes its the quotes. the ' quote in Javascript is seen as an opening of a string and thus breaking your append Commented Oct 1, 2020 at 8:50
  • php.net/manual/en/function.htmlspecialchars.php you need to escape the string in prep to inject into the HTML ie(echo htmlspecialchars($workingdaystablelines, ENT_QUOTES);) Commented Oct 1, 2020 at 8:54
  • 1
    Please share more details. How is this problem related to PHP? Commented Jan 14, 2023 at 18:40

1 Answer 1

2

I tested your code locally but single quotes gives me an error

 $(document).ready(function(){
   var workingdaystablelines = "<?php echo $workingdaystablelines;?>"
   $('#worked_days_table').append(workingdaystablelines)
 })

You may use double qoutes instead

and also you may use htmlspecialchars to avoid xss attack

echo htmlspecialchars($workingdaystablelines,ENT_QUOTES);
Sign up to request clarification or add additional context in comments.

1 Comment

have you tested it with double quotes? ... Because that will break too...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.