Is there any way to pass the value of a variable to the value of an input through PHP?

5

Good, as the question shows, I wanted to know if it is possible only with PHP since I see examples but they do it with JavaScript. I would appreciate if you could give an example to take it as a reference.

                                         

    <?PHP
        if(isset($_POST['enviar'])){
            $nombre = $_POST['nombre'];
            $apellidos = $_POST['apellidos'];
            $edad = $_POST['edad'];
            $provincia = $_POST['provincias'];
            $dni = $_POST['dni'];
            $sexo = $_POST['sexo'];
            $cp = 0;

            switch($provincia){
                case "Palencia":   $cp = 1010;
                      break;

                case "Valladolid": $cp = 1020;
                      break;

                case "Salamanca": $cp = 1030;
                      break;

                case "Burgos": $cp = 1040;
                      break;

                case "Avila": $cp = 1050;
                      break;

                case "Soria": $cp = 1060;
                      break;

                case "Segovia": $cp = 1070;
                      break;

                case "Zamora": $cp = 1080;
                      break;       
            }
       }
    ?>

  <form action="<?PHP echo $_SERVER['PHP_SELF']?>" method="post">
       <div>
            <label for="nombre">Nombre</label>
            <input type="text" name="nombre" size="15"><br/><br/>
            <label for="apellido">Apellidos</label>
            <input type="text" name="apellido" size="25"/><br/><br/>
            <label for="edad">Edad</label>
            <input type="text" name="edad" size="4"/><br/><br/>
            <label for="proincias">Pais</label>
            <select id="" name="provincias">
                <option value = "Palencia">Palencia</option>
                <option value = "Valladolid">Valladolid</option>
                <option value = "Salamanca">Salamanca</option>
                <option value = "Burgos">Burgos</option>
                <option value = "Avila">Avila</option>
                <option value = "Soria">Soria</option>
                <option value = "Segovia">Segovia</option>
                <option value = "Zamora">Zamora</option>
            </select><br/><br/>
            <label for="cp">CP</label>
            <input type="text" name="cp" value="<?PHP ?>" size="10" disabled /><br/><br/>
            <label for="dni">DNI</label>
            <input type="text" name="dni" size="10"/><br/><br/>
            <input type="radio"  id="" name="sexo" value="hombre" />Hombre
        <input type="radio" name="sexo" value="mujer" />Mujer<br/><br/>
        <input type="submit" name="enviar"/>
        </div>
    </form> 
</body>

What I try is that according to the fence, selecting each province shows me the province code but established in an input in which I call it CP (Postal Code). There in the value I was trying but leave it half XD

    
asked by Ronald Sanchez 25.10.2017 в 19:20
source

4 answers

5

The code you need is the following:

<input type="text" name="cp"
  value="<?= isset($cp)?htmlspecialchars($cp):'' ?>"
  size="10" disabled />
<br/><br/>

Look for the use of htmlspecialchars so that special HTML characters such as quotes (") become HTML entities such as &quote; and so do not break the HTML tag or suffer XSS attacks .

Although the value of $cp is bounded and you have it checked, it is always good practice to use this function so that in case you change the code or the way you calculate the zip code do not open a hole in security of your page.

Also, I've used isset to avoid a warning message saying that the variable is not defined when the page loads before sending the form and defining the value of it.

The ternary operator ?: ( isset($cp)?htmlspecialchars($cp):'' ) is used to avoid a block if checking if there is $cp and if it is showing its content and otherwise it will show an empty string (or whatever you want, you just have to change the '' for anything else such as '00000' , for example) .

If you do not want the user to know what value this field has, you can hide it in the following way:

<input type="hidden" name="cp"
  value="<?= isset($cp)?htmlspecialchars($cp):'' ?>"
  size="10" disabled />
<br/><br/>

The complete code is:

<?php
    if(isset($_POST['enviar'])){
        $nombre = $_POST['nombre'];
        $apellidos = $_POST['apellidos'];
        $edad = $_POST['edad'];
        $provincia = $_POST['provincias'];
        $dni = $_POST['dni'];
        $sexo = $_POST['sexo'];
        $cp = 0;

        switch($provincia){
            case "Palencia":   $cp = 1010;
                  break;

            case "Valladolid": $cp = 1020;
                  break;

            case "Salamanca": $cp = 1030;
                  break;

            case "Burgos": $cp = 1040;
                  break;

            case "Avila": $cp = 1050;
                  break;

            case "Soria": $cp = 1060;
                  break;

            case "Segovia": $cp = 1070;
                  break;

            case "Zamora": $cp = 1080;
                  break;       
        }
   }
?>

<form action="<?PHP echo $_SERVER['PHP_SELF']?>" method="post">
   <div>
        <label for="nombre">Nombre</label>
        <input type="text" name="nombre" size="15"><br/><br/>
        <label for="apellido">Apellidos</label>
        <input type="text" name="apellido" size="25"/><br/><br/>
        <label for="edad">Edad</label>
        <input type="text" name="edad" size="4"/><br/><br/>
        <label for="proincias">Pais</label>
        <select id="" name="provincias">
            <option value="Palencia">Palencia</option>
            <option value="Valladolid">Valladolid</option>
            <option value="Salamanca">Salamanca</option>
            <option value="Burgos">Burgos</option>
            <option value="Avila">Avila</option>
            <option value="Soria">Soria</option>
            <option value="Segovia">Segovia</option>
            <option value="Zamora">Zamora</option>
        </select><br/><br/>
        <label for="cp">CP</label>
        <input type="text" name="cp"
          value="<?= isset($cp)?htmlspecialchars($cp):'' ?>"
          size="10" disabled />
        <br/><br/>
        <label for="dni">DNI</label>
        <input type="text" name="dni" size="10"/><br/><br/>
        <input type="radio"  id="" name="sexo" value="hombre" />Hombre
    <input type="radio" name="sexo" value="mujer" />Mujer<br/><br/>
    <input type="submit" name="enviar"/>
    </div>
</form> 
</body>
    
answered by 25.10.2017 / 19:40
source
4

With htmlspecialchars :

<input type="text" value="<?=htmlspecialchars($tuvariable);?>" />

If you use a version prior to php 5.4

<input type="text" value="<?php echo htmlspecialchars($tuvariable); ?>" />

Adapted to your code

<input type="text" name="cp" value="<?=htmlspecialchars($cp); ?>" size="10" disabled />'
    
answered by 25.10.2017 в 19:23
2

You simply have to print the variable in the attribute value of your input

<input type="text" value="<?= htmlspecialchars($varibable); ?>">

And that's it

    
answered by 25.10.2017 в 19:23
1

With PHP you can only apply the value at the moment of creating the static html on the server to be sent to the client's browser. If what you want is to modify a value based on the behavior of the user, etc .. You can only do it with Javascript since PHP runs only on the server side.

PHP - Sentencia echo
Ejem: <img src="<?php echo './miLogotipo.jpg' ?>">

Javascript - elemento.setAttribute('nombre del atributo', 'valor a poner')
Ejem: document.getElementsByTagName("img")[0].setAttribute("src", "./miLogotipo.jpg")
    
answered by 25.10.2017 в 19:32