Forum Moderators: open
I've almost got everything working, drag and drop, entering new task items via Ajax.Request, and edit-it-place. The problem is that whenever I delete a record or add a new entry my edit-in-place and sortable stop working. I'm fairly sure that the problem is how I'm showing the updated data after server has completed the operation with my php script. If anyone could point me where I'm wrong in my thinking I'd be greatly appreciative:
Here is the javascript for sending new records to the server
//ajax for entering new notes entry
function createProject(proj_desc, proj_title) {
var pars = 'proj_title=' + $(proj_title).value + '&proj_desc=' + $(proj_desc).value;
var myAjax = new Ajax.Request('process.php', {
method: 'post',
parameters: pars,
onComplete: showProjectResponse
});
$(proj_desc).value = "";
$(proj_title).value = "";
$(proj_title).focus();
}
Relevant code from class in process.php
function procCreateProject() {
global $database, $session, $projects, $form;
//get/set vars
$proj_title = $_POST['proj_title'];
$proj_desc = $_POST['proj_desc'];
$username = $session->username;
$start_date = time();
$return_msg = ""; //initialize return var
//Make sure title is not blank
$field = "test";
if(!$proj_title ¦¦ strlen($proj_title = trim($proj_title)) == 0 ¦¦ $proj_title === ""){
$return_msg .= "<div id='error'><p><font size=\"2\" color=\"#ff0000\">* ATTENTION: Please "
."enter a title for your project.</font></p></div>";
echo $return_msg;
exit;
}
//Return if form errors exist
if($form->num_errors > 0){
return false;
}
//add record into db
$q = "INSERT INTO projects (project_title, project_desc, username, start_date, bool_active) VALUES "
."('$proj_title', '$proj_desc', '$username', '$start_date', 'y')";
mysql_query($q, $database->connection) or die(mysql_error());
echo $projects->currentProjects();
}
Relevant code from project.class. I think this is probably the problem. I fresh the entire task list instead of the new task only. I'm to brain dead to figure out how to find a good way of doing this.
function currentProjects() {
global $database, $session;
$q = "SELECT * FROM projects ORDER By sort_id,lower(project_id)";
$result = mysql_query($q);
$return_var = "<ul class='project_list' id='ul_project_parent'>";
while($row = mysql_fetch_assoc($result)) {
$return_var .= '<li id="project_'.$row['project_id'].'">';
$return_var .= "<a onclick=\"if (confirm('Are you sure you want to "
."delete this todo item?')) { projectDelete(".$row['project_id']
."); }\" >";
$return_var .= '<img src="images/delete_me.gif" alt="delete" '
.'class="delete_me" id="img_'.$row['project_id']
.'"/></a><input type="checkbox" /> <strong>'.$row['project_title'].'</strong>';
if(!$row['project_desc'] == "") {
$return_var .= " - ".$row['project_desc']."</li>";
}
}
$return_var .= "</ul>";
echo $return_var;
}
I am still having problems with my sortable list though. Here is what I'm currently doing:
1. create a sortable list when the page loads.
2. I then add a new record to that list and have it inserted into the web page via javascript/DOM from the server's response.
3. All the items in my list that were setup to be sortable when the page loaded are still sortable. However any new item in my list are not.
How does one go about fixing this? Am I taking the wrong approach?
Here's is what I think I could have done wrong. First, I'm trying hard to break the whole procedural mind set that I'm used in my past scripts. I'm a networking guy who writes scripts to save time an not a programmer. This said I think maybe I probably shouldn't have my classes echoing anything directly? Or is it that I'm having it echo and setting other declared classes as global? If someone can at least point me to the path of secure coding I'd be grateful.
Regardless, I have come to realize that even though I'm using the syntax for oop that php provides I'm have still used it in a very procedural way. It's more like I'm using classes for the sake of holding functions only. :P