Jump to content

[SOLVED] echo a variable in a form based on user selection


jeff5656

Recommended Posts

Ok I am very very new to java to the point where I only know how to use pre-made javascripts in my php code so be gentle. :)

 

First, here is part of a form I have:

 

<select name = "pulmonologist" >
<option selected="" value="">---Select---
<option value="none">None</option>
<?php		
while ($row2 = mysql_fetch_assoc ($results2)) {   
        ?><option value = "<?php echo $row2['staff_name'];?>"><?php echo $row2['staff_name'];?></option>
<?php }
?>
</select>

 

 

Ok now I want to echo the value that the user selects on the SAME page  (i.e. before the submit button is pressed) (so I can't do it with php, I need javascript)  like this:

 

<input type="submit" value = "Contact <?php echo $pulmonologist;?>" />

 

Can you tell me how to do this?  Thanks!

Link to comment
Share on other sites

someone posted this:

 

http://www.webdeveloper.com/forum/showthread.php?t=171456

<script>

function changeButton(selectMenu)
{
var button = document.getElementById('mySubmitButton');
button.value = selectMenu.options[selectMenu.selectedIndex].text
}

</script>
<select name="choosefruit" onchange="changeButton(this);">
<option value=1>Apple</option>
<option value=2>Orange</option>
<option value=3>Pear</option>
</select>
<input id="mySubmitButton" name="submit" type="submit" value="click to submit a fruit">

 

you'll just have to edit the names and such

Link to comment
Share on other sites

That is great and it works.  Thank you.

Now can anyone tell me how to make the button change so that the word "Contact" always appears in the button such as

"Contact Smith"  (if Smith was a name the user selected in the dropdown).

 

The above script just changes the button value to only "Smith" (or whatever name user selected).

Link to comment
Share on other sites

I'm not the best with JavaScript, but try..

 

button.value = "Contact " + selectMenu.options[selectMenu.selectedIndex].text

 

Where do I put that code?  Inside this somewhere?

 

<input id= "mySubmitButton" type="submit"  value = "Contact"  onClick="return check('newsubmit', this.name)"/>

 

Or inside this?

 

<select name = "pulmonologist" onchange="changeButton(this);" >

Link to comment
Share on other sites

yes sorry, i meant just to replace the "button.value = selectMenu.options[selectMenu.selectedIndex].text" with it, basically adding the red part here:

 

function changeButton(selectMenu)

{

var button = document.getElementById('mySubmitButton');

button.value = "Contact " + selectMenu.options[selectMenu.selectedIndex].text

}

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.