Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a PHP variable in a text input value = statement

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

I echo them out to verify the variables contain the data.

When I then use the variables to populate three input boxes on the page, they do not populate correctly.

The following do not work:

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

Yes, the variable must be inside <?php var ?> for it to be visible.

So:

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

The field displays /.

When I escape the quotes,

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

the field then displays \"\".

With single quotes

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

the field displays nothing or blank.

With single quotes escaped,

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

the field displays \'\'.

With a forward slash (I know that's not correct, but to eliminate it from the discussion),

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

the field displays /"/".

Double quotes, escape double quotes, escape double quotes on left side only, etc. do not work.

I can set an input box to a string. I have not tried using a session variable as I prefer to avoid do that.

What am I missing here?

like image 910
malcolm laplante Avatar asked Dec 15 '10 04:12

malcolm laplante


People also ask

How can get input field value in PHP variable?

In order to get an input value, you can use the htmlspecialchars() method and $_REQUEST variable. Note: The htmlspecialchars() method functions by converting special characters to HTML entities. The $_REQUEST variable is a built-in PHP variable that functions by getting data from the input field.

How do we get the textbox value in a PHP variable without a Submit or click button?

on('keyup', function() { // on keyup event in your input field $("#needs"). append(this. value);// append values from input field }); ); You want to save this value in a php variable then you need ajax call(without submitting your form) or you need to submit the form.

How assign textbox value to variable in PHP?

php" method="post"> Name: <input type="text" name="fname"'; if(isset($_POST['fname']) echo " value='$_POST['fname']"; // adds the value of any part-completed form echo ' /><br /> Age: <input type="text" name="age" '; if(isset($_POST'age']) echo " value='$_POST['age']"; // adds the value of any part-completed form echo ...

Can I use PHP variable in JavaScript?

Inside the JavaScript, we can use the PHP tag to echo the PHP variable assigning it to a JavaScript variable. For example, assign a value Metallica to a variable $var inside the PHP tags. Write a script tag and inside it, write a PHP tag. Inside the PHP tag, echo a JavaScript variable jsvar .


1 Answers

Try something like this:

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

That is, the same as what thirtydot suggested, except preventing XSS attacks as well.

You could also use the <?= syntax (see the note), although that might not work on all servers. (It's enabled by a configuration option.)

like image 84
icktoofay Avatar answered Oct 13 '22 00:10

icktoofay