Closed Thread Icon

Topic awaiting preservation: Two behaviors from one form? Pages that link to <a href="https://ozoneasylum.com/backlink?for=8067" title="Pages that link to Topic awaiting preservation: Two behaviors from one form?" rel="nofollow" >Topic awaiting preservation: Two behaviors from one form?\

 
Author Thread
butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 02-04-2002 19:49

I have a drop down box that I'm using for navigation choices. When the user selects an option it uses a php script to reload the page with the proper content using a php include().

I now want to be able to put some .pdf choices in the dropdown that won't be properly viewable if included as content in the webpage itself.

Is it possible to do an if else kinda thing based on the value of the selected option? Say the option values ended in either .pdf or .inc. Would it be possible to do something like:

if (the last 3 letters of option value == .pdf) {

pop the pdf in a new window

} else {

submit the form with the option value to the php script

}

I don't want to go crazy fishing through this JavaScript Bible if it can't be done in the first place.

Speaking of books, does anyone have any good recommendations for books to learn JavaScript from. I have O'Reilly's Definitive guide, and as mentioned before JavaScript Bible. Although these have a lot of information and make great references, I'm having a hard time really learning from them.

Thanks


-Butcher-

bitdamaged
Maniac (V) Mad Scientist

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

posted posted 02-04-2002 20:01

Sure all you want to do is create funtion that handles the form.

<form onSubmit="return pdfcheck(this)" ....

function pdfcheck(form) {
if (form.NameOfDropdown.value.indexOf('.pdf') > 1) {
location.href = form.NameOfDropdown.value;
return false
}
else {
return true
}
}

This should redirect the user to the pdf if the value of the dropdown box has .pdf somewhere in the value. (Easiest method off the top of my head) form will NOT be submitted.

Otherwise the form submits as normal.
}



:[ 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 02-04-2002 20:26

Thanks Bitdamaged!

Your my hero!!

I'd still like good book recommendations anyone has.

-Butcher-

dk01
Bipolar (III) Inmate

From: dk's house of love
Insane since: Oct 2001

posted posted 02-04-2002 21:35

Of hand I would do something like this:

code:
<form onSubmit="return pdfcheck(this)">

function pdfcheck(form) {
if (form.NameOfDropdown.value.substring(form.NameOfDropdown.value.length-4,form.NameOfDropdown.value.length) = ".pdf") {
location.href = form.NameOfDropdown.value;
return false
}
else {
return true
}
}



but it all works so
I would recommend Javascript for Dummies for quick references but if you are interested in dHtml and things then maybe Essential CSS & DHTML. It walks you through each step. Well those are the two that helped me at least. Later!
-dk

- can't decide? have another drink.

[This message has been edited by dk01 (edited 02-04-2002).]

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 02-04-2002 23:30

Okay, I may be twisting this more than I'm allowed to but take a look if you will please. I don't want to have a submit button, so I added the onChange handler in the select tag. Does this mean I should change the function from return true to submit()?

Running the code like it is gives me an error that 'nav.value' is null or not an object.

My script in the head looks like this:

<script language="JavaScript" type="text/JavaScript">
<!--

function pdfcheck(form) {
if (form.nav.value.indexOf('.pdf') > 1) {
location.href = 'includes/' + form.nav.value;
return false
}
else {
return true
}
}
//-->
</script>

My form looks like this:

code:
<form onSubmit="return pdfcheck(this)" method="post" action="/schedules/scheduletest.php">

<select name="nav" onChange="return pdfcheck(this)">

<option value="">More - Schedules --></option>
<option value="one.pdf">One</option>
<option value="4_one.pdf">4 One</option>
<option value="3_one.pdf">3 One</option>
<option value="2_one.pdf">2 One</option>
<option value="1_one.pdf">1 One</option>
</select>

</form>



Thanks

-Butcher-

bitdamaged
Maniac (V) Mad Scientist

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

posted posted 02-05-2002 01:08

in that case you can remove all the return stuff, that only applies wtih submit. (including in the onChange.

you may need to use form.submit();



:[ 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 02-05-2002 01:32

Thanks Bitdamaged

I've made these changes but still get the 'nav.value' null or not an object error. What am I not doing right?

function pdfcheck(form) {
if (form.nav.value.indexOf('.pdf') > 1) {
location.href = 'includes/' + form.nav.value;
}
else {
form.submit();
}
}

code:
<form method="post" action="/schedules/scheduletest.php">

<select name="nav" onChange="pdfcheck(this)">
<option value="">More - Schedules --></option>
<option value="one.pdf">One</option>



Thanks again

-Butcher-

bitdamaged
Maniac (V) Mad Scientist

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

posted posted 02-05-2002 02:18

hmm.. I think if you pu the onChange in the select then the "this" is reffering to the select instead of the form which is making things difficult.




:[ 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 02-05-2002 02:31

What if I do onChange="submit()" in the select and then do onSubmit="return pdfcheck(form)" in the form tag?

Or is there another way to handle this without a submit button?

-Butcher-

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 02-05-2002 08:07

Here you go...

function pdfcheck(field) {
&nbsp;&nbsp;&nbsp;&nbsp;if (field.value.indexOf('.pdf') > 1)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;location.href = 'includes/' + field.value;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.navForm.submit();
&nbsp;&nbsp;&nbsp;&nbsp;}
}

<FORM METHOD="post" ACTION="/schedules/scheduletest.php" NAME="navForm">
<SELECT ONCHANGE="pdfcheck(this)">
<OPTION VALUE="">More - Schedules --></OPTION>
<OPTION VALUE="one.pdf">One</OPTION>
</SELECT>
</FORM>

BTW Notice that your <FORM> needs to have a name in order to submit it from pdfcheck() function...


butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 02-05-2002 13:58

Thanks mr.maX and Bitdamaged!!

It works great.

I swear I'm going to learn JavaScript if it kills me... before you guys do for asking so many questions.

-Butcher-

[This message has been edited by butcher (edited 02-05-2002).]

« BackwardsOnwards »

Show Forum Drop Down Menu