Using a PHP variable in a text input value = statement

I retrieve three pieces of information from the database, one integer, one line and one date.

I repeat them to check if the variables contain data.

When I use variables to populate the three input fields on the page, they do not fill out correctly.

The following steps do not work:

id: <input type="text" name="idtest" value=$idtest> 

Yes, the variable must be inside <? php var? > so that it is visible.

So:

 id: <input type="text" name="idtest" value=<?php $idtest ?> /> 

The field displays / .

When I get out of quotes,

 id: <input type="text" name="idtest" value=\"<?php $idtest ?>\" /> 

the field displays \"\" .

With single quotes

 id: <input type="text" name="idtest" value='<?php $idtest ?>' /> 

the field does not display anything or is empty.

If single quotes are escaped,

 id: <input type="text" name="idtest" value=\'<?php $name ?>\' /> 

\'\' displayed in the field.

With a slash (I know it’s not right, but to exclude it from the discussion)

 id: <input type="text" name="idtest" value=/"<?php $name ?>/" /> 

the field displays /"/" .

Double quotes, avoid double quotes, avoid double quotes only on the left side, etc. does not work.

I can set an input field for a string. I have not tried using a session variable as I prefer to avoid this.

What am I missing here?

+4
source share
6 answers

Try something like this:

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

Same as thirty days , with the exception of preventing XSS attacks.

You can also use the <?= Syntax (see note), although this may not work on all servers. (It is enabled using the configuration option.)

+33
source

You need, for example:

 <input type="text" name="idtest" value="<?php echo $idtest; ?>" /> 

The echo function is what actually outputs the value of the variable.

+5
source

Decision

You are missing an echo . Every time you want to show the value of a variable in HTML, you need an echo.

 <input type="text" name="idtest" value="<?php echo $idtest; ?>" > 

Note. Depending on the value, your echo is a function that you use to delete it, for example, htmlspecialchars.

+2
source

From an HTML point of view, everything was said, but a little fix on the PHP side and take into account thirty and icktoofay tips:

 <?php echo '<input type="text" name="idtest" value="' . htmlspecialchars($idtest) . '">'; ?> 
+1
source

If you want to read any created function, then how to do it:

 <input type="button" value="sports" onClick="window.open('<?php sports();?>', '_self');"> 
0
source

I am doing PHP for my project, and I can say that the following code works for me. You must try.

 echo '<input type = "text" value = '.$idtest.'>'; 
-3
source

Source: https://habr.com/ru/post/1332175/


All Articles