简体   繁体   English

如何使用php echo变量将大字符串的html解析为Javascript

[英]How to parse large strings of html into Javascript with php echo variable

I have a javascript function that displays html code that is stored in the mysql database into CKeditor <textarea> . 我有一个javascript函数,可将存储在mysql数据库中的html代码显示到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. 当用户选择任何选项时,mysql查询旨在将html代码块作为php变量回显到javascript中。

The select option works really fine in selecting the echoed variables from the javascript. select选项在从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. 这里的问题是,当我将纯文本保存到数据库中时会显示它们,但是当我将html代码保存到数据库中时,javascript拒绝显示它。

I have tried several ways to echo the html string into the javascript such as: 我尝试了几种将html字符串回显到javascript中的方法,例如:

 <?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>

Hi as you have html content, you can follow below process to get html and send it to the function as parameter. 嗨,因为您有html内容,您可以按照以下过程获取html并将其作为参数发送给函数。

    <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 然后在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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM