Closed Thread Icon

Preserved Topic: Using PHP to generate Javascripts (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=18310" title="Pages that link to Preserved Topic: Using PHP to generate Javascripts (Page 1 of 1)" rel="nofollow" >Preserved Topic: Using PHP to generate Javascripts <span class="small">(Page 1 of 1)</span>\

 
butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-30-2001 20:10

I am using a PHP script to take a user defined number and generate a form based on that user input. The form fields are set up as an array, i.e.:

<form name="games" method="post" action="addgames_admin.php" ONSUBMIT="return testAndSubmit(this)">
Game 1<br>
Date: <input name="date[1]" type="text" value=""><br>
Our Team: <input name="us[1]" type="text" value=""><br>
Their Team: <input name="them[1]" type="text" value=""><br><br>
Game 2<br>
Date: <input name="date[2]" type="text" value=""><br>
Our Team: <input name="us[2]" type="text" value=""><br>
Their Team: <input name="them[2]" type="text" value=""><br><br>

Every time I try to test the form validation scripts (no there's nothing wrong with them, they're Mr. Max's) I get an error that says "date.1 is null or has no value, but I have no form field named date.1 it's name is date[1].

Can someone enlighten me as to why am I having this problem?

Thanks
-Butcher-



[This message has been edited by butcher (edited 12-30-2001).]

massacre
Bipolar (III) Inmate

From: the space between us
Insane since: Dec 2001

posted posted 12-30-2001 20:55

kinda similar problem here:
http://www.ozoneasylum.com/Forum2/HTML/001261.html

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 12-30-2001 21:11

the problem here is probably that when you do the validation the date[1] is seen as a javascript array.

have to take a look at the validation to make sure



:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-30-2001 21:21

Thanks Grumble

I read the whole post and unfortunately almost all of it went over my head. Ahh, one day I'll get it.

Bitdamaged

Are you saying I should write the validation as date1 instead of date[1]?

Here's the script:

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!-- ;

// Written by mr.maX, http://www.max.co.yu/

function testBlank(field, msg) {
if (field.value == "") {
alert(msg);
field.focus();
field.select();
return false;
} else
return true;
}

function checkDate(field, msg) {
var timeRe = /^([\d]{1,2}\/[\d]{1,2}\/[\d]{2})$/
if (!timeRe.test(field.value)) {
alert(msg);
field.focus();
field.select();
return false;
} else
return true;
}

function testNumber(field, msg) {
var maxRe = /^[\d]+$/
if (!maxRe.test(field.value)) {
alert(msg);
field.focus();
field.select();
return false;
} else
return true;
}

function testAndSubmit(form) {
return (
checkDate(form.date[1], "Date for Game1 (must not be blank and it's format must be MM/DD/YY )") &&
testBlank(form.us[1], "Our Team (Game1) must not be blank)") &&
testBlank(form.them[1], "Their Team (Game 1) must not be blank)") &&
checkDate(form.date[2], "Date for Game2 (must not be blank and it's format must be MM/DD/YY )") &&
testBlank(form.us[2], "Our Team (Game2) must not be blank)") &&
testBlank(form.them[2], "Their Team (Game 2) must not be blank)") );
}
// -->
</SCRIPT>

Thanks



[This message has been edited by butcher (edited 12-30-2001).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 12-30-2001 21:33

You're trying to assign these form elements to an array just by giving them names that look like javascript code. You can't do that. When the browser sees date[0], it's looking for an object called "date" that's an array, and then it looks for the first element of that array. But you haven't made an object called "date".

The reason it says date.1 is not an object, is because the bracket [] operator looks for a property of the given object; for instance, with date[1], it's looking for the property called "1" of the date object. But since the date object hasn't been defined, of course it's properties aren't defined either, so it fails to find date.1.

Change them to date0, date1, date2, etc. Then, if you have a variable called "x", and you want date[x], just use document.games["date"+x].value. That looks for the property of the document.games object that is named "date" plus whatever x is.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 12-30-2001 22:02

Butcher, modify testAndSubmit() function to look like this:

function testAndSubmit(form) {
return (
checkDate(form.elements["date[1]"], "Date for Game1 (must not be blank and it's format must be MM/DD/YY )") &&
testBlank(form.elements["us[1]"], "Our Team (Game1) must not be blank)") &&
testBlank(form.elements["them[1]"], "Their Team (Game 1) must not be blank)") &&
checkDate(form.elements["date[2]"], "Date for Game2 (must not be blank and it's format must be MM/DD/YY )") &&
testBlank(form.elements["us[2]"], "Our Team (Game2) must not be blank)") &&
testBlank(form.elements["them[2]"], "Their Team (Game 2) must not be blank)") );
}

Slime, he named input fields like that so that PHP can automatically put their values into array...


butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-30-2001 22:11

Thanks Slime

As always, your right. Changing them to date1, date2 ect. makes the javascript work... But, it screws up my PHP script that is collecting the info to put in the database. In my PHP code:

for ($i = 1; $i <= $num_fields; $i++)
{

$newline = "$date[$i]

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-30-2001 22:13

Thanks mr.maX

You got in there while I was posting back to Slime's answer.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 12-31-2001 00:22

Ah, I didn't realize the PHP required that. Yup, Max's code works because by using the object["property"] syntax, it keeps the browser from looking at date[0] and thinking it's an array in itself.

« BackwardsOnwards »

Show Forum Drop Down Menu