Set placeholder with PHP variable

Asked

Viewed 1,964 times

-3

I have a Registration/Search Form and now I’m adding the option to edit the fields. To keep the employee from getting confused, I wanted to set the placeholder with a PHP variable, through the echo. It is possible to do this?

<div class="inputs">
    <?php if ($id != '') { ?>
        <input type="hidden" name="id" value="<?php echo $id; ?>" />ID: <?php echo $id; ?>
    <?php } ?> 
    <?php echo $cliente; ?>
    <input type="text" name="cliente" maxlength="50" placeholder="<?php echo $cliente; ?>" value="<?php echo $cliente; ?>"/><br/>
    <input type="text" name="produto" maxlength="25" placeholder="PRODUTO" value="<?php echo $produto; ?>"/><br>
    <input type="text" name="solicitante" maxlength="50" placeholder="SOLICITANTE" value="<?php echo $solicitante; ?>"/><br/>
    <input type="text" name="status" maxlength="25" placeholder="STATUS" value="<?php echo $status; ?>"/><br/>
    <input type="text" name="incluiu" maxlength="25" placeholder="INCLUIU" value="<?php echo $incluiu; ?>"/><br/>
    <input id="submit" class="button_text" type="submit" name="submit" value="Cadastrar" />
</div>
  • 2

    Thiago the question is unclear... Want to do this in PHP to be applied to an HTML element? What kind of element is? an input? You can attach an example of the code you have and how it is presented to the official

  • 1

    You either set or pick up the placeholder?

  • Sorry for the unclear question, I updated the question with my code, I tried to echo the placeholder but it came out blank.

  • Earendul, search my bank.

  • Okay, thank you very much for all the answers ;)

  • 1

    If what you want is a way to reset the value to a previous value, the path is to use the field value and the method reset of the form, or a data-Parameter. (or even Labels even)

  • I understood, I asked if it was possible to do this and the answer was yes. thank you all for the answers.

Show 2 more comments

4 answers

6

It is possible yes:

<input type="text" placehold="<?php  echo 'palavra'; ?>" />
  • I’ve tried this way and result comes blank.

  • Probably the variable $cliente is coming in white. The value is also blank?

  • No, the value is correct.

3

It only makes sense to use this if the variable is different from the field, because the placeholder only appears with empty field. And the way you’re doing, the placeholder will only be used empty anyway. Probably would have to be done something like this:

<input type="text" name="cliente" maxlength="50" placeholder="<?php echo 'CLIENTE'; ?>"
   value="<?php echo $cliente; ?>"/><br/>

Or so:

$pla_cliente = 'Cliente';
... seu código PHP ...

<input type="text" name="cliente" maxlength="50" placeholder="<?php echo $pla_cliente; ?>"
   value="<?php echo $cliente; ?>"/><br/>

but always using different variables for the placeholder and to the countryside.


If you are trying to use the value for a reset of the fields, just use the reset of own form to return the previous value

  • Not necessarily. Goes that he wants to leave the placeholder being the user name, to be easy for him to understand if he deletes or wants to change the value. The problem is, he didn’t make his goal very clear, so there’s no way we could set a good example.

  • I want to do an UPDATE in the bank and I want the placeholder to be the value I intend to change.

  • @Thiago has no problem where you will pick up the placeholder. It just can’t be the same value as the field, otherwise it doesn’t make any sense.

  • Rodrigo Coelho, that’s exactly it.

  • Thiago, the placeholder is just a display for the user to identify what can be typed in the field. It will not influence anything in the POST form.

  • So it is not possible to redeem the previous value?

  • 4

    The placeholder is the display value when the field is empty, nothing less. If you want to use the value for a form reset, either use value, or use a custom data-Parameter.

  • I have a form that was filled by a user, eventually, you need to make certain changes to this form, this is already done. works perfectly.

  • What I want to do now is this: .

Show 4 more comments

1

With Javascript you can do so:

var placeholder = document.getElementById("entrada").getAttribute("placeholder");
alert(placeholder);
<input type="text" placeholder="teste" id="entrada">

  • Yet it comes in white.

1

The amendment I suggest is as follows::

<input type="text" name="cliente" maxlength="50" placeholder="<?php echo $cliente; ?>" value="<?php echo $cliente; ?>"/><br/>

Realize that you are setting the placeholder and then replacing it with value

in case you remove value and you will see that the placeholder is working properly.

<input type="text" name="cliente" maxlength="50" placeholder="<?=
$cliente; ?>"/><br/>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.