/    Sign up×
Community /Pin to ProfileBookmark

echo a variable in a form based on user selection

Ok I am very very new to javascript 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:

{code}
<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>

{code}

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:

{code}<input type=”submit” value = “Contact <?php echo $pulmonologist;?>” />{code}

Can you tell me how to do this? Thanks!

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@jeff5656authorAug 15.2009 — I found this solution. How do I change the submit button value so it is always preceeded by "Contact" SO the button will read "Contact Smith" if the user selected smith

<script>

function changeButton(selectMenu)

{

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

button.value = selectMenu.options[selectMenu.selectedIndex].text

}

</script>

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

<option value=1>Smith</option>

<option value=2>Johnson</option>

<option value=3>Edwards</option>

</select>

<input id="mySubmitButton" name="submit" type="submit" value="click to submit a pulmonologist">
×

Success!

Help @jeff5656 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...