Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2009
    Location
    Manipal
    Posts
    45
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Ajax js php problem

    I have a todo table which has two columns TODO and Date . There is a option to delete and add . I have used onclick funtions to asynchronously add or delete data from the database . But , the result is not being showed , I have to refresh the page to see the result
    Please help
    This is the JS function

    Code:
    <script type="text/javascript">
    var x=false;
    if(window.XMLHttpRequest)
    x=new XMLHttpRequest();
    else if(window.ActiveXObject)
    x= new ActiveXObject("Microsoft.XMLHTTP");
    function add(adate,atodo)
    {
    	if(adate==""||atodo=="")
    	{
    		alert("Its Empty , sorry cant add");
    		
    	}
    	else
    	{
    			alert("Date : "+adate +" and Todo : "+atodo +" is being added");
    			if (x)
    	{
    		var url="add.php";
    		url=url+"?date="+adate+"&todo="+atodo;
    		x.open("GET",url);
    		x.onreadystatechange=function()
    		{
    			if(x.readyState==4 && x.status==200)
    			{
    				document.getElementById("add").innerHTML=x.responseText;
    			}
    			
    		}
    		x.send(null);
    		
    		}
    		
    	}
    	
    }
    	
    function del(ddate,dtodo)
    {
    	var r=confirm("Are you sure you want to delete Date : " +ddate +" and Todo : "+ dtodo );
    	if(r==true)
    	{
    		if (x)
    	{
    		var url="delete.php";
    		url=url+"?date="+ddate+"&todo="+dtodo;
    		x.open("GET",url);
    		x.onreadystatechange=function()
    		{
    			if(x.readyState==4 && x.status==200)
    			{
    				document.getElementById("delete").innerHTML=x.responseText;
    			}
    			
    		}
    		x.send(null);
    		
    	}
    	}
    	
    	
    }
    This is where the onclick is present

    The delete :

    PHP Code:
    <input type="button"  name="delete"  value="Delete" onClick="del('<?php echo $row['Date'];?>','<?php echo  $row['Todo']; ?>')" />
    The add
    PHP Code:
    <td><input type="button" value="Add" name="submit" onClick="add(date.value,todo.value)">
    </
    td
    This is the add.php
    PHP Code:
    <?php
    session_start
    ();

        include 
    '../includes/mysqlconnect.php';

    if(!empty(
    $_GET['date'])||!empty($_GET['todo']))
    {
        
    $sql="INSERT INTO todo (Date,Todo,user) VALUES('".$_GET['date']."',
    '"
    .$_GET['todo']."','".$_SESSION['user']."')" ;

    $result=mysql_query($sql) or die(mysql_error());

    echo 
    "SUCCESSFULLY ADDED " ;
    }

    ?>

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    You mean it doesn't show the message "SUCCESSFULLY ADDED"? That's the only response you're sending back from the backside handler.

    In your javascript, you have this line:
    Code:
    document.getElementById("add").innerHTML=x.responseText;
    Where's the element with the ID "add"? I don't see it in you code above.
    Are you a Help Vampire?

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by kar2905 View Post
    I have a todo table which has two columns TODO and Date . There is a option to delete and add . I have used onclick funtions to asynchronously add or delete data from the database . But , the result is not being showed , I have to refresh the page to see the result
    What else do you expect? As it is, your AJAX application should delete some columns from data base, not from your web document. And it does that. But it does not suppose to return the new configuration from the DB, does it? If you need that, you must continue your query with a mySQL SELECT, get the new data from DB, return the data (as a string or, better, as a JSON object) in responseText. Now you must create and call another javascript function which will change the document according to the new data response (with innerHTML or, better, using DOM methods: createElement(), setAttribute(), removeChild() ... and so on).
    Last edited by Kor; 06-27-2009 at 02:36 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    kar2905 (06-27-2009)

  • #4
    New Coder
    Join Date
    Jun 2009
    Location
    Manipal
    Posts
    45
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by tomws View Post
    You mean it doesn't show the message "SUCCESSFULLY ADDED"? That's the only response you're sending back from the backside handler.

    In your javascript, you have this line:
    Code:
    document.getElementById("add").innerHTML=x.responseText;
    Where's the element with the ID "add"? I don't see it in you code above.
    I am getting the message SUCCESSFULLY ADDED . Thats not the problem . The element "add" is not visible because I have pasted only bits of codes .

    I want to see the final result . So how do I do it ??

    @Kor

    Thanks for the answer . I have never used JSON or DOM but I think I can do it using PHP .

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by kar2905 View Post
    I have never used JSON or DOM but I think I can do it using PHP .
    No, you can not with php, unless you quit AJAX and use a classical server-side solution.

    Your problem looks like that: You need to send and receive data to and from a data base within the same query, then to reflect immediately those changes on the document. You have 2 possibilities: on changing the client session (a pure server-side solution - I think you know that way: by auto-submitting) and without changing the client session (a combined AJAX + server-side solution).

    Your thread here uses the second solution. In this case you may use the php application (your add.php file) only as an informational node, as a data filter between the document and the data base. At the client-side part of the job, you can use nothing but javascript to display data on the document, if you want to remain within the same session (client-session).
    Last edited by Kor; 06-28-2009 at 06:16 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    kar2905 (06-28-2009)

  • #6
    New Coder
    Join Date
    Jun 2009
    Location
    Manipal
    Posts
    45
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Kor View Post
    No, you can not with php, unless you quit AJAX and use a classical server-side solution.

    Your problem looks like that: You need to send and receive data to and from a data base within the same query, then to reflect immediately those changes on the document. You have 2 possibilities: on changing the client session (a pure server-side solution - I think you know that way: by auto-submitting) and without changing the client session (a combined AJAX + server-side solution).

    Your thread here uses the second solution. In this case you may use the php application (your add.php file) only as an informational node, as a data filter between the document and the data base. At the client-side part of the job, you can use nothing but javascript to display data on the document, if you want to remain within the same session (client-session).
    I was thinking that I could receive data from the database in the add.php and then echo it to the main file and display through JS ..

    Could you elaborate on the first solution ??

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by kar2905 View Post

    Could you elaborate on the first solution ??
    The first solution? I guess that there is nothing to elaborate. Haven't you use the form's submit action so far?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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