Jump to content

If .. else if statement.. urgent help!


Elmacik
 Share

Recommended Posts

i have for example 3 of forms. all of them have their own actions.for example i will use these forms to upload files to different sites of mine. here is a code example:

<form name="form1" action="http://something" method="post"><input type="file" name="site1" /><input type="submit" value="Upload!" /></form>-------------------------------------------------------------------------------------------<form name="form2" action="http://someotherthing" method="post"><input type="file" name="site2" /><input type="submit" value="Upload!" /></form>--------------------------------------------------------------------------------------------<form name="form3" action="http://differentthing" method="post"><input type="file" name="site3" /><input type="submit" value="Upload!" /></form>

now, i want to make this sites appear in a select box. for example:

<form name="combined" action=""><input type="file" name="file what to name this??? 3 different names above(!)" /><select name="selecting"><option value="">select the server</option><option value="form_1">upload to site 1</option><option value="form_2">upload to site 2</option><option value="form_3">upload to site 3</option></select><input type="submit (or should use button? )" onclick="if (this.selectedIndex = 0) { alert('you didnt select a server!') } elseif (this.form.file.value = 0) { alert('you didnt select a file to upload!') } elseif (this.options[this.selectedIndex].value = form_1) { here goes the code to submit form 1 } elseif (this.options[this.selectedIndex].value = form_2) { here goes the code to submit form 2 } elseif (this.options[this.selectedIndex].value = form_3) { here goes the code to submit form 3 }" /></form>

my knowledge about javascript approaches to zero.i read the tutorials in w3school (if.. elseif statements)but it was very simple to meet my question.anybody? please help its really needed

Link to comment
Share on other sites

<form name="combined" action=""><input type="file" name="file what to name this??? 3 different names above(!)" /><select name="selecting"><option value="">select the server</option><option value="form_1">upload to site 1</option><option value="form_2">upload to site 2</option><option value="form_3">upload to site 3</option></select><input type="submit (or should use button? )" onclick="if (this.selectedIndex = 0) { alert('you didnt select a server!') } elseif (this.form.file.value = 0) { alert('you didnt select a file to upload!') } elseif (this.options[this.selectedIndex].value = form_1) { here goes the code to submit form 1 } elseif (this.options[this.selectedIndex].value = form_2) { here goes the code to submit form 2 } elseif (this.options[this.selectedIndex].value = form_3) { here goes the code to submit form 3 }" /></form>

What the heck?
You put WAY too much javascript inside the events of the button/whatever, you have to write a function. Explain better what you're trying to do, and i'll help out.
Link to comment
Share on other sites

first, thanks a lot for caring.now, i summerize,i have 3 of sites to upload and host files in.i want to make a select box to let me choose which site to be used to upload onto.the form will consist of three parts.one is the file browsing, second is the select box that will contain three sites, and one button to make the upload action...

Link to comment
Share on other sites

Hmm...so let me understand this.All you want is a select box with a file box and a submit button, and when the user selects a file and selects a site and clicks the submit button, take the user to the site? If that's what you want, i can do it. If you want the location of the file to be taken to the site..... :ph34r:

Link to comment
Share on other sites

First, as Chocolate570 said, use a function. to do so, open the tags <script> </script> with a content like this:

<script type="text/javascript">function YourFunctionName(target) {  if (target.selectedIndex = 0) {    alert('you didnt select a server!')  } else if (target.form.file.value = 0) {    alert('you didnt select a file to upload!')  } else if (target.options[target.selectedIndex].value = form_1) {    here goes the code to submit form 1  } else if (target.options[target.selectedIndex].value = form_2) {    here goes the code to submit form 2  } else if (target.options[target.selectedIndex].value = form_3) {    here goes the code to submit form 3  }}</script>

As you can see, i changed little in your code, put it in a function and add some tabulation. Much easier to read now, dont you think?Now, in your 'onclick' event, you should put something like onclick="YourFunctionName(this)". Notice that the 'this' is sent to the function, that calls it 'target', and use 'target' instead of 'this'. Another change is the 'elseif'...javascript dont know what it means, whe just know 'else' and 'if', so you can use it in the same context as 'else if' :)

Link to comment
Share on other sites

Second, I suggest you use Switch...Case instead of If...Else If. The 'Switch' controll verify a variable value (or a property of an object, like a 'select' value) and specify a set of actions to do in each case (specified values). Here how it will look like:

<script type="text/javascript">function Send2Server() {  Switch(document.getElementById("selecting").value) {    case "form_1":      //Here comes the code to upload to server 1      break;    case "form_2":      //Here comes the code to upload to server 2      break;    case "form_3":      //Here comes the code to upload to server 3      break;  }}</script><form name="combined" action="">  <input type="file" name="filename" id="filename" />  <select name="selecting" id="selecting">    <option value="">select the server</option>    <option value="form_1">upload to site 1</option>    <option value="form_2">upload to site 2</option>    <option value="form_3">upload to site 3</option>  </select>  <input type="button" value="Upload!" onclick="Send2Server()" /></form>

hope I helped you solve your problem :)

Link to comment
Share on other sites

muchas gracias :)thanks a lot MadPotato..i couldnt make it work though. i am really very bad at coding.in your opinion, in the last examples you gave, what should i use for here in example 1 of yours: here goes the code to submit form 1and in here, in example 2 of yours? ://Here comes the code to upload to server 1

