0

I have a HTML text that let user to enter value for searching ID.

<input name="sname" type="text" id="sname" value="<?=$sname?>">

I'm now adding a HTML button to set the value of the text to blank after clicking.

<input type="submit" name="bth_clear" id="bth_clear" value="Clear">

I did it in the php but it doesn't work. The value entered is still there. Any ways that I can do ?

<?php   

if ($bth_clear) {
    $sname=""; 
}
?>
hatched
  • 795
  • 2
  • 9
  • 34
  • 3
    Shouldn't it be `if (isset($_POST['btn_clear']))` (assuming you're using `action="post"` in your form)? PHP's [_register globals_](http://php.net/manual/security.globals.php) went away a long time ago – Phil Aug 07 '18 at 07:10
  • 1
    I think you should consider doing that in JavaScript or jQuery. From the PHP perspective you will do this on the server side, the client side will not be affected by this button unless the screen is refreshed. – Rafael Aug 07 '18 at 07:14
  • 1
    @RafaelBoszko I think it's safe to assume the PHP script renders the HTML page and OP is doing a normal form submit. Where else would `value="=$sname?>"` come from? – Phil Aug 07 '18 at 07:15
  • 2
    @RafaelBoszko the screen is refreshed everytime the button is clicked – hatched Aug 07 '18 at 07:15
  • @Phil `if (isset($_POST['btn_clear']))` won't work, my entire query went blank – hatched Aug 07 '18 at 07:16
  • @hatched That is meant to be in place of `if ($btn_clear)`. Please update the code in your question if yours has changed. Also, I see no _"query"_ at all so have no idea what you're talking about. – Phil Aug 07 '18 at 07:17
  • 2
    fyi, it's `bth_clear`, not `btn_clear` – brombeer Aug 07 '18 at 07:20
  • @kerbholz thanks, I fail at reading comprehension. OP, replace `if ($bth_clear)` with `if (isset($_POST['bth_clear']))` if it's a POST form, or `if (isset($_GET['bth_clear']))` otherwise – Phil Aug 07 '18 at 07:21

2 Answers2

-1

You want to use a function that loads variables when you load the view.

Look here for the second answer PHP pass variable to include

How to use it ?

if (isset($_POST['bth_clear'])) {

     $name = isset($_POST['sname']) && is_string($_POST['sname']) ? $_POST['sname'] : null;

     includeWithVariables('view/MyView.php', array('sname' => $name ));
}

And in the view

<input name="sname" type="text" id="sname" value="<?php echo isset($sname) ? $sname: null?>">

Like this you can save the data from the input and pass it back to the user in the same form that he sent or in another form .

Also using this way you don't have to include any PHP file in your View

O.Rares
  • 1,031
  • 1
  • 16
  • 19
-2

you can do it with jquery

   <script>$(document).ready(function(){var bth_clear = $("#bth_clear").val();if(bth_clear.length > 0){$("#bth_clear").val('');}});</script>
sam
  • 47
  • 5