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

时间:2017-06-22 09:06:31

标签: javascript php html echo

我有一个javascript函数,它将存储在mysql数据库中的html代码显示在CKeditor <textarea>中。当用户选择任何选项时,mysql查询意味着将html代码块作为php变量回显到javascript中。

select选项在从javascript中选择回显变量时效果很好。这里的问题是,当我将纯文本保存到数据库中时,它们被显示但是当我将html代码保存到数据库中时,javascript拒绝显示它。

我已经尝试了几种方法将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

这是我的代码:

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>

1 个答案:

答案 0 :(得分:0)

嗨,因为你有html内容,你可以按照以下流程获取html并将其作为参数发送到函数。

 try {
        PrintWriter arq = new PrintWriter(jTextField1.getText()+".txt");
        arq.println("Username: " + jTextField1.getText());
        arq.println("Email: " + jTextField2.getText());




        String algorithm = "SHA";

        byte[] plainText = jPasswordField1.getText().getBytes();

    MessageDigest md = null;

    try {       
        md = MessageDigest.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
    }

    md.reset();     
    md.update(plainText);
    byte[] encodedPassword = md.digest();

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < encodedPassword.length; i++) {
        if ((encodedPassword[i] & 0xff) < 0x10) {
            sb.append("0");
        }

        sb.append(Long.toString(encodedPassword[i] & 0xff, 16));
    }

        arq.println("Password: " + sb.toString());

        arq.close();
        if(!jTextField2.getText().equals(jTextField3.getText()) 
   ||!jPasswordField1.getText().equals(jPasswordField2.getText())){
            JOptionPane.showMessageDialog(null, "Either your email or 
   password are not corresponding. Please fix the issue.");
        }
        else{
            JOptionPane.showMessageDialog(null, "Account created!");
        }

    } catch (HeadlessException | FileNotFoundException erro) {
        JOptionPane.showMessageDialog(null, "Error creating Account. Please 
   try again.");
    }

然后在javascript中

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