0
var editing  = false;

if (document.getElementById && document.createElement) {
    var butt = document.createElement('BUTTON');
    var buttext = document.createTextNode('Ready!');
    butt.appendChild(buttext);
    butt.onclick = saveEdit;
}

function catchIt(e) {
    if (editing) return;
    if (!document.getElementById || !document.createElement) return;
    if (!e) var obj = window.event.srcElement;
    else var obj = e.target;
    while (obj.nodeType != 1) {
        obj = obj.parentNode;
    }
    if (obj.tagName == 'TEXTAREA' || obj.tagName == 'A') return;
    while (obj.nodeName != 'P' && obj.nodeName != 'HTML') {
        obj = obj.parentNode;
    }
    if (obj.nodeName == 'HTML') return;
    var x = obj.innerHTML;
    var y = document.createElement('TEXTAREA');
    var z = obj.parentNode;
    z.insertBefore(y,obj);
    z.insertBefore(butt,obj);
    z.removeChild(obj);
    y.value = x;
    y.focus();
    editing = true;
}

function saveEdit() {
    var area = document.getElementsByTagName('TEXTAREA')[0];
    var y = document.createElement('P');
    var z = area.parentNode;
    y.innerHTML = area.value;
    z.insertBefore(y,area);
    z.removeChild(area);
    z.removeChild(document.getElementsByTagName('button')[0]);
    editing = false;
}

document.onclick = catchIt;

This code is a quick edit and I want to add a PHP script that will UPDATE my database base on the changes on the text.

4 Answers 4

7

I suggest studying AJAX.

There are also JavaScript libraries out there that make AJAX easy to use. jQuery is one.

Sign up to request clarification or add additional context in comments.

Comments

1

You'll need to use Ajax for that.

Comments

1

JavaScript works on the client side, so it is not possible to add a PHP script, however you can use AJAX for this purpose, from which you can interact with your database.

For this you would have to make a separate PHP script that you can call from your JavaScript function without reloading the entire page.

Comments

0

You may want to use AJAX, yes, as said above. I'm going to tell you, it's a rough trip to learn. But it can be made pretty easy to use if you're willing to learn a small bit of commands. Pipes is what I use. I am completely sure it will work for you. Here's an example:

post_html = "<b headers='method:post;' ajax='db.php' insert='success' query='addName=" + obj.name + "'>Click me to add Your Name to the DB!</b>";
post_html += "<h1 id='success'></h1>";

The clickable HTML is the tag and the second line of HTML will get the "echo" or whatever print format you're using from db.php. That's simply Pipes!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.