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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need Help on This urgently....

    hi guys...iam a new member nd im new to ajax nd php... iam trying to update my database through ajax. the problem is that when i m passing parameters to the update file it only pass the id not the edited text. its in a textarea. here is some code: database name is backend.

    <a onclick="updatedata(<?php echo $id; ?>)" href="#"><img src="images/save.png"></a>

    update.js

    // JavaScript Documentvar xmlhttp
    function updatedata(id)
    {
    var xmlhttp
    xmlhttp= GetXmlHttpObject()
    if(xmlhttp==null)
    {
    alert("browser does not support HTTP request")
    return
    }
    xmlhttp.onreadystatechange= function()
    {
    if(xmlhttp.readystate==4)
    {
    var updt= document.getElementById("mydata");
    updt.value=xmlhttp.responseText;
    }
    }
    var str= document.getElementById("mydata").value;
    var url="insert.php?id=" + id + "&data=" + str;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }

    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
    {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
    }
    if (window.ActiveXObject)
    {
    // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
    }

    update.php
    <?php
    $id=$_GET["id"];
    $data=$_GET["data"];
    $conn= mysql_connect("localhost", "root", "");
    if(!$conn)
    {
    die("connection failed!" . mysql_error());
    }
    mysql_select_db("backend", $conn);
    $query = "UPDATE myfirst SET description ='$data' WHERE id='$id'";
    mysql_query($query);
    ?>

    what is the problem?kindly spot it to me......

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by Mr.47 View Post
    when i m passing parameters to the update file it only pass the id not the edited text.
    how did you confirm that?

    it could a) not read the form data, b) crop data due to fixed GET length (but that would leave at least some data), c) the DB query fails.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose. You can (and should) edit it to make it more meaningful.

    Also please read the posting guidelines regarding [code] tags.

  • #4
    New Coder
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    there is no problem with the db query. when i alert the url in the js file it show me like this:
    insert.php?id=1&data=these online lectures have attracted people of all ages.It offers purely education courses which are online especially designed for kids and children although these online lectures have attracted people of all ages. while when i use to echo the id on insert.php it shows nothing and similarly the id is not in the url.

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    if you have FireFox with the FireBug add-on installed, you can check, what has been submitted in the AJAX call. on the other hand, if insert.php echoes out anything, it should be present in the return data (also available in FireBug)

  • #6
    New Coder
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the ajax call submits both the id and the content but at the insert.php page i could only see the content in the url not the id thats y the database never updates..i echoed the content in the insert.php page it shows it but the problem is about the id??

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    what does var_dump($_GET); print out?

    additionally, you have to escape your input data (e.g. by using mysql_real_escape_string()), currently you're wide open to SQL Injection.

  • #8
    New Coder
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    var_dump($_GET) showed this:
    array(3) { ["mydata"]=> string(366) " Sabia Technologies is a part of Bir Al Sabia Group Of Companies achieved recognition with high quality web development projects and delivering web development services of any complexity to clients worldwide. Our customers are companies of all sizes ranging from start ups to large enterprises who realize that they need a professional internet solution to generate." ["x"]=> string(2) "14" ["y"]=> string(1) "8" }

  • #9
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    that is, you don't pass id/data to begin with. and (as I just noticed) the AJAX calls insert.php while you show the code of update.php ...

  • #10
    New Coder
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok then tell me what should i do to make it correct? i am new to all of this stuff so be a little explanatory plz...if u can show it to me in the code then it will be more easy to me

  • #11
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    what’s in insert.php?

  • #12
    New Coder
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    its actually the update.php file i had renamed it now

  • #13
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    do you have a page to look at? seems to be more to it than the code given here can explain.

  • #14
    New Coder
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hey dormilich! its working now but now the problem is that when i click the save button the record is updated but when i veiw the record it shows me the older one not the updated record while when i look at the database its updated there.... and when i use to delete the offline files in the internet options then for one instance it shows me the updated record but after that the same goes on.... wats the problem?
    Last edited by Mr.47; 01-18-2010 at 10:18 AM.

  • #15
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    I have experienced, that you have to wait a bit to get the updated query back.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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