Link to comment
Share on other sites

lol

(yes, sites have upload actions)
i thought you already had the code to upload files...well, thats a little harder to do, then i'll send you an example and you try to implement it by yourself, ok? If you got trouble, just go back here and scream :)save this content as "index.php"
<form enctype="multipart/form-data" action="uploaded.php" method="post"><input type="hidden" name="MAX_FILE_SIZE" value="3000000" />Send this file: <input name="userfile" type="file" /><input type="submit" value="Send File" /></form>

save this content as "uploaded.php

<?php// In versions of php < 4.1.0, use $HTTP_POST_FILES// isntead of $_FILES.$uploaddir = 'C:/apache/htdocs/misc/upload/';$uploadfile = $uploaddir . $_FILES['userfile']['name'];print "<pre>";if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $_FILES['userfile']['name'])) {    print "File is valid and has been uplead sucessful. Here some info:\n";    print_r($_FILES);} else {    print "Oh Oh! Something is wrong here...dont try anything funny lammy mother######er! Some info:\n";    print_r($_FILES);}print "</pre>";?>

int the "if", if you cant move the file, maybe the user is trying a "upload attack"...but we are smart guys and won't let them in! :(hope I helpedPS.: Brazilians speak Portuguese, not Spanish! :)

Link to comment
Share on other sites

hmm... well i am neither Brazilian, nor Portugese or nor Spanish :)MadPotato, thanks a lot for your help, but my head really went rubbish.I confused everything.Of course i have the form codes. (like index.php of your example)but this is the point i cant figure out,there will be three of forms. all they have different actions and different file name attributes (<input type=file name=something>)so, using one form IMO wont work for all three of them as they wont accept the same name attribute.

Link to comment
Share on other sites

i'm not sure if I understood your doubt, but if you have 3 servers to send the files, the 'action=uploaded.php' should link to 3 diferent servers (like 'www.server1.com/uploaded.php', 'www.server2.com/uploaded.php'...) , and the 'name' really doesnt matter, if you change it in both files...put a 'uploaded.php' in each server...got it?

Link to comment
Share on other sites

well then i must say we deviated out of our aim.my aim was to put the three sites in a select box.its of course very easy to write all of them a seperate form script. :)and since action will act for example like this: /upload.php?file=archive.zipnames matter i think

Link to comment
Share on other sites

the name matters of course, but if you change it in both files it will workand the file name wont appear in the url, cuz you're using "post", not "get" methodand sorry for my mistake, i can hardly understand what you talk in english :), but i'll give you a suggestion this way:i dont know if you can just 'move' a file throught servers, i guess not...then, you think that is acceptable an intermediate page? something like:Selected file: "your_file_name.zip"Confirm Upload? [Yes]

Link to comment
Share on other sites

if you do, you can write a form with PHP, with the 'action' that you want...if not, i have another suggestion...when the guy select the item, you refresh the page, with an GET argument (like index.php?server=1), so you have the chance to re-write the action with PHP...the big problem is: the <input type="file"> isn't a common input, the string isnt all that matters...i dont understand it very well and maybe i'm wrong, but take a look in that part of the code:$uploadfile = $uploaddir . $_FILES['userfile']['name'];if it was a simple text box, you could use $_GET["userfile"] or $_POST["userfile"], but that $_FILE seems quite 'specific'...i'm pertty sure there are another ways to do it, but i just cant figure out...if i come to figure it out i'll tell you

Link to comment
Share on other sites

thanks a lot MadPotato.and sorry for my english. its been 6 years finished the course and as a result, i forgot nearly everything.let me turn back to the Post #7 and Post #8 that are sent by you.can we use this:document.forms.form1name.submit();to submit a form?and can i attach two functions to one button? (offtopic question)and take it easy if we cant figure out, thanks for the effort.. thanks a loteditjust for a referance,i learn from some friend, he used vars and getElementById to chage the file name attributes and forum actions for each form. it works very well.thanks for your effort too.and the above 2 questions are still alive and unanswered :)

Link to comment
Share on other sites

yup, you can. Suppose you had:<input type="button" value="function test" onclick="dothisthing();dothisecondthing()">That would work. Unless it's supposed to be a semicolon.... uhhh.... ok, here's an example.

<head><script type="text/javascript">function dothisthing(){alert("First thing done,")}function dothisecondthing(){alert("Second thing done")}</script></head><body><input type="button" value="function test" onclick="dothisthing();dothisecondthing()"></body>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...