1

I have a javascript function that displays html code that is stored in the mysql database into CKeditor <textarea>. When a user selects any of the options, the mysql query is meant to echo the chunk of html code into the javascript as a php variable.

The select option works really fine in selecting the echoed variables from the javascript. The problem here is that, when i saved plain text into the database they were displayed but when i saved html codes into the database, the javascript refuses to display it.

I have tried several ways to echo the html string into the javascript such as:

 <?php echo json_encode($fbilling); ?> \\Accepts Only Plain Texts Not HTML Codes
'<?php echo decodeURI(rawurlencode($freply)); ?>' \\Refuses To load Page
'<?php echo htmlspecialchars_decode(rawurlencode($freply)); ?>'  \\Changes HTML Codes To Characters Like :%3Ctable%20data-module%3D%22header%22%20data
'<?php htmlspecialchars(json_encode($freply)); ?>'  \\ No Value is Displays, Just Empty
'<?php echo htmlspecialchars(json_encode($freply)); ?>' \\ No Value is Displays, Just Empty
'<?php echo htmlspecialchars(rawurlencode($freply)); ?>' \\Changes HTML Codes To Characters Like :%3Ctable%20data-module%3D%22header%22%20data

This is my code:

var Code = new Array("", <?php echo json_encode($fwelcome); ?>, <?php echo json_encode($fbilling); ?>, <?php echo json_encode($freply); ?>, <?php echo json_encode($fdelivery); ?>);

function change() {
  var ID = formconteudo.message.options[formconteudo.message.selectedIndex].value;
  CKEDITOR.instances.editor1.setData('<p>' + Code[ID] + '</p>');
}
<div class="col-md-4">
  <div class="form-group">
    <?php             
                        $sql3="SELECT * FROM email WHERE id='1'";
                        $result3=mysql_query($sql3) or die(mysql_error());
                        $rwsf=  mysql_fetch_array($result3);
                        $fwelcome= $rwsf[3]; 
                        $freply= $rwsf[2]; 
                        $fbilling= $rwsf[1]; 
                        $fdelivery= $rwsf[4]; 
                        ?>
    <label>Email Templates: </label>
    <select name="message" onChange="change();" class="form-control select2 required" style="width: 100%;">
      <option disabled selected>Click To Select</option>
      <option value="1">Welcome Email</option>
      <option value="2">Billing Email</option>
      <option value="3">Basic Reply Email</option>
      <option value="4">Delivery Process Email</option>
    </select>
  </div>
</div>
</div>


<div class="row">
  <div class="col-md-12">
    <div class="form-group">
      <input type="text" name="subject" placeholder="Subject" class="form-control required">
    </div>
  </div>
</div>
<textarea id="editor1" name="editor1" rows="10" cols="80">Select your mail design from the 'Email' drop down list above.</textarea><br>

<div class="form-wizard-buttons">
  <button type="submit" class="btn btn-success" style="border:0px"><i class="fa  fa-send"> Send Mail</i></button>
