views:

317

answers:

5

Hello, 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!

+2  A: 
onclick="document.getElementById('mytextbox').value = '';"
webbiedave
Note, Idealflip, that this is javascript, not PHP.
Am I able to use onclick within a <?php ?> block?
Idealflip
@Idealflip: This is being done through javascript, a client-side technology. Check out basic PHP tutorials on how to embed JavaScript and HTML in your PHP pages.
webbiedave
+2  A: 
ovais.tariq
Not exactly what I did, but very similar. The checking of Post back was the kicker.
Idealflip
thanks for marking my answer as correct
ovais.tariq
A: 

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.

favo
A: 

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.

R. Bemrose
+1  A: 

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 comment 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;" />
Josh
I added the JavaScript tag because he stated "is there a way to remove text from a text box using a submit button"
webbiedave
@webbiedave: AH HA! It all becomes clear now ;-)
Josh
@josh: lol.....
webbiedave