is there a way to remove text from a text box using a submit button?
I don't want to delete text from all boxes on the form.
And I don't want to delete text 'onclick', for the element.
I need the code to clear the text box, to initiate from within a block.
For example
<input name="text" value="submit" />
<input type="sumbit"/>
<?php
if($_POST['submit'] == "submit")
{
//clear text box with name "text"
}
?>
Thanks for your help!
is there a way to remove text from a text box using a submit button?
I don't want to delete text from all boxes on the form.
And I don't want to delete text 'onclick', for the element.
I need the code to clear the text box, to initiate from within a block.
For example
<input name="text" value="submit" />
<input type="sumbit"/>
<?php
if($_POST['submit'] == "submit")
{
//clear text box with name "text"
}
?>
Thanks for your help!
Share Improve this question edited May 21, 2010 at 19:45 Since_2008 asked May 21, 2010 at 19:32 Since_2008Since_2008 2,3418 gold badges38 silver badges69 bronze badges 1- 5 You could prevent the text within the box from being submitted to the database with php, but you can't do anything client-side with it*. JavaScript would allow this, but we'd need to see some code to offer a specific solution/idea. (* except such actions as can be effected via Ajax, or Ajax-like approaches). – David Thomas Commented May 21, 2010 at 19:36
5 Answers
Reset to default 2You can do it in two ways:
using onclick handler of the submit button in javascript
using php
if(count($_POST) > 0) { $_REQUEST['text'] = $_GET['text'] = $_POST['text'] = ''; }
onclick="document.getElementById('mytextbox').value = '';"
Your question is somewhat vague... You tagged your question with [javascript]
yet you don't want to use onclick
, and you mentioned PHP in the title.
Your ment to @webbiedave, "Am I able to use onclick within a block?", suggests that you want this to happen on the client side, but PHP only runs on the server side.
If you edit your question and post the code you're using, I can be of much more help. But there's two basic ways of doing this. Using PHP alone (server side), it would look like:
<?php
$textbox_value = 'submit';
if(isset($_POST['clear']))
{
$textbox_value = '';
}
?>
<input name="text" value="<?php ech $textbox_value; ?>" />
<input type="sumbmit" name="clear"/>
The second way is to use javascript, and would look more like:
<input name="text" value="some value" id="someUniqueIDYouMakeUp" />
<input type="sumbmit" name="clear" onclick="document.getElementById('someUniqueIDYouMakeUp').value='';return false;" />
You can not empty a previous output using PHP only.
Instead you can use PHP to control your output:
Modify your code like this:
<?php
if($_POST['submit'] == "submit") {
//clear text box with name "text"
echo '<input name="text" value="" />';
}
else {
echo '<input name="text" value="submit" />';
}
?>
This will print your input field with a prefilled value of "submit" by default. If you submit the form with that input field, it will be prefilled with an empty string.
Simply put, you can't. PHP is a server-side language. By the time the user receives the page, all the PHP code on it has executed.
If your current code had a form element, it would submit it to that page... which, if it was the same page, would clear the text box anyway... values in text boxes do not automatically persist after submits.
You need to use a client-side language to be manipulate the page after it has been sent to the user. This includes to clear textboxes.
Currently, your only real choice for this is JavaScript.