PDA

View Full Version : Need Help on This urgently....


Mr.47
01-16-2010, 08:31 AM
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......

Dormilich
01-16-2010, 09:27 AM
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.

Philip M
01-16-2010, 09:40 AM
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.

Mr.47
01-16-2010, 11:06 AM
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.

Dormilich
01-16-2010, 12:57 PM
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)

Mr.47
01-16-2010, 01:15 PM
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??

Dormilich
01-16-2010, 01:39 PM
what does var_dump($_GET); print out?

additionally, you have to escape your input data (e.g. by using mysql_real_escape_string() (http://php.net/mysql-real-escape-string)), currently you're wide open to SQL Injection.

Mr.47
01-16-2010, 02:02 PM
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" }

Dormilich
01-16-2010, 02:08 PM
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 ...

Mr.47
01-18-2010, 05:16 AM
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

Dormilich
01-18-2010, 07:51 AM
what’s in insert.php?

Mr.47
01-18-2010, 08:14 AM
its actually the update.php file i had renamed it now

Dormilich
01-18-2010, 08:17 AM
do you have a page to look at? seems to be more to it than the code given here can explain.

Mr.47
01-18-2010, 10:21 AM
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?

Dormilich
01-18-2010, 11:20 AM
I have experienced, that you have to wait a bit to get the updated query back.

Mr.47
01-20-2010, 07:14 AM
thanks alot dormilich for helping me at last its working now. now im trying to put this thing in wordpress but dont know how to do it? any ideas?

Dormilich
01-20-2010, 09:04 AM
what is wordpress?

Mr.47
01-20-2010, 11:17 AM
its a cms. have a search on it

jeddi
01-24-2010, 07:47 PM
what is wordpress?

hahaha :D

Dormilich
01-24-2010, 11:18 PM
I knew someone got the joke.