/    Sign up×
Community /Pin to ProfileBookmark

Error in AJAX script…

Ok so… I’m trying to fetch a div’s content dynamically through a script from another page. Now, initially, it works great. But when I try to use the form in the generated div content, I get an error:

[code]Uncaught TypeError: object is not a function native runtime.js:213[/code]

Here’s the JS code for the page:

[code]function getAjax()
{
if (window.XMLHttpRequest)
return new XMLHttpRequest();
else if (window.ActiveXObject)
return new ActiveXObject(“Microsoft.XMLHTTP”);
return null;
}
function startAddWebsite()
{
xmlhttp = getAjax();
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4) document.getElementById(‘addWebsiteDiv’).innerHTML = xmlhttp.responseText;
};
xmlhttp.open(“POST”, “templates/add-website.php?sid=”+Math.random(), true);
xmlhttp.send(null);
}
function addWebsite()
{
xmlhttp = getAjax();
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4) document.getElementById(‘addWebsiteDiv’).innerHTML = xmlhttp.responseText;
};
xmlhttp.open(“POST”, “templates/add-website.php?website=”+document.addWebsite.website.value+”&sid=”+Math.random(), true);
xmlhttp.send(null);
}[/code]

And here’s the add-website.php code:

[code]<? if (!$_GET[‘website’]) : ?>
<form name=”addWebsite” onsubmit=”this.addButton.onclick(); return false;”>
Add a website to your homepage: <input type=”text” name=”website” />
<input type=”button” name=”addButton” value=”Add” onclick=”addWebsite();” />
</form>
<? else : if ($_GET[‘website’])
{
$website = str_replace(‘javascript:’, ”, $_GET[‘website’]);
$seg = explode(“/”, $website);
$domain = $seg[2];
$website_result = mysql_query(“SELECT * FROM `websites` WHERE `url` = ‘$website'”);
$website_assoc = mysql_fetch_assoc($website_result);
if (mysql_num_rows($website_result))
mysql_query(“INSERT INTO `user_grid` (`user`, `website`) VALUES (‘1’, ‘{$website_assoc[‘id’]}’)”);
else
{
mysql_query(“INSERT INTO `websites` (‘url’, ‘domain’) VALUES (‘$website’, ‘$domain’)”);
$new_website = mysql_fetch_assoc(mysql_query(“SELECT * FROM `websites` WHERE `website` = ‘$website'”));
mysql_query(“INSERT INTO `user_grid` (`user`, `website`) VALUES (‘1’, ‘{$new_website[‘id’]}’)”);
}
echo ‘Added website successfully.’;
}
endif; ?>[/code]

What’s going wrong? I can’t really trace the error… I’m just starting with JS/AJAX, maybe I’m getting a wrong idea about something.

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @Hen_Asraf 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.22,
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,
)...