</div>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Daniel C.
  • 49
  • 1
  • 1
  • 6
  • try var Code = new Array("", "", "", "", ""); – Venkatesh Konatham Jun 22 '17 at 09:10
  • `var Code = ["", "", "", "", ""];` – mplungjan Jun 22 '17 at 09:15
  • @VenkateshKonatham i have tried this already, it does not respond each time i select an option. Nothing works nor displays. – Daniel C. Jun 22 '17 at 09:17
  • **Please** stop using the deprecated mysql_* API. Use mysqli_* or PDO. Then you can avoid the security concerns associated with this library and use parameterised queries to properly protect yourself against SQL injection attacks. Although the query shown here is not vulnerable, anything you write that relies on variables to complete the query will be wide open to hacking. Also mysql_ is removed entirely in PHP7 as well, so whenever you upgrade to this version your code will cease to work at all. – ADyson Jun 22 '17 at 09:18
  • Also : `function changeIt(sel) { CKEDITOR.instances.editor1.setData('

    ' + Code[sel.selectedIndex] + '

    '); }` using `onchange"=changeIt(this)`
    – mplungjan Jun 22 '17 at 09:20
  • so what you can do is first render this html into div tags and then retrieve them in java script. like
    and in java script get var Code = new Array("", "+$("#fbilling_content").html()+" ...... like this
    – Venkatesh Konatham Jun 22 '17 at 09:21
  • @VenkateshKonatham please can u showfull working, i would love to try this. – Daniel C. Jun 22 '17 at 09:30
  • @mplungjan Please idont understand all your explanation. – Daniel C. Jun 22 '17 at 09:36
  • @DanielC. please check my answer. – Venkatesh Konatham Jun 22 '17 at 09:38
  • Daniel: Quote your strings!!! – mplungjan Jun 22 '17 at 09:39

1 Answers1

0

Hi as you have html content, you can follow below process to get html and send it to the function as parameter.

    <div class="col-md-4">
      <div class="form-group">
        <?php             
                            $sql3="SELECT * FROM email WHERE id='1'";
                            $result3=mysql_query($sql3) or die(mysql_error());
                            $rwsf=  mysql_fetch_array($result3);
                            $fwelcome= $rwsf[3]; 
                            $freply= $rwsf[2]; 
                            $fbilling= $rwsf[1]; 
                            $fdelivery= $rwsf[4]; 
                            ?>
        <label>Email Templates: </label>
        <select name="message" onChange="change();" class="form-control select2 required" style="width: 100%;">
          <option disabled selected>Click To Select</option>
          <option value="1">Welcome Email</option>
          <option value="2">Billing Email</option>
          <option value="3">Basic Reply Email</option>
          <option value="4">Delivery Process Email</option>
        </select>
      </div>
    </div>
    </div>


    <div class="row">
      <div class="col-md-12">
        <div class="form-group">
          <input type="text" name="subject" placeholder="Subject" class="form-control required">
        </div>
      </div>
    </div>
    <textarea id="editor1" name="editor1" rows="10" cols="80">Select your mail design from the 'Email' drop down list above.</textarea><br>

    <div class="form-wizard-buttons">
      <button type="submit" class="btn btn-success" style="border:0px"><i class="fa  fa-send"> Send Mail</i></button>
    </div>
   <div name="fwelcome_content">
     <?php echo $fwelcome; ?>
   </div>
   <div name="fbilling_content">
     <?php echo $fbilling; ?>
   </div>
   <div name="freply_content">
     <?php echo $freply; ?>
   </div>    
   <div name="fdelivery_content">
     <?php echo $fdelivery; ?>
   </div>

and then in javascript

var Code = new Array();
window.onload = function(e){     
   Code = new Array("", getHTMLContent("fwelcome_content"), getHTMLContent("fbilling_content"), getHTMLContent("freply_content"), getHTMLContent("fdelivery_content"));
}
function change() {
  var ID = formconteudo.message.options[formconteudo.message.selectedIndex].value;
  CKEDITOR.instances.editor1.setData('<p>' + Code[ID] + '</p>');
}
function getHTMLContent(elementId){
  var htmlContent = document.getElementsByName(elementId)[0].innerHTML;
  document.getElementsByName(elementId)[0].remove();
  return htmlContent;
}
  • Why not `');` – mplungjan Jun 22 '17 at 09:44
  • see first understand what his issue is, that he is facing issue while sending html to new Array so i made changes over there – Venkatesh Konatham Jun 22 '17 at 09:48
  • Bro i copied your source and replaced it with mine but it stopped the page from loading. – Daniel C. Jun 22 '17 at 09:49
  • did you seen any errors in debug logs? there may be typo issues – Venkatesh Konatham Jun 22 '17 at 09:50
  • @DanielC. I have just updated my answer and tested it on my local. its working as expected, before the issues is that elements are not getting loaded. So moved some code in to on load to make sure we get html after page load. I am sorry about this – Venkatesh Konatham Jun 22 '17 at 10:28
  • @VenkateshKonatham Bro you've really tried in helping me out, but i still copied the piece of code into my page and it still couldnt load it. Do u have any knowledge about CKEditor, i think the issue might be from there. Or do u mind if i share my soure code with u? – Daniel C. Jun 22 '17 at 16:29
  • See the [url](http://thai.cheapusedcarsturkey.com/server-side/custom_mails) – Daniel C. Jun 22 '17 at 16:35
  • Hey bro its asking for login, its my pleasure to help you :) you can share source code and login details to konathamvenkatesh@gmail.com – Venkatesh Konatham Jun 22 '17 at 16:46