/    Sign up×
Community /Pin to ProfileBookmark

saving using ajax

Guys, I was trying to save a data using ajax but it seems not working. is there something wrong here? Here’s my javascript code:

[CODE]
<script type=”text/javascript”>
function saveitem()
{
name=document.getElementById(“txtname”).value;

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}

xmlhttp.open(“GET”,”save.php?q=”+name,true);
xmlhttp.send();
}
</script>

<body>

<form name=”frm1″>
<input type=”text” name=”txtname” id=”txtname”>
<input type=”submit” name=”cmdsave” id=”cmdsave” onClick=”saveitem();”>
</form>

</body>
[/CODE]

and here’s my save.php code:

[CODE]
<?php

$name = $_GET[‘q’];

mysql_connect(‘192.168.100.9′,’root’,’1234′) or die(“Unable to connect to MySQL”);

mysql_select_db(sampledb) or die(“Unable to select a database”);

$query = “insert into table1(name,age) values(‘$name’,”)”;

mysql_query($query) or die(“error in query”);

?>
[/CODE]

Now, what I’m trying to do is to transfer the inputted item to save.php so that in can be saved to my database. But save.php doesn’t seem to receive the data or maybe my javascript isn’t working. Can someone tell me why?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@tirnaJul 23.2010 — Do you have to use ajax for what you are doing?

I would have thought that submitting the form directly to save.php would be better.

[CODE]<form name="frm1" action="save.php">
<input type="text" name="txtname" id="txtname">
<input type="submit" name="cmdsave" id="cmdsave">
</form>[/CODE]


If you want to use ajax then try changing the submit button to type="button".
Copy linkTweet thisAlerts:
@iahneauthorJul 23.2010 — just trying out something new tirna..hehe..but yah i've tried to change the button into "button" type but still not working.
Copy linkTweet thisAlerts:
@tirnaJul 23.2010 — Have you confirmed in your debugger or using alert() if the value from the textbox is being assigned to the variable correctly in your function?

If so, then maybe do some Basic Debugiing 101 using a debugger or alert()'s to find which line of code is causing the problem.
Copy linkTweet thisAlerts:
@iahneauthorJul 23.2010 — yap i did as well... like:

alert('Here is the name value :'+name);

xmlhttp.open("GET","save.php?q="+name,true);

xmlhttp.send();

I try to check if there's a value before i send it.

Thank you for your replies tirna..always helping out. =)
Copy linkTweet thisAlerts:
@tirnaJul 23.2010 — Did you

echo $name;

in save.php and check the output of the above echo in the responseText property of your ajax object?
Copy linkTweet thisAlerts:
@criterion9Jul 23.2010 — 
onClick="saveitem();"
[/quote]

Try attaching that to your form element rather than a "button" or "submit".
<i>
</i>&lt;form name="blah" action="save.php" onsubmit="return saveitem();"&gt;

Then in your function return false to cancel the form being submitted. This has the added benefit of allowing the "regular" method to work if javascript is not enabled or doesn't work for some reason.
×

Success!

Help @iahne spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...