Jump to content

"is Not Defined" Error


[dx]

Recommended Posts

Hi there,I've made Ajax script which is saved to ajax.js file. Then I've made html form for registration with method="post". There's <input type="button" onclick="reg()" value="Register" />In ajax.js file there's function called reg

function reg() {// rest of code

And script is included to registration page (register.php) via

<script type="javascript" src="/ajax.js"></script>

(already chechked .js location, it's correct)So when using firebug, I'm keep getting error "is not defined"I've tried searching google, lots of tuts, but everything seems to be ok with script.Regards

Link to comment
Share on other sites

  • Replies 62
  • Created
  • Last Reply

There could be a JS error in the reg() function - post all your code. Also, remember that / refers to the root directory, not the current one.Note - it is better to handle the onsubmit event of the form, because the are other ways to submit a form besides clicking the submit button (e.g. by pressing enter in a text field).

Link to comment
Share on other sites

ajax.js

function kreirajRO {	var katalog;	if (window.XMLHttpRequest) {	rez = new XMLHttpRequest(); }	else if (window.ActiveXObject) {	rez = new ActiveXObject("Microsoft.XMLHTTP"); }	else {		alert("Greska u kreiranju stranice! Pokusajte sa drugim browserom.");	}	return rez;}var katalog = kreirajRO();function ajax_katalog(sta) {	if (katalog.readyState == 4) {		var rez = katalog.responseText;		if (rez) {		document.getElementById(sta).innerHTML = rez;		}	}}function regU() {	ime=document.getElementById("ime");	prezime=document.getElementById("prezime");	email=document.getElementById("email");	sifra=document.getElementById("sifra");	d_rodj=document.getElementById("d_rodj");	m_rodj=document.getElementById("m_rodj");	g_rodj=document.getElementById("g_rodj");	spol=document.getElementById("spol");	katalog.open('get', 'registracija_src.php?ime='+ime.value+'&prezime='+prezime.value+'&email='+email.value+'&sifra='+sifra.value+'&d_rodj='+d_rodj.value+'&m_rodj='+m_rodj.value+'&g_rodj='+g_rodj.value+'&spol='+spol.value, true);	katalog.onreadystatechange = gotova_reg () {		if (katalog.readyState == 4) {			var rez = katalog.responseText;			if (rez == "1") { document.getElementById("registracijastatus").innerHTML = "Unesite vase ime"; }			else if (rez == "2") { document.getElementById("registracijastatus").innerHTML = "Unesite vase prezime"; }			else if (rez == "3") { document.getElementById("registracijastatus").innerHTML = "Unesite vas email"; }			else if (rez == "4") { document.getElementById("registracijastatus").innerHTML = "Unesite vasu sifru"; }			else if (rez == "5") { document.getElementById("registracijastatus").innerHTML = "Izaberite datum rodjenja"; }			else if (rez == "6") { document.getElementById("registracijastatus").innerHTML = "Izaberite spol"; }			else if (rez == "7") { document.getElementById("registracijastatus").innerHTML = "Sifra mora sadrzavati najmanje 5 znakova"; }			else if (rez == "8") { document.getElementById("registracijastatus").innerHTML = "Ovaj email je vec registrovan"; }			else if (rez == "9") { document.getElementById("registracijastatus").innerHTML = "Nepravilan format email adrese"; }			else { document.getElementById("registracijastatus").innerHTML = rez; }		}	}	katalog.send(null);}

registracija_src.php

<?PHPrequire_once("baza.php");$ime = $_GET['ime'];$prezime = $_GET['prezime'];$email = $_GET['email'];$sifra = $_GET['sifra'];$d_rodj = $_GET['d_rodj'];$m_rodj = $_GET['m_rodj'];$g_rodj = $_GET['g_rodj'];$datum_rodjenja = "$d_rodj-$m_rodj-$g_rodj";$datum_rodjenja = strtotime($datum_rodjenja);$spol = $_GET['spol'];$rez_email = mysql_query("SELECT email FROM korisnici WHERE email=$email");if (empty($ime)) { echo "1"; }elseif (empty($prezime)) { echo "2"; }elseif (empty($email)) { echo "3"; }elseif (empty($sifra)) { echo "4"; }elseif (empty($datum_rodjenja)) { echo "5"; }elseif (empty($spol)) { echo "6"; }elseif (strlen($sifra) <= 4) { echo "7"; }elseif (mysql_num_rows($rez_email) > 0) { echo "8"; }elseif (!preg_match('/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/i', $email)) { echo "9"; }else {// Rest of code (mysql query) here}?>

registracija.php

<?phprequire_once("baza.php");echo '<script type="javascript" src="/webk/ajax.js"></script>';echo '<div id="registracijastatus"></div><form name="registracija" method="post" action="">  Ime: <input id="ime" onfocus="this.value=\'\'" value="Unesi ime" /><br />  Prezime: <input id="prezime" onfocus="this.value=\'\'" value="Unesi prezime" /><br />  Email: <input id="email" onfocus="this.value=\'\'" value="Unesi email" /><br />  Sifra: <input id="sifra" onfocus="this.value=\'\'" type="password" value="" /><br />  Datum rodjenja: 	<select id="d_rodj">'; $d = 1; while ($d <= 31) { echo '<option value="'.$d.'">'.$d.'</option>'; $d++; } echo '</select>	<select id="m_rodj">'; $m = 1; while ($m <= 12) { echo '<option value="'.$m.'">'.$m.'</option>'; $m++; } echo '</select>	<select id="g_rodj">'; $g = date(Y)-13; while ($g >= 1950) { echo '<option value="'.$g.'">'.$g.'</option>'; $g--; } echo '</select><br />  Spol: <select id="spol"><option value="m">Musko</option><option value="z">Zensko</option></select>  <input type="button" value="Registruj se" onclick="regU()" /></form>';?>

Locations:

http://localhost/webk/registracija.phphttp://localhost/webk/registracija_src.phphttp://localhost/webk/ajax.js

Thats all,Regards

Link to comment
Share on other sites

Well, this is a problem: katalog.onreadystatechange = gotova_reg () {You are trying to assign an anonymous function to .onreadystatechange . For that, you need to use the function keyword. Instead, the interpreter thinks you are trying to assign the return value of a function called gotova_reg () . And of course this function doesn't exist. So maybe you want this:katalog.onreadystatechange = function () { There may be other problems, but this stands out.

Link to comment
Share on other sites

Yes, I understand now. I thought that you makeing new function there :)But still getting this: function onclick(event) {regU(); regU is not defined}

Link to comment
Share on other sites

it still looks as if it can't find regU function from within ajax.js, so it throws up an undefined error.it looks as though all these files are in the same location, so try using this instead<script type="javascript" src="ajax.js"></script>echo '<script type="javascript" src="ajax.js"></script>';

Link to comment
Share on other sites

You mean like this?

<head><script type="text/javascript" src="ajax.js"></script></head><?phprequire_once("baza.php");echo '<script type="text/javascript" src="ajax.js"></script>';echo '<div id="registracijastatus"></div>...

Link to comment
Share on other sites

I just showed both because you had listed one in first post, and the second within the registracija.php pagei don't know where the first one was placed, so i just listed both, so you could make sure wherever they were inserted matched, IF the pages they are inserted into are at the same location as ajax.js.

Link to comment
Share on other sites

Yeah, great, no errors now, but I can't get answer. It should post answer to <div id="registracijastatus"></div> but nothinge.g. if (rez == "1") { document.getElementById("registracijastatus").innerHTML = "Unesite vase ime"; }from if (empty($ime)) { echo "1"; }

Link to comment
Share on other sites

What do you mean nothing, what did it output? I think I've told you this before, but you need to be using Firebug to help with this. Run this in Firebug and you can actually see the request go out and look at the response, you can see if the response had an error or what the response code was.

Link to comment
Share on other sites

I see html souce code

<script type="text/javascript" src="ajax.js"></script><div id="registracijastatus"></div><form onsubmit="regU()" name="registracija" method="post" action="">  Ime: <input id="ime" onfocus="this.value=''" value="Unesi ime" /><br />  Prezime: <input id="prezime" onfocus="this.value=''" value="Unesi prezime" /><br />  Email: <input id="email" onfocus="this.value=''" value="Unesi email" /><br />  Sifra: <input id="sifra" onfocus="this.value=''" type="password" value="" /><br />  Datum rodjenja: 	<select id="d_rodj"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option><option value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option value="31">31</option></select>	<select id="m_rodj"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option></select>	<select id="g_rodj"><option value="1996">1996</option><option value="1995">1995</option><option value="1994">1994</option><option value="1993">1993</option><option value="1992">1992</option><option value="1991">1991</option><option value="1990">1990</option><option value="1989">1989</option><option value="1988">1988</option><option value="1987">1987</option><option value="1986">1986</option><option value="1985">1985</option><option value="1984">1984</option><option value="1983">1983</option><option value="1982">1982</option><option value="1981">1981</option><option value="1980">1980</option><option value="1979">1979</option><option value="1978">1978</option><option value="1977">1977</option><option value="1976">1976</option><option value="1975">1975</option><option value="1974">1974</option><option value="1973">1973</option><option value="1972">1972</option><option value="1971">1971</option><option value="1970">1970</option><option value="1969">1969</option><option value="1968">1968</option><option value="1967">1967</option><option value="1966">1966</option><option value="1965">1965</option><option value="1964">1964</option><option value="1963">1963</option><option value="1962">1962</option><option value="1961">1961</option><option value="1960">1960</option><option value="1959">1959</option><option value="1958">1958</option><option value="1957">1957</option><option value="1956">1956</option><option value="1955">1955</option><option value="1954">1954</option><option value="1953">1953</option><option value="1952">1952</option><option value="1951">1951</option><option value="1950">1950</option></select><br />  Spol: <select id="spol"><option value="m">Musko</option><option value="z">Zensko</option></select>  <input type="submit" value="Registruj se" /></form>

:S

Link to comment
Share on other sites

Well, it shows me http://127.0.0.1/webk/registracija , which is used by htaccess to open registracija.php via ^registracija$And all of that is in webk folder. And no matter what it call back, why alert function won't work.I've added it like this:

katalog.onreadystatechange = function () {  if (katalog.readyState == 4) {	rez = katalog.responseText;	alert(rez);

Link to comment
Share on other sites

With Firebug, it's usually more useful to use console.log instead of alert, console.log won't stop execution and you can see all of the messages in the Firebug console, on the Console tab. Add a few of those to see what's going on, e.g.:

katalog.onreadystatechange = function () {  console.log('readyState=' + katalog.readyState);  if (katalog.readyState == 4) {	console.log('ajax object:');	console.log(katalog);	rez = katalog.responseText;	console.log(rez);

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...