I want to set the value of an input text field with php, but it does not seem to work.
There are many similar Questions where the answer was something like this:
<?php $value = "Show This!" ?>
<form>
<input type="text" name="testname" value="<?php echo $value;?>" >
</form>
But this will print the whole <?php... stuff in the text field like this.
For clarification:. I am expecting to see this.
What am I doing wrong?
Note: I also tried several variants with/without string symbols, also the shorthand variant <input type="text" value="<?=$value?>"> and even something without a php variable:
<form>
<input type="text" name="testname" value="<?php echo 'Show this please';?>" >
</form>
your code looks correct, you just need to run it as PHP instead of HTML
Related
Form:
<form method="POST" action="edit_work.php">
<input type="hidden" name="wid[]" size="1" value="<?php echo "$wid1" ?>" >
<input type="text" name="course[]" size="15" value="<?php echo "$course1" ?>" >
PHP:
extract($_POST);
for($i=0;$i<$count;$i++) {
echo $wid[$i];
echo $course[$i];
}
gives the wid values OK but not the text entered for the course names.
I have been through all forums for 2 days now. Any help? Thanks.
If you want your PHP to retrieve your data from the form, can't you name your text input "course", then get it inside your PHP with $_POST['course'] ?
What is your $count ?
Using brackets with your name attribute inside your input tag may be dangerous.
If you're using a list of inputs maybe you can define a text format like name="course#" where # is your index and then access it form your $_POST variable using $_POST['course'.$index]
You don't need to extract($_POST) in that case.
<input type="radio" name="package" value="SOME_VALUE_HERE">
I am using Joomla but i think its something related to PHP. I have a form on my website, and it has RADIO button , what i want is when the user submits the form with that RADIO BUTTON selected, in place of the value (SOME_VALUE_HERE) , i want something else to get stored in the database. That something should a 10-15 liner text. Can i make a $PHP variable and assign that 10-15 liner text to that variable and use it in place of the value=(SOME_VALUE_HERE).
Example :
<input type="radio" value="$phpvariable">
Where $phpvariable is a 10-15 liner text!
Hope you got my point!
You need to use php-tags and echo the php variable in the value of the input:
Assign value to php variable:
<?php $phpvariable = 'SOME LONG TEXT HERE'; ?>
The input:
<input type="radio" name="package" value="<?php echo $phpvariable; ?>">
You will also need a name for your input so that you can get the value
My suggestion would be to try:
<input type="radio" name="package" value="<?php echo htmlspecialchars($name); ?>">
If that does not work, just create a hidden field:
<input type="hidden" name="packageHidden" value="<?php echo htmlspecialchars($name); ?>">
function htmlspecialchars prevents XSS attacks as well.
I encountered a very strange issue, that I never seen before. I have a loop and I echo the loop's output without any problems using or other similar tags:
<p></php echo $values[1];?></p>
it works like a charm, but when I try to echo the same value inside a text input strange things start to happen. The output inside an input is wrapped in tags.
<input type="text" value="<?php echo $values[1]; ?>"/>
gives me in result (that's how it looks like in web inspector in Chrome):
<input type="text" value=" <td>2.62</td>">
What did I do wrong??
Based on your comments then replace:
<input type="text" value="<?php echo $values[1]; ?>"/>
.. with:
<input type="text" value="<?php echo trim(strip_tags($values[1])); ?>"/>
It really sounds like a mistake that there can be HTML tags inside your variables and if this should not happen then of course this should be fixed
$values[1] contains the data
<td>2.62</td>
If your example is from your actual code, take notice that your first variable is $values[0] and the input one is $values[1]
I'm trying to make a user search with the following code:
<?php
session_start();
include("../BD/bd.php");
$searched_for = $_POST['searched_for'];
$query = #mysql_query("SELECT * FROM user_media WHERE nombre LIKE '%$searched_for%'") or die(mysql_error());
while($got_users = #mysql_fetch_array($query)){
echo '<div class="searched-content-info">'.
'<div class="searched-photo"><img src="'.$got_users['foto'].'"></div>
<div class="searched-names"><h3>'.$got_users['nombre'].'</h3></div>
<div class="searched-dates"><h3>'.'Miembro desde: '.$got_users['created_on'].'</h3></div>
</div>
<div class="divisor-search-user"></div>';
}
?>
But I'm getting all the rows, I just want to display the searched users info, seems like the $query is receiving a clean $searched_for
Any help here? Btw, I'm a little newbie here, please don't bully :)
EDIT: I tried changing $got_users['nombre']; with $searched_for to see if $searched_for is empty and yes it doesn't return any string that's why I am getting all the rows. $query is getting an empty variable but Why?
Here's my HTML:
<form target="u-n" id="search_input" action="search_user.php" method="post">
<input id="search-input" name="searched_for" type="search" placeholder="Search">
</form>
You used <input type="search" /> which is a HTML5 feature. Older browsers may not support this. Replace this input with type="text".
Then, your $_POST['searched_for'] should populate properly, that is:
<input name="searched_for" type="text" placeholder="Search" />
Also, you used the same id multiple times, which is an invalid HTML syntax.
Reference: HTML input tag at MDN
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?
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.)
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.
Solution
You are missing an echo. Each time that you want to show the value of a variable to HTML you need to echo it.
<input type="text" name="idtest" value="<?php echo $idtest; ?>" >
Note: Depending on the value, your echo is the function you use to escape it like htmlspecialchars.
From the HTML point of view everything's been said, but to correct the PHP-side approach a little and taking thirtydot's and icktoofay's advice into account:
<?php echo '<input type="text" name="idtest" value="' . htmlspecialchars($idtest) . '">'; ?>
If you want to read any created function, this how we do it:
<input type="button" value="sports" onClick="window.open('<?php sports();?>', '_self');">
I have been doing PHP for my project, and I can say that the following code works for me. You should try it.
echo '<input type = "text" value = '.$idtest.'>';