SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict jamus's Avatar
    Join Date
    Jul 2004
    Location
    Devon, UK
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Jquery posting to MySQL via php - help

    Hi,

    I have a 'to do' item in a page that has an ID attached to it. These items are stored in a database. When clicked I want to reverse the 'state' of the item (active to complete or complete to active) by updating the database using jquery & PHP.

    I think I almost have it. As all the values are collected ok and if I change the the page that writes to the database to GET and pass the data through the URL it updates the database fine.

    Where am I going wrong?

    PHP Code:
    /*//////   My items HTML /////////*/
    <li id="todosArray_<?php echo $row['todo_id']; ?>" class="todo <?php echo $row['statename']; ?>"><a href"" id="<?php echo $row['todo_id']; ?>"><?php echo $row['text'" - " $row['todo_id']; ?></a></li>
    Code:
    /*////// my JQUERY function
    $('.todo a').click(function() {
    								
    		// get toto id 
    		var todoID     		= $(this).attr("id");  
    		 alert(todoID);
    		 
    			  if($(this).parent().hasClass('active')){
    				alert("active");
    				var todoState = '1';
    			  }
    			  
    			   if($(this).parent().hasClass('complete')){
    				alert("complete");
    				var todoState = '2';
    			  }
     			
    				$.ajax({  
    					type: "POST",  
    					url: "functions/todos-state.php",  
    					data: "todoID="+todoID+"&todoState="+todoState,  
    					success:function(data_response){
    						alert(data_response);
    						alert(todoID);
    						alert(todoState);
    }  
    				});  
    			return false;  
    			  
        });
    PHP Code:
    /*///// my todos-state.php

    $todoID  = htmlspecialchars(trim($_POST["todoID"]));
    $todoState = htmlspecialchars(trim($_POST["todoState"]));
     
     $query = "UPDATE todos SET state_id = " . $todoState . " WHERE id = " . $todoID . "";
            mysql_query($query) or die('Error, insert query failed'); 

  2. #2
    SitePoint Addict jamus's Avatar
    Join Date
    Jul 2004
    Location
    Devon, UK
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure what happened to my PHP code for the MySQL update but here it is...

    PHP Code:
     $todoID  htmlspecialchars(trim($_POST["todoID"]));
        
    $todoState htmlspecialchars(trim($_POST["todoState"]));
     
      
            
    $query "UPDATE todos SET state_id = " $todoState " WHERE id = " $todoID "";
            
    mysql_query($query) or die('Error, insert query failed'); 

  3. #3
    SitePoint Addict jamus's Avatar
    Join Date
    Jul 2004
    Location
    Devon, UK
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I now have this working so if anyone is interested let me know.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •