1

I have this code to show some html from a db :

$("#menuOverlay").append('<?php include('aggiungiPaginaComp.php');?>');

It's working, the source file from the browser :

$("#menuOverlay").append('<form id="aggiungiPagina"
name="aggiungiPagina"
action="script/aggiungiPagina.php"
method="post">

<tablestyle="width:100%;">
    <tr>
        <td>NomePagina</td>
        <td><inputsize="30"
            maxlength="30"
            placeholder="Inseriscinomepagina"
            autofocus
            required
            type="text"
            name="nomePagina"
            id="nomePagina"></td>
    </tr>
    <tr>
        <td>Descrizione</td>
        <td><inputsize="50"
            maxlength="200"
            placeholder="Inseriscidescrizione"
            required
            type="text"
            name="descrizione"
            id="descrizione"></td>
    </tr>

</table>
<divid="scrisciaBtnForm">
    <inputclass="btnMenu"type="submit"value="invia"/>
</div>
</form>');

But the div #menuOverlay is blank.. with :

$("#menuOverlay").append('<Button> ciao </Button>');

works, why? sorry for bad english.

Johannes Jander
  • 4,974
  • 2
  • 31
  • 46
Alessandro Zago
  • 793
  • 3
  • 12
  • 33

3 Answers3

0

For once, you have a lot of invalid HTML, things like <tablestyle="width:100%;"> - I assume that somehow you have lost a lot of spaces in tags (maybe due to a RegEx?) which destroys the HTML you are trying to insert.

And you can't just have multiline strings in JS. You need to either append a \ at every newline or replace \n to make it a string concatenation:

$("#menuOverlay").append('<form id="aggiungiPagina" '+
'name="aggiungiPagina" '+
'action="script/aggiungiPagina.php" '+
'method="post"> '+
' '+
'<table style="width:100%;"> '+
'    <tr> '+
'        <td>NomePagina</td> '+
'        <td><input size="30" '+
'            maxlength="30" '+
'            placeholder="Inseriscinomepagina" '+
'            autofocus '+
'            required '+
'            type="text" '+
'            name="nomePagina" '+
'            id="nomePagina"></td> '+
'    </tr> '+
'    <tr> '+
'        <td>Descrizione</td> '+
'        <td><input size="50" '+
'            maxlength="200" '+
'            placeholder="Inseriscidescrizione" '+
'            required '+
'            type="text" '+
'            name="descrizione" '+
'            id="descrizione"></td> '+
'    </tr> '+
' '+
'</table> '+
'<div id="scrisciaBtnForm"> '+
'    <input class="btnMenu" type="submit" value="invia"/> '+
'</div> '+
'</form>');

See here: https://jsbin.com/vidajohevi/edit?html,js,output

Johannes Jander
  • 4,974
  • 2
  • 31
  • 46
0

You should HTML encode the page in PHP (so your double quotes wont cause errors) and then html decode it in JS. If your using jQuery this should work:

String.prototype.htmlDecode = function(){
  return $('<div/>').html(this).text();
};

$("#menuOverlay").append("<?php
    ob_start();
    include('aggiungiPaginaComp.php');
    $page = ob_get_clean();
    echo htmlentities($page);
?>".htmlDecode());
Dustin Poissant
  • 3,201
  • 1
  • 20
  • 32
0

This:

 <tablestyle="width:100%;"> 

should be:

<table style="width:100%;">

Similarly, this:

<divid="scrisciaBtnForm">

should be:

<div id="scrisciaBtnForm">

Lastly, remove all new lines in aggiungiPaginaComp.php. Javascript is going to have a problem with new lines when you use it in this manner.

Tim Hysniu
  • 1,446
  • 13
  • 24