SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php update database with ajax

    Hello,

    I want to help with my code.i am just not able to find out that is it a php problem or java scripts.

    viewf.php
    HTML Code:
    <span>
     <a href="javascript: edit(<?=$row['id'];?>,<?=$row['friends'];?>);" title="edit">Edit</a></span>|
    can i send something value like this in java script. i want to update my friends name so i need both id and friends value

    edit.php
    PHP Code:
    $id $_REQUEST['id'];
    $friends =$_REQUEST['friends'];
    $query "UPDATE  friends set friends='".$friends."' where id='$id'"
    and my ajax.js
    Code JavaScript:
    function edit(id,friends){
     
    document.getElementById(id,friends).innerHTML = "<em>editing...</em>";
      alert("hello");
     
     
                  http.open('GET', 'edit.php?id='+id+'&friends='+friends);
     	     http.onreadystatechange = insertReply1;
                  http.send(null);
     
     
         }

    can anyone help me with my code? now the status is record is getting empty as soon as i fire the query. i dont want to delete record but want to update it. it seems friends value is not getting passed through my JS.

    thanks in advance

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    document.getElementById(id,friends).innerHTML = "<em>editing...</em>";
    The only argument to document.getElementById is the ID attribute of an HTML element on the page. So "id,friends" doesn't make sense here.

  3. #3
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your quick reply Dan Grossman,

    you mean to say cant i send two id values in edit function?

    if yes so i can use something like this right?
    var site_url= encodeURI(document.getElementById('id').value);
    var site_name = encodeURI(document.getElementById('friends').value);

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    No, I mean to say only what I said.

  5. #5
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    Code JavaScript:
    function edit(){
     
     document.getElementById('insert_response').innerHTML = "Updating...";
      alert("hello");
     var id= encodeURI(document.getElementById('id').value);
    var friends = encodeURI(document.getElementById('friends').value);              
     
     
    http.open('get', 'edit.php?id='+id+'&friends='+friends+'&nocache = '+nocache);
     
     	     http.onreadystatechange = insertReply;
                  http.send(null);
     
     
         }


    i have tried this but its not working... what is your suggestion?

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You were passing in id and friends as parameters through a link, now you're reading them from the value property of some elements. Do those elements exist, are they the type to have value properties, and is their value the correct ID and string you want for the query?

  7. #7
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is their value the correct ID and string you want for the query?
    yes, i want to update my friends value.
    Do those elements exist
    yes, they exits on my viewf.php page like this
    PHP Code:
     $result =  $op->runsql("SELECT * FROM friends WHERE myid = '$userid' ORDER BY id ASC");


         $result1                = $op->runsql($result);
         $total_records         = mysql_num_rows($result1);
         $limit_value           = 10;
         $top                   = $total_records/$limit_value;
         $total_pages           = ceil($top);
         while($row = $op->select($result)){ ?>
     <input type="text" name="friends"   value="<?=$row['friends'];?>"  size="25" maxlength=15 /><span><a href="javascript: edit();" title="edit">Edit</a></span>|
    <input type="hidden" name="id" value="<?=$row['id'];?>" id="<?=$row['id'];?>" /><span><a href="javascript:msgdelete(<?=$row['id'];?>);" title="Delete">Delete</a></span>
    <? ?>
    are they the type to have value properties
    dont know what does that mean

  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The ID attribute of those inputs should be "id" and "friends", not the name attribute.

  9. #9
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML Code:
    <input type="text" name="friends"   value="<?=$row['friends'];?>" id="<?=$row['friends'];?>" size="25" maxlength=15 />
    	 <input type="hidden" name="id" value="<?=$row['id'];?>" id="<?=$row['id'];?>" /><span>&nbsp;&nbsp; &nbsp;<a href="javascript: edit();" title="edit">Edit</a></span>|<a href="javascript:msgdelete(<?=$row['id'];?>);" title="Delete">Delete</a></span>

    but here i have to use name attribute as id right because those value i want to pass.right?

  10. #10
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    document.getElementById('friends').value returns the value of an input with an *ID* of "friends". There is no such input. Change the ID on your two form elements. The ID attribute is supposed to be a unique identifier, not a value holder.

  11. #11
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    while deleting records i have sent id as
    id="<?=$row['id'];?>"
    and it's working fine. so i am bit confused when you said

    The ID attribute is supposed to be a unique identifier, not a value holder
    because id is holding its database value.

  12. #12
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Perhaps, but your code is looking for an element with an ID of "friends", and no such element exists. We can talk in circles all day.

  13. #13
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We can talk in circles all day.
    not getting here what does that mean?

    HTML Code:
    <input type="text" name="friends"   value="<?=$row['friends'];?>" name="friends1" id ="<?=$row['friends'];?>">
    id for friends is much more exits in my code. while deletion i have to pass only one id. so it was easy but for updation i need both values what to change and where to change(where condition)

    so i just want to know the method to pass two values into my JS function.please

  14. #14
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    United States
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Deepson2, This may help: http://www.tizag.com/javascriptT/jav...lementbyid.php He's telling you that your id is wrong.

  15. #15
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I want to help with my code.i am just not able to find out that is it a php problem or java scripts.
    When trying to sort out where the fault lies with PHP/JS I developed this method.

    Always write your JS by hand first with real values in a straight html file, and get that debugged and working first.

    Then get your PHP backend handler to do what you expect it to do, use the GET method and just fiddle around with the values till you know it does exactly as you want.

    Only when those 2 things are working should you try to get PHP to output the JS and link the two together.

    Otherwise you will lose your mind (I did, 'till I made that rule up).
    Last edited by Cups; Apr 30, 2009 at 03:38. Reason: clarified

  16. #16
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thnaks cups for your reply.
    use the GET method and just fiddle around with the values till you know it does exactly as you want.
    I am using GET method only. but the confusion comes when i need to pass two values. i think it creates conflict there. because i didnt find anything on net where pass two values using getelementid.

    i will try to work on what you said. now the stutus is getting this:
    idiot's not working[object HTMLDivElement]updation's not done
    thanks tmapm,

    your link was good. but i think its not useful for me. because i want to pass two values in URL using JS. anyways thakns.

    can anyone help me.


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
  •