Jump to content

[SOLVED] Re: display a value from a form


mgs019

Recommended Posts

Hi,

 

I want to display the vale from a hidden form element as text in a document. I want it to also change as the value changes with mouseovers etc. This comes from a php generated form so I could do with it all fitting in the <a onmouseover="" part of the page.

 

I tried:

 

document.write(document.forms.myform.myinput.value) but it did not work. I also tried document.getelementbyid("mydiv").innerHTML=document.forms.myform.myinput.value that did not work either

 

I tried all sorts of wierd things. I managed it to alert the value but not write it!!

This seems like it should be really easy. Am I missing something in the getting the value part.

 

Please help this is driving me nuts!!

 

Thanks,

 

Martin

Link to comment
Share on other sites

I am years out of practice at JavaScript, but you could try document.myform.myinput.value instead of document.forms.myform.myinput.value.  I don't know if that will help or if JavaScript allows both methods, but it can't hurt to try it.

Link to comment
Share on other sites

[i tried:

<script>
document.write(document.forms['diamond'].elements['colour'].value)
</script>

 

Still no luck.

 

in the main body of the page. The page is at www.blood-diamond.co.uk/testbed.php but you have to go to www.blood-diamond.co.uk/shape2.php first and select a shape to pass the post variable. You will see I have used the Yahoo UI sliders to change the form elements values. What I want to do is to get rid of the drop down boxes and just have a string of text saying something like: You have choosen a XXX shape diamond with XXX colour, XXX clarity and XXX carats

 

I thought that maybe if just document write did not work maybe I could use an iframe and a script in the head but I am a bit of a javascript novice so did not know what would work.

Any help will be very gratefully received.

 

Martin.

 

Link to comment
Share on other sites

I'm having a similar issue. I'm trying to set a dropdown value using javascript.

 

I'm using

document.forms['filters'].select['theme'].value="Animal";

 

I've tried variations:

 document.filters.theme.selected="Animal"; 

 

At this stage, I'm just trying to get the syntax correct, hardcoding the choice "Animal" in.

 

I've managed to get the VALUE to change, within the code, but browser just displays the value briefly in the dropdown and then resets to "All". How do I change the selected object? Thanks....

 

Emma

Link to comment
Share on other sites

I have this one working I think. I have a value change from a mouseover but my script is just

onmouseover="document.forms.myform.myselect.value='myvalue'"

You can set value to one thing and display text to another too.

 

I don't know why it is going back to "all" though. Do you have another script pulling it back?

 

Martin

Link to comment
Share on other sites

Nope. No other script. Even when I remove the bit of code and just work with that alone, it behaves the same way. It's so frustrating!

 

The script below for example should just have three dropdowns. When you click Apply, "purpose" sets itself to "wholesale" (as hardcoded) for a fraction of a second, then resets itself to "All".

 

Maybe it's my browser or config files. Any ideas?

 

Emma

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<form name = "filters" method="get" action="<?=$_SERVER['PHP_SELF']?>">
		<table width="100%">
			<tr>
				<td colspan="2" width="100%"><img src = "buttons/apply_filters.gif" ></td>
			</tr>
			  <tr>
				<td width="51" scope="row">Theme</td>
				<td scope="row"><select name="theme" id="theme1">
				  <option value="1" selected="selected">All</option>
				  <option value="2">Animal</option>
				  <option value="3">Floral</option>
				  <option value="4">Leisure</option>
				  <option value="5">Travel</option>
				</select></td>
			  </tr>
			  <tr>
				<td width="51" scope="row">Message</td>
				<td scope="row"><select name="message" id="message1">
				  <option value="1" selected="selected">All</option>
				  <option value="2">Blank</option>
				  <option value="3">Birthday</option>
				  <option value="4">Congrats</option>
				  <option value="5">Get Well</option>
				  <option value="6">Thankyou</option>
				  <option value="7">Christmas</option>
				</select>
				</td>
			  </tr>
			  <tr>
				<td width="51" scope="row">Purpose</td>
				<td width="97" scope="row"><select name="purpose" id="purpose">
				  <option value="All">All</option>
				  <option value="Personal">Personal</option>
				  <option value="Invitations">Invitations</option>
				  <option value="Business">Business</option>
				  <option value="Retail">Retail</option>
				  <option value="Wholesale">Wholesale</option>
				</select></td>
			  </tr>
			  <tr ><td colspan="2" scope="row" align="right"><input type="submit" name="submit2" value="Apply" onclick="document.filters.purpose.value='Wholesale';"></td></tr>
		</table>
</form>
</body>
</html>

Link to comment
Share on other sites

Hi,

 

Thanks for the brainstorm.

 

I think <? is a shortcut to <?php - and the php is executing with the same syntax elsewhere, so I think that's ok.

 

Removing selected="selected" (as I already had done for "purpose") has no effect. I already tried it, but I just tried again now.

 

Thanks again... any other thoughts welcome...

 

If you load this into your system do you get the same problem (bit of a liberty to ask you, don't worry if you don't have time!)

Link to comment
Share on other sites

I've got It (I think).

 

You cant have an action and an onclick because the action will win (and reload your page giving you all again). Best bet is to add a button as type="button" not submit. and onclick="...

 

Martin

Link to comment
Share on other sites

Hmmm. Thanks. But I just removed the "action" line completely and the problem is still there. Dammit!

 

The $_GET variable still seems to work, though. I wonder what the action = <? $_SERVER ?> thing was meant to do - don't seem to have lost anything by deleting it!

 

Argh. Was on this till 10pm last night. I'll try the button thing too, but I suspect it's a no-go, given that the action command has been proven not to be the problem. Ta, though.

 

Emma

Link to comment
Share on other sites

Nope. No other script. Even when I remove the bit of code and just work with that alone, it behaves the same way. It's so frustrating!

 

The script below for example should just have three dropdowns. When you click Apply, "purpose" sets itself to "wholesale" (as hardcoded) for a fraction of a second, then resets itself to "All".

 

Maybe it's my browser or config files. Any ideas?

 

Emma

The action submits the form back to this page... so you change the field, and then reload the page, which has all pre-selected. 

Link to comment
Share on other sites

I still think the problem is that you are submitting the form when you do not want to. But you are still submitting the form by pressing the button. Try changing the button ftom type="submit" to type="button".

 

Whatis the overall goal in this?

 

The $_SERVER is a command that detects the page url. so the form gets the values and submits to itself. The problem is that it does not use them.

 

The thing to do is to either change the action to a second page which will process the $_GET variables.

 

Or, at the top of the page before the <HTML tag> put something to process the page. The other way is to put a line of php in the form to say

<option value="2"
<?php
if($_GET['theme'] == 2)
echo ' selected="selected"';
?>>Animal</option>

this will then keep the value selected that is passed back to the page. the only thing is that you have to do this lots of times but hey thats what copy/paste's for.

 

Martin

Link to comment
Share on other sites

OK, progress! Yes, if I make it a button, the problem goes away. But then the GET variable does not do its job, even with action=<?$_Server..." added back in. I guess this is because the submit thing has gone away.

 

So, if I want to both store the data, and keep the selections, I guess I have to try the other route. Trying it now. Will report back!

Link to comment
Share on other sites

Martin, I love you  :-*

 

Cheers. Good luck with your problem (and sorry I stole your thread). If you're still struggling in a couple of months when I actually have a handle on this, I'll be glad to help....  ;)

Link to comment
Share on other sites

Glad I could help, anyway to get back to my problem:

 

I have a form where I have a select element with numbered values which relate to diamond colours and clarities. So 1=D, 2=E etc. I also have sliders from the Yahoo UI library which work great. I want to have the value displayed but not as a select. I tried disabled="yes" and it stopped passing th evariable through.

 

It seems to work for alert but not document.write!?!?!?

 

My backup plan was to use a readonly textbox with a white border so it would not show up and use javascript to process the variables so something like

 

var colname
var colnum = document.forms.diamond.colour.value;
if(colnum=1) colname=D
...

in the head as a function, then put an onchange in the inputs.

 

any chance of a bit of help in the formatting for the function, particularly the if or maybe switch part?

 

Thanks alot,

 

Martin

Link to comment
Share on other sites

Guest
This topic is now 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.