/    Sign up×
Community /Pin to ProfileBookmark

Error: type mismatch need some help

hey guys, i got a little problem, i just started with javascript so there might be an easy solution but okay… im trying to make a function with which i can perform some simple actions: here my javascript:

IE: Line: 38, Character: 5, Error: Type mismatch
it has something to do with the objectid im trying to get from the other function…

[CODE]
var http = createRequestObject();
var objID = ”;

function createRequestObject(htmlObjectId){
var obj;
var browser = navigator.appName;

objID = htmlObjectId;

if(browser == “Microsoft Internet Explorer”){
obj = new ActiveXObject(“Microsoft.XMLHTTP”);
}
else{
obj = new XMLHttpRequest();
}
return obj;
}

function sendReq(serverFileName, objID, variableNames, variableValues) {
var paramString = ”;

variableNames = variableNames.split(‘,’);
variableValues = variableValues.split(‘,’);

for(i=0; i<variableNames.length; i++) {
paramString += variableNames[i]+’=’+variableValues[i]+’&’;
}
paramString = paramString.substring(0, (paramString.length-1));

if (paramString.length == 0) {
http.open(‘get’, serverFileName);
}
else {
http.open(‘get’, serverFileName+’?’+paramString);
}
http.onreadystatechange = handleResponse(objID);
http.send(null); <<< Line 38

}

function handleResponse(objID) {

if(http.readyState == 4){
responseText = http.responseText;
document.getElementById(objID).innerHTML = responseText;
}
}[/CODE]

I hope some of you guys can help me !

Thx!

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscNov 06.2008 — Does it perhaps have anything to do with that fact that objID is nothing....so that when the request gets sent (line 38) the handleResponse function gets called with a bogus (i.e. null) objID?

The way you tell your code which HTML object to update is at line 1:

<i>
</i>var http = createRequestObject('idOfObjectToBeUpdated');
Copy linkTweet thisAlerts:
@nickooonameauthorNov 06.2008 — function sendReq(serverFileName, objID, variableNames, variableValues)

the start of the function gives objID a value right???


the .php file which call the js function:

(the quoting is also wrong i guess... but i dont know how it should be...)

&lt;form&gt;
Select a User:
&lt;select name="users" onChange="sendReq('ajaxart.php', 'txtHint', 'q, optie', '101 , show')"&gt;
&lt;option value="101"&gt;Art 101&lt;/option&gt;
&lt;option value="102"&gt;Art 102&lt;/option&gt;
&lt;option value="103"&gt;Art 103&lt;/option&gt;
&lt;option value="104"&gt;Art 104&lt;/option&gt;
&lt;/select&gt;
&lt;/form&gt;
Copy linkTweet thisAlerts:
@aj_nscNov 06.2008 — my apologies, i was looking at the JS code posted in a vaccuum, yeah, you're right about that.

Sorry, I totally missed this. You can't pass values the way you are doing it through onreadystatechange

http.onreadystatechange takes a reference to a function, it doesn't call a function directly with parameters.

It should be
<i>
</i>http.onreadystatechange = handleResponse;


Since objID is a global variable and it was updated in your sendReq function then everything else should be fine.
Copy linkTweet thisAlerts:
@nickooonameauthorNov 06.2008 — var http = createRequestObject();
var objID = '';


function createRequestObject(htmlObjectId){
var obj;
var browser = navigator.appName;

<i> </i>objID = htmlObjectId;

<i> </i>if(browser == "Microsoft Internet Explorer"){
<i> </i> obj = new ActiveXObject("Microsoft.XMLHTTP");
<i> </i>}
<i> </i>else{
<i> </i> obj = new XMLHttpRequest();
<i> </i>}
<i> </i>return obj;
}

function sendReq(serverFileName, objID, variableNames, variableValues) {
var paramString = '';

<i> </i>variableNames = variableNames.split(',');
<i> </i>variableValues = variableValues.split(',');

<i> </i>for(i=0; i&lt;variableNames.length; i++) {
<i> </i> paramString += variableNames[i]+'='+variableValues[i]+'&amp;';
<i> </i>}
<i> </i>paramString = paramString.substring(0, (paramString.length-1));

<i> </i>if (paramString.length == 0) {
<i> </i> http.open('get', serverFileName);
<i> </i>}
<i> </i>else {
<i> </i> http.open('get', serverFileName+'?'+paramString);
<i> </i>}
<i> </i>http.onreadystatechange = handleResponse;
<i> </i>http.send(null); &lt;&lt;&lt; Line 38

}

