jeff5656 Posted August 15, 2009 Share Posted August 15, 2009 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 https://forums.phpfreaks.com/topic/170401-solved-echo-a-variable-in-a-form-based-on-user-selection/ Share on other sites More sharing options...
smerny Posted August 15, 2009 Share Posted August 15, 2009 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 https://forums.phpfreaks.com/topic/170401-solved-echo-a-variable-in-a-form-based-on-user-selection/#findComment-898901 Share on other sites More sharing options...
jeff5656 Posted August 15, 2009 Author Share Posted August 15, 2009 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 https://forums.phpfreaks.com/topic/170401-solved-echo-a-variable-in-a-form-based-on-user-selection/#findComment-898909 Share on other sites More sharing options...
smerny Posted August 15, 2009 Share Posted August 15, 2009 I'm not the best with JavaScript, but try.. button.value = "Contact " + selectMenu.options[selectMenu.selectedIndex].text Link to comment https://forums.phpfreaks.com/topic/170401-solved-echo-a-variable-in-a-form-based-on-user-selection/#findComment-898911 Share on other sites More sharing options...
jeff5656 Posted August 15, 2009 Author Share Posted August 15, 2009 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 https://forums.phpfreaks.com/topic/170401-solved-echo-a-variable-in-a-form-based-on-user-selection/#findComment-898920 Share on other sites More sharing options...
jeff5656 Posted August 15, 2009 Author Share Posted August 15, 2009 button.value = "Contact " + selectMenu.options[selectMenu.selectedIndex].text I put that in the <script> section in the <head> and it works. Thanks! Link to comment https://forums.phpfreaks.com/topic/170401-solved-echo-a-variable-in-a-form-based-on-user-selection/#findComment-898926 Share on other sites More sharing options...
smerny Posted August 15, 2009 Share Posted August 15, 2009 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 https://forums.phpfreaks.com/topic/170401-solved-echo-a-variable-in-a-form-based-on-user-selection/#findComment-898933 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.