I'm just trying to learn a few handy AJAX things, so I'm not sure quite how well I'll explain this. I apologise in advance if I go into too much or too little detail.
I apologise as well if this is a simple thing - I'm hoping that learning the nuts and bolts of it here will prevent me from having to ask it again.
Basically I found an example of editing within a page that I thought was really good (example is here). It did mention that it would have no way of saving the end result, so I tinkered with the suggested edit.php file.
I am using the code in a private notepad page. Individual notepads are saved in a MySql database and get called in the url, i.e. /editnotepad.php?notepadid=2. The only problem was that it wasn't passing over the $notepadid to the edit.php page, so I had to cheat.
Here are the full codes.
editnotepad.php is parsed to this:
edit.php is this (spot the blatantly obvious cheat):HTML Code:<script src="ajax/prototype.js" type="text/javascript"></script> <script src="ajax/editinplace.js" type="text/javascript"></script> <h1>Test notepad</h1> <p id="desc">This is a test.
editinplace.js is this:PHP Code:<?php
$id = $_POST['id'];
$content = $_POST['content'];
$notepadid = $_POST['notepadid'];
echo htmlspecialchars($content);
include("$DOCUMENT_ROOT/project/config/dbdetails.inc");
// Establish connection since we are going to use the database throughout page
$dbLink = mysql_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD);
if(!$dbLink) {
print "Unable to connect to the database, please contact Sysadmin asap.";
} else {
$dbUse = mysql_select_db(DATABASE_NAME, $dbLink);
}
// following section for testing purposes
if (!$notepadid){
// set $notepadid if not already set
$notepadid = 2;
// end test section
}
$result = mysql_query("UPDATE notepads SET `notes` = '$content' WHERE id = '$notepadid'");
if (mysql_errno()>0) {
echo "<BR>\n<FONT COLOR=\"#990000\">".mysql_errno().": ".mysql_error()."<BR>\n";
exit;
}
mysql_close($dbLink);
?>
The amendment I attempted to make to editinplace.js was this:Code:Event.observe(window, 'load', init, false); function init(){ makeEditable('desc'); makeEditable('pizza'); } function makeEditable(id){ Event.observe(id, 'click', function(){edit($(id))}, false); Event.observe(id, 'mouseover', function(){showAsEditable($(id))}, false); Event.observe(id, 'mouseout', function(){showAsEditable($(id), true)}, false); } function edit(obj){ Element.hide(obj); var textarea = '<div id="'+obj.id+'_editor"><textarea id="'+obj.id+'_edit" name="'+obj.id+'" rows="4" cols="60">'+obj.innerHTML+'</textarea>'; var button = '<div><input id="'+obj.id+'_save" type="button" value="SAVE" /> OR <input id="'+obj.id+'_cancel" type="button" value="CANCEL" /></div></div>'; new Insertion.After(obj, textarea+button); Event.observe(obj.id+'_save', 'click', function(){saveChanges(obj)}, false); Event.observe(obj.id+'_cancel', 'click', function(){cleanUp(obj)}, false); } function showAsEditable(obj, clear){ if (!clear){ Element.addClassName(obj, 'editable'); }else{ Element.removeClassName(obj, 'editable'); } } function saveChanges(obj){ var new_content = escape($F(obj.id+'_edit')); obj.innerHTML = "Saving..."; cleanUp(obj, true); var success = function(t){editComplete(t, obj);} var failure = function(t){editFailed(t, obj);} var url = 'edit.php'; var pars = 'id='+obj.id+'&content='+new_content+; var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure}); } function cleanUp(obj, keepEditable){ Element.remove(obj.id+'_editor'); Element.show(obj); if (!keepEditable) showAsEditable(obj, true); } function editComplete(t, obj){ obj.innerHTML = t.responseText; showAsEditable(obj, true); } function editFailed(t, obj){ obj.innerHTML = 'Sorry, the update failed.'; cleanUp(obj); }
No luck when I took out the test section from edit.php though.Code:var pars = 'id='+obj.id+'&content='+new_content+'¬epadid='+;
So how should I go about passing $notepadid through to edit.php?
P.S. I know that there are certain pages available via Google which mention about passing a PHP variable to Javascript, but I wasn't sure if passing these onto separate pages made matters more complicated than that.





Bookmarks