PDA

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



magictouch
05-07-2016, 02:18 AM
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.



<!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.

jscheuer1
05-07-2016, 04:00 AM
I'm not sure fully of the solution yet. That said, this can never be done this way (highlighted part):


<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:


document.getElementById('slct2').src = 'formulaire.php';

If you need more help, just let us know.

jscheuer1
05-07-2016, 04:16 AM
Not optimal, but probably instructive for you (see also my previous post in this thread, right above here), and should work:


<!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.

magictouch
05-08-2016, 01:26 AM
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!

jscheuer1
05-08-2016, 01:47 AM
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.