Author Topic: Get content from div, post it to database  (Read 5190 times)

0 Members and 1 Guest are viewing this topic.

Offline pshoegTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Get content from div, post it to database
« on: March 17, 2010, 07:18:33 AM »
Hi there,

Say I have this code:

Code: [Select]
<div id="name>Lorem ipsum dolor sit amet.</div>
I want to take the text inside the div and put it into a table in a database, in the same manner you would post content from a form to a database. Is that possible?

And the follow-up question to that: How is it possible?

Offline Kiwii

  • Irregular
  • Posts: 8
    • View Profile
Re: Get content from div, post it to database
« Reply #1 on: March 17, 2010, 07:24:18 AM »
Why would you want to? Surely you'll be typing that data into the div so why not just type it into the database?

Offline pshoegTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Re: Get content from div, post it to database
« Reply #2 on: March 17, 2010, 07:27:09 AM »
I want to use it along with HTML5's new contentEditable feature - so I wouldn't be entering it into the div before I'm in the browser.

Offline japesu

  • Irregular
  • Posts: 7
    • View Profile
Re: Get content from div, post it to database
« Reply #3 on: March 17, 2010, 07:29:18 AM »
It's possible ofc. You could do it for example parsing the html code using preg_match (get the html file and loop through it row by row searhing for matches).

Offline pshoegTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Re: Get content from div, post it to database
« Reply #4 on: March 17, 2010, 07:39:07 AM »
So I wouldn't be able to just say that the text in a table in the database should be equal to the text in the div?

Offline Kiwii

  • Irregular
  • Posts: 8
    • View Profile
Re: Get content from div, post it to database
« Reply #5 on: March 17, 2010, 07:50:01 AM »
If you just want to display the data from your database into your div tags then

Code: [Select]
$result = mysql_query(" SELECT * FROM example ")
or die(mysql_error()); 

$row = mysql_fetch_array($result);

echo "<div id='name'>".$row['text']."</div>";

Offline pshoegTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Re: Get content from div, post it to database
« Reply #6 on: March 17, 2010, 07:50:59 AM »
No, I want the opposite - I want to take the data from my div tags and put it in a database.

If you just want to display the data from your database into your div tags then

Code: [Select]
$result = mysql_query(" SELECT * FROM example ")
or die(mysql_error()); 

$row = mysql_fetch_array($result);

echo "<div id='name'>".$row['text']."</div>";

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Get content from div, post it to database
« Reply #7 on: March 17, 2010, 07:56:18 AM »
You need to explain what it is your wanting to do in more detail by the sound of it. I moved the thread here because it sounded like you wanted to parse some data from a remote site.

If you just want to grap some content from one of your own pages and send it to a php script which will place it within a database you could easily achieve that with a litle ajax. I use jQuery as ena example....

Code: [Select]
var content = $('#name').html();
$.ajax({
  url: 'processingscript.php,
  type: 'POST',
  data: {
    content: content
  }
});

Offline pshoegTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Re: Get content from div, post it to database
« Reply #8 on: March 17, 2010, 07:58:49 AM »
All right, I'll try to explain it in more detail:

I am going to use localStorage along with HTML5's new contentEditable feature to create a website that is editable directly from the browser. The localStorage should save what is written in a div with contentEditable=true on the client's computer. This is working:
Code: [Select]
<!DOCTYPE html>
<html>
<head>
<title>LocalStorage</title>
<script>
var addEvent = (function () {
if (document.addEventListener) {
return function (el, type, fn) {
if (el && el.nodeName || el === window) {
el.addEventListener(type, fn, false);
} else if (el && el.length) {
for (var i = 0; i < el.length; i++) {
addEvent(el[i], type, fn);
}
}
};
} else {
return function (el, type, fn) {
if (el && el.nodeName || el === window) {
el.attachEvent('on' + type, function () { return fn.call(el, window.event); });
} else if (el && el.length) {
for (var i = 0; i < el.length; i++) {
addEvent(el[i], type, fn);
}
}
};
}
})();

</script>
</head>
<body>
<div id="editable" contentEditable="true">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>

<script>
var editable = document.getElementById('editable');

addEvent(editable, 'blur', function () {
  // lame that we're hooking the blur event
  localStorage.setItem('contenteditable', this.innerHTML);
  document.designMode = 'off';
});

addEvent(editable, 'focus', function () {
  document.designMode = 'on';
});

addEvent(document.getElementById('clear'), 'click', function () {
  localStorage.clear();
  window.location = window.location; // refresh
});

if (localStorage.getItem('contenteditable')) {
  editable.innerHTML = localStorage.getItem('contenteditable');
}
</script>

</body>
</html>

However, I'd like that when the client hits a 'Save' button or something, all the stuff from the localStorage gets sent to a table in a MySQL database.

How would you do that?

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Get content from div, post it to database
« Reply #9 on: March 17, 2010, 08:00:52 AM »
Quote
However, I'd like that when the client hits a 'Save' button or something, all the stuff from the localStorage gets sent to a table in a MySQL database.

How would you do that?

See my example above. It uses the jQuery library to grab the data from the div and send it to a php script which would then add it to the database.

Offline pshoegTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Re: Get content from div, post it to database
« Reply #10 on: March 17, 2010, 08:08:25 AM »
See my example above. It uses the jQuery library to grab the data from the div and send it to a php script which would then add it to the database.

Thanks, I'll get back to you if I can't make it work...

Cheers!

Offline pshoegTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Re: Get content from div, post it to database
« Reply #11 on: March 17, 2010, 08:14:49 AM »
I'm back ;)

All right, so how do I send the content? Do I create an input button inside a form element (I guess I'm not, as the jQuery script already tells the URL of the PHP file and that it should POST)?

Can I use a regular link to send it off?

And should the PHP file contain anything else that the database connection stuff and the code for putting the content in the tables?

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Get content from div, post it to database
« Reply #12 on: March 17, 2010, 08:43:25 AM »
Quote
Can I use a regular link to send it off?

You could execute that piece of code at the trigger of any event you like.

Quote
And should the PHP file contain anything else that the database connection stuff and the code for putting the content in the tables?

The php script acts just the same as it would if you where posting data to it via a form.

Offline pshoegTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Re: Get content from div, post it to database
« Reply #13 on: March 17, 2010, 09:42:36 AM »
So, this is what I got (not working):

The saveToDb.php file:
Code: [Select]
<?php
$con 
mysql_connect("localhost","root","root");
if (!
$con)
  {
  die(
'Could not connect: ' mysql_error());
  }

mysql_select_db("test"$con);

$sql="INSERT INTO Content (main)
VALUES
('
$_POST[content]')";

if (!
mysql_query($sql,$con))
  {
  die(
'Error: ' mysql_error());
  }
echo 
"1 record added";

mysql_close($con)
?>

The script that you provided me with:
Code: [Select]
<script>
var content = $('#editable').html();
$.ajax({
url: 'saveToDb.php',
type: 'POST',
data: {
content: content
}
});
</script>

The div and the button:
Code: [Select]
<div id="editable" contentEditable="true">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>

<input type="button" value="Save" name="save" onclick="$.ajax">

When I click the button, it sends 'null' to the table. What am I doing wrong?

Offline isedeasy

  • Enthusiast
  • Posts: 248
    • View Profile
Re: Get content from div, post it to database
« Reply #14 on: March 17, 2010, 01:36:40 PM »
Code: [Select]
<script>
$(document).ready(function() {
    $('#save_editable').click(function() {
        var content = $('#editable').html();
        $.ajax({
            url: 'saveToDb.php',
            type: 'POST',
            data: {
                content: content
            }
        });
    });
});
</script>

<div id="editable" contentEditable="true">Lorem ipsum dolor ....</div>
<a id="save_editable">Save</a>

« Last Edit: March 17, 2010, 01:38:17 PM by isedeasy »
I <3 Memcache

Offline pshoegTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Re: Get content from div, post it to database
« Reply #15 on: March 17, 2010, 02:49:18 PM »
Code: [Select]
<script>
$(document).ready(function() {
    $('#save_editable').click(function() {
        var content = $('#editable').html();
        $.ajax({
            url: 'saveToDb.php',
            type: 'POST',
            data: {
                content: content
            }
        });
    });
});
</script>

<div id="editable" contentEditable="true">Lorem ipsum dolor ....</div>
<a id="save_editable">Save</a>


This works perfectly! Thank you so much!