Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I've been looking for 5 hours and I can't find a good answer to this. I will give you an example:

PHP
<?php
$usrtext = $_POST['usrtext'];
echo $usrtext;
?>


HTML
<form action="test.php" method="POST";
<input type="text" name="usrtext"  önKeyUp="this.form.submit();" />
</form>


So all I have on the page test.php is a textform and when a user types in the text field I wan't the same text to echo out. I wan't to be controlling what the user types as (s)he's typing.
I've done okey so far and succeded to make the form autosubmit when user types. But this have downsides as well. When the form submits it:
1. Leaves the form unselected
2. Leaves the form blank. Of course this is not really a problem since this can be escaped by adding: value="" to the input tag.

So how would you guys do to make this work perfectly? Maybe I shouldn't autosubmit the form, maybe there's a better way? All I know good so far is PHP and HTML, just starting with javascript a few days ago. Hopefully the answer won't be too advanced for me...

Thanks in advance!
Posted
Comments
Uday P.Singh 1-Jan-12 5:09am    
why you want to auto-submit the form? you can easily echo the same name entered in textbox on the same page without auto-submit.
siXor 1-Jan-12 5:44am    
What do you mean? It's just an example. I wan't to control the text while user's writing. If I'm being stupid and you actually have a solution, please post it here :-)

The best way is using AJAX.
and JQuery could be help to use Ajax
 
Share this answer
 
Comments
siXor 1-Jan-12 5:48am    
Is that the only way, or is there maybe a simplier solution?
XML
XMLHttpRequest is another way use javascript it is a dom object.
or you can use a Iframe to post your data.
<iframe id='myiframe' style=display:none ></iframe>
<script  >
function callmeonkeyup()
{
myiframe.src = 'somepage.php?data='+whatistyped;
}
</script>

but consider whether or not it is supported by the broweser.
 
Share this answer
 
Comments
siXor 1-Jan-12 19:02pm    
I'm trying this but can't get it to work. I think there's something wrong wrong with the syntax in the javascript.

Edit: Found the fault. It shouldn't be "myiframe.src", but "document.getElementById('myiframe').src". I'll keep testing...
siXor 1-Jan-12 19:51pm    
So now I have been testing this for a while and I've made progress I guess.

test.php:
<input type="text" name="usrtext" önKeyUp="callmeonkeyup()" />
<script type="text/javascript">
function callmeonkeyup(){
document.getElementById('myiframe').src = 'test2.php?data=Helllo!!';
}
</script>
<iframe id='myiframe' />

test2.php:


The result is that when I start typing in the text-box a message appears inside the frame. The problem is that I still can't send over the information from the text-box to test2.php. So my question is what should I write instead of +whatistyped ?
Jamal Seyedi 2-Jan-12 5:20am    
test.php:
<input id="usrtext" type="text" name="usrtext" önKeyUp="callmeonkeyup()" />
<script type="text/javascript">
function callmeonkeyup(){
var text = document.getElementById('usrtext').value
document.getElementById('myiframe').src = 'test2.php?data=' + text;
}
</script>
siXor 2-Jan-12 21:54pm    
I thought that would work as well, but it didn't. Does anoynone know what's wrong?
siXor 3-Jan-12 20:45pm    
Or maybe someone could just try the code so that I know it's not just me?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900