function handleResponse() {

<i> </i>if(http.readyState == 4){
<i> </i> responseText = http.responseText;
<i> </i> document.getElementById(objID).innerHTML = responseText;
<i> </i>}
}


Like this??? this one does not work btw

Error line 46 : document.getElementById(...) is null or not an object
Copy linkTweet thisAlerts:
@FourCourtJesterNov 06.2008 — [COLOR="Orange"]var http = createRequestObject();[/COLOR]
var objID = '';


function createRequestObject([COLOR="Red"]htmlObjectId[/COLOR]){
var obj;
var browser = navigator.appName;

<i> </i>objID = [COLOR="Red"]htmlObjectId[/COLOR];
<i> </i>...


Ok let's look at the [COLOR="Red"]RED[/COLOR] parts. I notice how your function requires you to pass in htmlObjectId. But, on [COLOR="Orange"]LINE ONE[/COLOR] you don't pass in an ID...

So yes, objID will be undefined because you're not passing anything to it...
Copy linkTweet thisAlerts:
@nickooonameauthorNov 06.2008 — function sendReq(serverFileName, objID, variableNames, variableValues)

the start of the function gives objID a value right???

[/QUOTE]


right?
Copy linkTweet thisAlerts:
@FourCourtJesterNov 06.2008 — Now you're talking about a totally different thing. sendReq takes in an objID yes, but you never assign that ID anywhere in sendReq.
Copy linkTweet thisAlerts:
@nickooonameauthorNov 06.2008 — okay but what do i have to do then to make it work??? cut and paste the function handleresponse into functionm sendReq ???
Copy linkTweet thisAlerts:
@FourCourtJesterNov 06.2008 — Put this into sendReq

objID = this.objID;

It will either solve the issue or just throw different ones.
Copy linkTweet thisAlerts:
@nickooonameauthorNov 06.2008 — doesn't make a difference same error here is the orginal code:

var http = createRequestObject();
var objectId = '';


function createRequestObject(htmlObjectId){
var obj;
var browser = navigator.appName;

<i> </i>objectId = htmlObjectId;

<i> </i>if(browser == "Microsoft Internet Explorer"){
<i> </i> obj = new ActiveXObject("Microsoft.XMLHTTP");
<i> </i>}
<i> </i>else{
<i> </i> obj = new XMLHttpRequest();
<i> </i>}
<i> </i>return obj;
}

function sendReq(serverFileName, variableNames, variableValues) {
var paramString = '';

<i> </i>variableNames = variableNames.split(',');
<i> </i>variableValues = variableValues.split(',');

<i> </i>for(i=0; i&lt;variableNames.length; i++) {
<i> </i> paramString += variableNames[i]+'='+variableValues[i]+'&amp;';
<i> </i>}
<i> </i>paramString = paramString.substring(0, (paramString.length-1));

<i> </i>if (paramString.length == 0) {
<i> </i> http.open('get', serverFileName);
<i> </i>}
<i> </i>else {
<i> </i> http.open('get', serverFileName+'?'+paramString);
<i> </i>}
<i> </i>http.onreadystatechange = handleResponse;
<i> </i>http.send(null);
}

function handleResponse() {

<i> </i>if(http.readyState == 4){
<i> </i> responseText = http.responseText;
<i> </i> document.getElementById(objectId).innerHTML = responseText;
<i> </i>}
}
Copy linkTweet thisAlerts:
@nickooonameauthorNov 07.2008 — ?

It works, i got some different script form internet and made it work together...

my code now is:


php which calls javascript:

&lt;!--&lt;script src="include/ajax.js"&gt;&lt;/script&gt;--&gt;
&lt;?php
include("include/ajax.inc.php");

?&gt;
&lt;div id=content&gt;
&lt;form&gt;
Select a User:
&lt;?php
$name ="'q, optie'";
$value ="'this.value , show'";
$ajax = new Ajax('txtHint');
?&gt;
&lt;select name="users" onChange="sendReq('include/ajaxart.php', 'q', '101')"&gt;
&lt;option value="101"&gt;Art 101&lt;/option&gt;
&lt;option value="102"&gt;Art 102&lt;/option&gt;
&lt;option value="103"&gt;Art 103&lt;/option&gt;
&lt;option value="104"&gt;Art 104&lt;/option&gt;
&lt;/select&gt;
&lt;/form&gt;
&lt;p&gt;
&lt;div id="txtHint"&gt;&lt;b&gt;No results...&lt;/b&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;/div&gt;



Javascript class:

&lt;?php

<i> </i>class Ajax{

<i> </i> /**
<i> </i> * Constructor function
<i> </i> */
<i> </i> function Ajax($htmlObjectId) {
<i> </i> print '&lt;script src="include/ajax.js" type="text/javascript"&gt;&lt;/script&gt;';
<i> </i> print "&lt;script type="text/javascript" language="JavaScript"&gt; createRequestObject('$htmlObjectId');&lt;/script&gt;";
<i> </i> }

<i> </i> /**
<i> </i> * This fucntion calls the remote file with certain parameters
<i> </i> *
<i> </i> * @param string $remoteFileName (filename with extension)
<i> </i> * @param [array $parameterNames] (names of all the parameters to be sent)
<i> </i> * @param [array $parameterValues] (corresponding values of the parameters)
<i> </i> *
<i> </i> * @return void
<i> </i> */
<i> </i> function sendRequest($remoteFileName, $parameterNames = array(), $parameterValues = array()) {

<i> </i> if (count($parameterNames) == count($parameterValues)) {
<i> </i> $parameterNames = implode(',', $parameterNames);
<i> </i> $parameterValues = implode(',', $parameterValues);

<i> </i> print "&lt;script type="text/javascript" language="JavaScript"&gt; sendReq('$remoteFileName', '$parameterNames', '$parameterValues');&lt;/script&gt;";
<i> </i> }
<i> </i> else{
<i> </i> die("'Parameter Names' and 'Parameters Values' do NOT match.");
<i> </i> }
<i> </i> }

<i> </i> /**
<i> </i> * This function returns the response as simple string/xml
<i> </i> * @package [$asXml] boolean
<i> </i> *
<i> </i> * @return string
<i> </i> */
<i> </i> function getResponse($asXml = false) {
<i> </i> if ($asXml) {

<i> </i> return "&lt;script type="text/javascript" language="JavaScript"&gt; document.write(responseXml);&lt;/script&gt;";
<i> </i> }
<i> </i> else {
<i> </i> return "&lt;script type="text/javascript" language="JavaScript"&gt; alert(responseText);document.write(responseText);&lt;/script&gt;";
<i> </i> }
<i> </i> }

<i> </i>}
?&gt;


Javascript functions:

var http = createRequestObject();
var objectId = '';


function createRequestObject(htmlObjectId){
var obj;
var browser = navigator.appName;

<i> </i>objectId = htmlObjectId;

<i> </i>if(browser == "Microsoft Internet Explorer"){
<i> </i> obj = new ActiveXObject("Microsoft.XMLHTTP");
<i> </i>}
<i> </i>else{
<i> </i> obj = new XMLHttpRequest();
<i> </i>}
<i> </i>return obj;
}

function sendReq(serverFileName, variableNames, variableValues) {
var paramString = '';

<i> </i>variableNames = variableNames.split(',');
<i> </i>variableValues = variableValues.split(',');

<i> </i>for(i=0; i&lt;variableNames.length; i++) {
<i> </i> paramString += variableNames[i]+'='+variableValues[i]+'&amp;';
<i> </i>}
<i> </i>paramString = paramString.substring(0, (paramString.length-1));

<i> </i>if (paramString.length == 0) {
<i> </i> http.open('get', serverFileName);
<i> </i>}
<i> </i>else {
<i> </i> http.open('get', serverFileName+'?'+paramString);
<i> </i>}
<i> </i>http.onreadystatechange = handleResponse;
<i> </i>http.send(null);
}

function handleResponse() {

<i> </i>if(http.readyState == 4){
<i> </i> responseText = http.responseText;
<i> </i> document.getElementById(objectId).innerHTML = responseText;
<i> </i>}
}




Thx for al the help / reply's
×

Success!

Help @nickoooname 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.19,
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,
)...