Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User Input in forms

Status
Not open for further replies.

silentq

Programmer
Sep 17, 2007
16
CA
Hi,
I'm trying to make a function where a user input in a form changes the length of a string. I would do it this way in PHP:
Code:
<?
$width =   $_POST['length'];
.
.
.
.?>
<form>
 <input name ="width" type="text" value="<? echo $length; ?>">
</form>
I want to do it in JS so that my forms are client side. Any Help??
 
the brief example you provided does not change the length of anything, all it does is output this:

Code:
<form>
 <input name ="width" type="text" value="">
</form>

So, maybe you could describe what you'd like to do...?



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I'n not really sure what you want to do, but the variables in your code have two different names:

Code:
<?
[red]$width[/red] =   $_POST['length'];
?>

<form>
 <input name ="width" type="text" value="<? echo [red][b]$length[/b][/red]; ?>">
</form>

If your retrieving the width value from the form, just change the name of the second var to $width and it will work:

Code:
<?
$width =   $_POST['length'];
?>

<form>
 <input name ="width" type="text" value="<? echo [green]$width[/green]; ?>">
</form>

/michel

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots.

So far, the Universe is winning.
 
Sorry for being so unclear. what I want to know is, how to retrieve data that is input by a user in a form. So, say the length of a sentence is 49 characters, and user inputs '7' into the length field. The sentence now becomes only seven characters long.
 
Still unclear on what you are trying to accomplish, but hope the example below helps.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
<script language="JavaScript">
	var mysentence = "this sentence is forty nine characters long......";
	function mysetenceSub(myinput){
		num = parseInt(myinput);
		alert(mysentence.substring(0,num));
	}
</script>
<body onload="">
<form name="myform">
	<input type="text" id="myinput" value="">
	<button onclick="mysetenceSub(document.getElementById('myinput').value);">test</button>
</form>
 </body>
</HTML>
 
Here is the code I have. Does this make sense?
Code:
<body>


<script language="javascript">
	
	
	 uInput = elem.value;
	 
	 sentence = "This is a Test sentence whose length will change "
document.write(sentence); document.write("</p>");

document.write("Test Sentence Length: "  + sentence.length  );

function chnageLen() {
	if(uInput.length <= sentence.length)
		sentence.length = (sentence.length - uInput) }


</script>

<form>
length:<input type="text" id="uInput"/>
<input type="button" value="Change Length" onclick="changeLen()" />
</form>
</body>
 
it sort of makes sense but you should prolly look at my example and use the Javascript built in string functions obtain a substring. Also
Code:
uInput = elem.value;
I don't see how this could ever work unless you are prototyping objects in which case should know how to use string functions.
 
Well thanks for your help. I'll play around with your code to get it to do exactly what I want. Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top