/    Sign up×
Community /Pin to ProfileBookmark

Javascript says variable is undefined?!?!?

Hi,

I’m pulling my hair out because I have checked, checked and checked again and I cannot find the problem!!

Here’s my code…

[CODE]
<script language=”JavaScript”>

function createInstance()
{
var req = null;
if (window.XMLHttpRequest)
{
req = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
try {
req = new ActiveXObject(“Msxml2.XMLHTTP”);
} catch (e)
{
try {
req = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (e)
{
alert(“XHR not created”);
}
}
}
return req;
};

function delcat()
{
var req = createInstance();
var cat_id = document.delcatform.delcatid.value;
var data = “action=delete_cat&cat_id=” + cat_id;

req.onreadystatechange = function()
{
if(req.readyState == 4)
{
if(req.status == 200)
{

}
else
{
alert(“Error: returned status code ” + req.status + ” ” + req.statusText);
}
}
};

req.open(“POST”, “ajax-test2.php”, true);
req.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”);
req.send(data);
alert(data);
};

</script>

<?php

include (‘../dbconfig.php’);

$result = mysql_query(“SELECT * FROM category ORDER BY cat_id ASC”);
while ($row = mysql_fetch_array($result))
{
echo $row[‘cat_name’];
echo ‘<form name=”delcatform” method=”POST” action=””>
<input type=”text” name=”delcatid” value=”‘.$row[‘cat_id’].'” /><br />
<input type=”submit” value=”Delete Category” onClick=”delcat()”>
</form<br />’;
}

?>
[/CODE]

I am having a few problems with trying to get three different forms to work from the one page but I’ll go into that after I have this one fixed!!…I have set it to alert the data so that I can see the information is being submitted properly. In the alert box it tells me “action=delete_cat&cat_id=undefined”.

Any help with solving would be appreciated….

thanks in advance

Smithster

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Feb 06.2011 — var cat_id = document.delcatform.delcatid.value;

alert(cat_id);

what does that say?

Eric
Copy linkTweet thisAlerts:
@smithsterauthorFeb 06.2011 — var cat_id = document.delcatform.delcatid.value;

alert(cat_id);

what does that say?

Eric[/QUOTE]


Hi Eric

It outputs "undefined". I have just thought that it has something to do with the whole form being within the while statement, which means it's echoing each form onto the page with the same name....I don't know how to get around that though.....any ideas?

Regards
Copy linkTweet thisAlerts:
@A1ien51Feb 06.2011 — onclick="delcat(this.form)"

and


function delcat(frm){

var req = createInstance();

var cat_id = frm.delcatid.value;

Eric
Copy linkTweet thisAlerts:
@smithsterauthorFeb 06.2011 — You beauty!! I nearly hung myself from the loft trying to figure this out ha ha!!

Thanks very much, that may actually help me out with the other problems I have but I'll post back if anymore probs.

Thanks again

Regards

Smithster
Copy linkTweet thisAlerts:
@smithsterauthorFeb 07.2011 — Ok, I successfully managed to get it all working with 3 different forms in play too!!

My next issue though is with file uploading. I need to upload a csv doc and then php puts the data into the database. Can I use the same ajax code to pass on the file details to the php script?

I have tried with this but although the file is uploading, I don't think the variables are being sent correctly.

Here's my code...
[CODE]
<script>
var req = createInstance();
var action = frm.action.value;
if (frm.action.value == 'uploadcsv')
{
var uploaded = frm.uploaded.value;
var data = "action=" + action + "&uploaded=" + uploaded;
}
</script>

<form enctype="multipart/form-data" action="" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="hidden" name="action" value="uploadcsv" />
<input type="submit" value="Upload CSV" onClick="uploadcsv(this.form)" />
</form>
[/CODE]

Thanks in advance

Smithster
Copy linkTweet thisAlerts:
@KorFeb 07.2011 — [B]action[/B] is already an attribute, thus it can not have itself an attribute of its own like [B]value[/B]

On the other hand you try to refer the form element straight in an open code (outside any function) before that element (the form) is actually loaded.
Copy linkTweet thisAlerts:
@smithsterauthorFeb 07.2011 — [B]action[/B] is already an attribute, thus it can not have itself an attribute of its own like [B]value[/B]
[/QUOTE]

I have used action as a variable on my other forms which submit data to the server and it hasn't failed so I don't think it is an issue.

On the other hand you try to refer the form element straight in an open code (outside any function) before that element (the form) is actually loaded.[/QUOTE]

Not following this part.... :/
Copy linkTweet thisAlerts:
@KorFeb 07.2011 — I have used action as a variable on my other forms which submit data to the server and it hasn't failed so I don't think it is an issue.

Not following this part.... :/[/QUOTE]

OK, let's detail:

1. <i>
</i>[COLOR="Red"]&lt;script&gt;[/COLOR]
//...
&lt;/script&gt;

You miss the type. Should be:
<i>
</i>&lt;script [COLOR="Blue"]type="text/javascript"[/COLOR]&gt;

2. <i>
</i>&lt;script type="text/javascript"&gt;
var action = [COLOR="Red"]frm.action.value[/COLOR];
//..
&lt;/script

- [B]frm[/B] was not defined. What is [B]frm[/B]?

- even it would have been defined elsewhere, it looks like it refers an HTML element, probably a form. But the HTML elements were not loaded yet the moment you wrote that code line. As a general rule you have to use functions and wrote the code lines inside the functions, not outside them.

- if [B]frm[/B] refers a FORM element, [B]action[/B] should be the HTML [I]attribute [B]action[/B][/I] of the form. But the attribute action can not have [I]again[/I] a value attribute of his own. It has simply a value, but not a property called [B]value[/B].

Could be, maybe:
<i>
</i>function myFunction(){
var action=document.getElementById('frm').action;
}

If the form has an id="frm"
Copy linkTweet thisAlerts:
@smithsterauthorFeb 07.2011 — Ok, sorry I must have caused confusion, I posted only a snippet of the entire code as I had already posted the entire code in the earlier post. I only posted the part that was relevant.

To save confusion, here is the entire code...
[code=php]
<script type="text/javascript">
function createInstance()
{
var req = null;
if (window.XMLHttpRequest)
{
req = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e)
{
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e)
{
alert("XHR not created");
}
}
}
return req;
};

