Jump to content

[SOLVED] Selector changes Text field value


Fabis94

Recommended Posts

Ok this is my form:

 

  		echo '<form name="myform" action="newpage.php" method="post"><b>Create a new page - Enter its name (without .php):</b> ';
  		echo '<input name="phpname2" type="text" size="15" maxlength="10" />';
	echo '<br /><b>Write in the folder where it should be saved*:</b> <input name="folder_location2" type="text" value="/" size="15"><br>';
	echo '<select name="howtowrite" id="howtowrite" onClick="whichTest()"><option selected>New</option><option>Quest</option><option>Skill</option></select>';
	echo '<br>*Root of the server is "/" so if you want to save it on a folder called PHP, write "/PHP/".';
	echo ' Always add the end-slash.<br><br><br><br>';
	echo '<center><b>Write in the contents of the file:</b> ';
	echo '<textarea name="page_contents2" cols="60" rows="10"></textarea>'; //' . $htmlstart . '
	echo '<br><input name="second_method" type="submit" /></center>';

 

(dont mind the Echos) I'm using it in a PHP script. I want howtowrite selector to change the textarea page_contents2 value. So for example if howtowrite is New the page_contents2 value becomes "New" etc. At howtowrite i already added onClick="whichTest()", but i am a total noob at Javascript i don't know what to do now. I made this function, but it doesn't work (obviously):

 

  <script language="JavaScript" type="text/javascript">
  function whichTest() {
  var n = obj.SelectedIndex;
  var val = obj[n].text;
  switch (document.myform.howtowrite.text) {
	  case "New":
	  	document.myform.page_contents2.value = '1';
		break;
	  case "Quest":
	  	document.myform.page_contents2.value = '2';
		break;
	  case "Skill":
	    document.myform.page_contents2.value = '3';
		break;
  }
  }
  </script>

Link to comment
Share on other sites

Don't tell me no one knows what's wrong. This looks so easy! 8 views for the whole day and no one has bothered to even post.

 

Right, and posting that comment really makes me want to help now!

 

If it looks so easy, why can you not do it?

 

For a helping hand since I bothered posting. Where is obj defined? My bet is, that is causing an undefined object error and causing the switch to not run. I would use Firefox for debugging JavaScript as under Tools > Error Console it will show JavaScript errors.

Link to comment
Share on other sites

I'm sorry just i've been trying to fix this for the whole day and if i don't fix that i'm stuck. Anyways i don't know about the obj because i just followed a tutorial (that was wrong obviously) and i don't understand a thing about this. So it would be better if you could show me how to make it work because i have no idea.

Link to comment
Share on other sites

What Premiso said, your attitude is not helping in convincing people to provide you with aid.

 

 

As for why your function isn't working, he's right again. You're trying to access "SelectedIndex" from obj.. but obj isn't declared as a variable anywhere.

 

Try the following instead:

      echo '<form name="myform" action="newpage.php" method="post"><b>Create a new page - Enter its name (without .php):</b> ';
      echo '<input name="phpname2" type="text" size="15" maxlength="10" />';
      echo '<br /><b>Write in the folder where it should be saved*:</b> <input name="folder_location2" type="text" value="/" size="15"><br>';
      echo '<select name="howtowrite" id="howtowrite" onClick="whichTest( this, \'txtArea\' )"><option selected>New</option><option>Quest</option><option>Skill</option></select>';
      echo '<br>*Root of the server is "/" so if you want to save it on a folder called PHP, write "/PHP/".';
      echo ' Always add the end-slash.<br><br><br><br>';
      echo '<center><b>Write in the contents of the file:</b> ';
      echo '<textarea name="page_contents2" cols="60" rows="10" id="txtArea"></textarea>'; //' . $htmlstart . '
      echo '<br><input name="second_method" type="submit" /></center>';

 

  <script language="JavaScript" type="text/javascript">
  function whichTest( select, id ) {
     var textarea = document.getElementById( id );

     switch ( select.value ) {
        case "New":
           textarea.value = '1';
         break;
        case "Quest":
           textarea.value = '2';
         break;
        case "Skill":
          textarea.value = '3';
         break;
     }
  }
  </script>

Changes made are in the parameters given to the function, as well in how the elements are accessed.

The first parameter this is a self reference to the select element.

The second parameter is a string with the ID of the textarea, as you can see I've added id="txtArea" to the textarea.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.