Results 1 to 5 of 5

Thread: Trying to do form with 2 options, don't know what's wrong in the code(I'm a begginer)

  1. #1
    Join Date
    May 2016
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Trying to do form with 2 options, don't know what's wrong in the code(I'm a begginer)

    Hi, I'm a beginner and autodidact in programming so need your patience because for me it is still hard to understand many things...
    I'm trying to do a form with 2 option, if the person choose "Massage" I what it to open one file and if the person choose the option "Rencontres" I want the other file with other form to open.
    But of course the way I'm trying is not working, of course because still don' t know that much about javascript. So I have no idea where is the error and I've seen many tutorials even so I don't know what to do.
    If someone can do the charity of point what I could to to obtain the result I what I will be grateful.
    Here is my code, hope you understand what I'm saying.

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function populate(s1,s2){
    	var s1 = document.getElementById(s1);
    	var s2 = document.getElementById(s2);
    	s2.innerHTML = "";
    	if(s1.value == "Massage"){
    		var optionArray = ["formulaire.php|formulaire.php"];// I'm putting the file I want to open on the iframe here, maybe is ridiculous what I did?
    	} else if(s1.value == "Rencontres"){
    		var optionArray = ["|","formulairerencontres.php|formulairerencontres.php"];
    	} 
    	for(var option in optionArray){
    		var pair = optionArray[option].split("|");
    		var newOption = document.createElement("option");
    		newOption.value = pair[0];
    		newOption.innerHTML = pair[1];
    		s2.options.add(newOption);
    	}
    }
    </script>
    </head>
    <body>
    <h2>Choisi oł tu veux publier ton annonce:</h2>
    <hr />
    Choose Car Make:
    <select id="slct1" name="slct1" onchange="populate(this.id,'slct2')">
      <option value=""></option>
      <option value="Massage">Massage</option>
      <option value="Rencontres">Rencontres</option>
     
    </select>
    
    <hr />
    <iframe id="slct2" name="slct2" width="560" height="315" src="function populate(s1,s2)" frameborder="0"></iframe>// I wanted to open the files formulaire.php and formulairerencontres.php here, Something is wrong here but I have no idea what.
    <hr>
    
    </body></html>
    To be honest I even don't know if I can do what I want with this code, but I'm open to learn also please help me, it is not ease learn alone only withe the tutorials and with not much time.
    Thanks, and hope have answers.
    Last edited by jscheuer1; 05-07-2016 at 03:40 AM. Reason: format code, spelling, English usage

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm not sure fully of the solution yet. That said, this can never be done this way (highlighted part):

    Code:
    <iframe id="slct2" name="slct2" width="560" height="315" src="function populate(s1,s2)" frameborder="0"></iframe>// I wanted to open the files formulaire.php and formulairerencontres.php here, Something is wrong here but I have no idea what.
    That never works. The src attribute of an iframe must be a string, not a function. You can run a function and/or some code someplace else and use that to set the src attribute of an iframe to one or another of two or more strings as long as the iframe has already been parsed by the browser. Most likely this will be the resolution of your issue, or at least play a major part in it.

    Like:

    Code:
    document.getElementById('slct2').src = 'formulaire.php';
    If you need more help, just let us know.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    magictouch (05-08-2016)

  4. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Not optimal, but probably instructive for you (see also my previous post in this thread, right above here), and should work:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function populate(s1, s2){
    	s1 = s1.value, s2 = document.getElementById(s2);
    	if(s1 === "Massage"){
    		s2.src = "formulaire.php";
    	} else if(s1 === "Rencontres"){
    		s2.src = "formulairerencontres.php";
    	}
    }
    </script>
    </head>
    <body>
    <h2>Choisi oł tu veux publier ton annonce:</h2>
    <hr />
    Choose Car Make:
    <select id="slct1" name="slct1" onchange="populate(this,'slct2');">
      <option value=""></option>
      <option value="Massage">Massage</option>
      <option value="Rencontres">Rencontres</option>
     
    </select>
    
    <hr />
    <iframe id="slct2" name="slct2" width="560" height="315" src="about:blank" frameborder="0"></iframe>
    <hr>
    
    </body></html>
    Any more questions, feel free to ask.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following User Says Thank You to jscheuer1 For This Useful Post:

    magictouch (05-08-2016)

  6. #4
    Join Date
    May 2016
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thank you very much, it is working perfectly and I understand a bit more but not everything, I see I still must learn a lot!! See you soon!

  7. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You're welcome. Again, any questions, just ask. There are far fewer stupid questions than stupid answers. But I try not to be too stupid when answering (not always successful there ). And I would add in this case that, though I might be wrong, I think you are overcomplicating things in general. I still do that sometimes. The answer is usually simpler than we imagine. Just keep that in mind. Sooner or later it will come in handy.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Begginer - creating a table
    By mrabin in forum MySQL and other databases
    Replies: 2
    Last Post: 09-01-2010, 08:15 AM
  2. What is wrong with this code?
    By lindm in forum JavaScript
    Replies: 5
    Last Post: 09-14-2007, 06:28 PM
  3. what's wrong with this code?
    By fsy in forum HTML
    Replies: 1
    Last Post: 10-09-2006, 10:06 AM
  4. What 's Wrong with this Code
    By yoyali in forum ASP
    Replies: 2
    Last Post: 08-02-2006, 12:12 PM
  5. Changing form options!
    By Chopper77 in forum JavaScript
    Replies: 4
    Last Post: 11-15-2005, 12:25 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •