Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-16-2010, 07:31 AM   PM User | #1
Mr.47
New Coder

 
Join Date: Jan 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Mr.47 is an unknown quantity at this point
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......
Mr.47 is offline   Reply With Quote
Old 01-16-2010, 08:27 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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.
Dormilich is offline   Reply With Quote
Old 01-16-2010, 08:40 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 01-16-2010, 10:06 AM   PM User | #4
Mr.47
New Coder

 
Join Date: Jan 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Mr.47 is an unknown quantity at this point
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.
Mr.47 is offline   Reply With Quote
Old 01-16-2010, 11:57 AM   PM User | #5
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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)
Dormilich is offline   Reply With Quote
Old 01-16-2010, 12:15 PM   PM User | #6
Mr.47
New Coder

 
Join Date: Jan 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Mr.47 is an unknown quantity at this point
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??
Mr.47 is offline   Reply With Quote
Old 01-16-2010, 12:39 PM   PM User | #7
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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.
Dormilich is offline   Reply With Quote
Old 01-16-2010, 01:02 PM   PM User | #8
Mr.47
New Coder

 
Join Date: Jan 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Mr.47 is an unknown quantity at this point
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" }
Mr.47 is offline   Reply With Quote
Old 01-16-2010, 01:08 PM   PM User | #9
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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 ...
Dormilich is offline   Reply With Quote
Old 01-18-2010, 04:16 AM   PM User | #10
Mr.47
New Coder

 
Join Date: Jan 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Mr.47 is an unknown quantity at this point
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
Mr.47 is offline   Reply With Quote
Old 01-18-2010, 06:51 AM   PM User | #11
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
what’s in insert.php?
Dormilich is offline   Reply With Quote
Old 01-18-2010, 07:14 AM   PM User | #12
Mr.47
New Coder

 
Join Date: Jan 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Mr.47 is an unknown quantity at this point
its actually the update.php file i had renamed it now
Mr.47 is offline   Reply With Quote
Old 01-18-2010, 07:17 AM   PM User | #13
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
do you have a page to look at? seems to be more to it than the code given here can explain.
Dormilich is offline   Reply With Quote
Old 01-18-2010, 09:21 AM   PM User | #14
Mr.47
New Coder

 
Join Date: Jan 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Mr.47 is an unknown quantity at this point
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..
Mr.47 is offline   Reply With Quote
Old 01-18-2010, 10:20 AM   PM User | #15
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
I have experienced, that you have to wait a bit to get the updated query back.
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:23 PM.


Advertisement
Log in to turn off these ads.