function uploadcsv(frm)
{
var req = createInstance();
var action = frm.action.value;
if (frm.action.value == 'uploadcsv')
{
var uploaded = frm.uploaded.value;
var data = "action=" + action + "&uploaded=" + uploaded;
}

req.onreadystatechange = function()
{
if(req.readyState == 4)
{
if(req.status == 200)
{

}
else
{
alert("Error: returned status code " + req.status + " " + req.statusText);
}
}
};

req.open("POST", "functions.php", true);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.send(data);
alert(data);
}
</script>
<form enctype="multipart/form-data" action="" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="hidden" name="action" value="uploadcsv" />
<input type="submit" value="Upload CSV" onClick="uploadcsv(this.form)" />
</form>
[/code]

Hope that helps ?
Copy linkTweet thisAlerts:
@KorFeb 07.2011 — Oh, I see. If so, the same line brings you problems:
<i>
</i>var action = frm.[COLOR="Red"]action[/COLOR].value;

The form has already a native [I]HTML attribute[/I] called [B][COLOR="Blue"]action[/COLOR][/B], but it has also an [I]element[/I] (an INPUT element) with the [COLOR="DarkGreen"][B]name="action"[/B][/COLOR], which will confuse the interpreter.
<i>
</i>&lt;form enctype="multipart/form-data" [COLOR="Blue"][B]action=""[/B][/COLOR] method="POST"&gt;
...
&lt;input type="hidden" [B][COLOR="DarkGreen"]name="action"[/COLOR][/B] value="uploadcsv" /&gt;


What is [B]frm.action[/B], he might think ? ? The attribute or the element? Usually he decides for the attribute.

The solve? Change the name of the element. say: "[B]A[/B]ction"
<i>
</i>var req = createInstance();
var action = frm.[B][COLOR="Blue"]A[/COLOR][/B]ction.value;
if (frm.[B][COLOR="Blue"]A[/COLOR][/B]ction.value == 'uploadcsv')
{
var uploaded = frm.uploaded.value;
var data = "[COLOR="Blue"][B]A[/B][/COLOR]ction=" + action + "&amp;uploaded=" + uploaded;
}
...
&lt;input type="hidden" name="[B][COLOR="Blue"]A[/COLOR][/B]ction" value="uploadcsv" /&gt;

Copy linkTweet thisAlerts:
@smithsterauthorFeb 07.2011 — I have changed from action to task although I understand where you're coming from but action has never failed me before.

Anyway, with using task instead, it still doesn't work. I have included alert(data); which says action=uploadcsv&uploaded=C:fakepathprices.csv

I'd like to check the PHP side....
[code=php]
if ($_POST['action'] == 'uploadcsv')
{
$target = "";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
[/code]

I know it isn't secure, right now I just want to get it working!! I don't think it's the PHP because the file upload works without the javascript if I just post straight to the php script. But I need it to work from the current page without a refresh.

Regards

Paul
Copy linkTweet thisAlerts:
@KorFeb 07.2011 — If you want to send an URL as a value on submit, you should encode it:

http://www.openjs.com/scripts/data/ued_url_encoded_data/
×

Success!

Help @smithster 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.29,
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,
)...