I am using GrapesJS to build a simple webpage.
I included the script in the following way inside head
part :
<script type="text/javascript" src="js/jquery-1.10.2.js"></script>
<link rel="stylesheet" type="text/css" href="grapesjs-dev/dist/css/grapes.min.css">
<script type="text/javascript" src="grapesjs-dev/dist/grapes.min.js"></script>
HTML:
<div id="gjs" style="height:0px; overflow:hidden;">
</div>
Javascript :
<script>
var editor = grapesjs.init({
showOffsets: 1,
noticeOnUnload: 0,
container: '#gjs',
fromElement: true,
height: '100%',
fromElement: true,
storageManager: { autoload: 0 },
assetManager: {
assets: [
'http://placehold.it/350x250/78c5d6/fff/image1.jpg',
// Pass an object with your properties
{
type: 'image',
src: 'http://placehold.it/350x250/459ba8/fff/image2.jpg',
height: 350,
width: 250
},
{
// As the 'image' is the base type of assets, omitting it will
// be set as `image` by default
src: 'http://placehold.it/350x250/79c267/fff/image3.jpg',
height: 350,
width: 250
},
],
},
storageManager: {
type: 'remote',
stepsBeforeSave: 1,
autosave: true, // Store data automatically
autoload: true,
urlStore: 'save_now.php',
urlLoad: 'load_now.php',
// ContentType: 'application/json',
// For custom parameters/headers on requests
//params: { _some_token: '....' },
contentTypeJson: true,
storeComponents: true,
storeStyles: true,
storeHtml: true,
storeCss: true,
headers: {
'Content-Type': 'application/json'
},
json_encode:{
"gjs-html": [],
"gjs-css": [],
}
//headers: { Authorization: 'Basic ...' },
}
});
window.editor= editor;
var blockManager = editor.BlockManager;
// 'my-first-block' is the ID of the block
blockManager.add('my-first-block', {
label: 'Simple block',
content: '<div class="my-block">This is a simple block</div>',
});
</script>
So I get in the blocks panel a block namely Simple block
which I can drag and drop on the editor. When ever any change is made then the autosave
is trigerred with an ajax call to save.php
file. Inside save.php
, I have:
$content_found="";
$content_found= file_get_contents('php://input');
mysqli_real_escape_string($conn, $content_found);
echo " content found = ".$content_found;
$sql = "INSERT INTO `grapes_content` (`content_found`)
VALUES ('".$content_found."')";
But in Chrome developer tool network tab, I can see :
It is not clear what payload variables I should save in database and how . I used $content_found= file_get_contents('php://input');
to get the full content instead.
After saving it into DB, on page refresh, I load the page with load_now.php
. Inside load_now.php
, I have :
$content_found="";
$sql = "SELECT * FROM `grapes_content`";
$result=$conn->query($sql);
$content_found="";
while($row=mysqli_fetch_assoc($result)){
$content_found=$row['content_found'];
}
echo $content_found;
But the editor shows no saved data.
I am pretty sure that the way I save and retrieve data is not correct.
So points are:
Q1) What things should I save in database ? And how can I get the variables or data from ajax payload or in any other way ?
Q2) How can I show the saved data into the editor after page reload ?
In the editor, I see a preview option with an eye image that can show me the HTML page without any editor.
Q3) After saving data into database, how can I show the data simply just as a HTML page and not inside any editor ?
See Question&Answers more detail:
os