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!

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

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

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);" >

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

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.