I just want to ask why after getting a data from MySQL and trying to put it's value on a textfield:
$contractorx = $row['Contractor'];
The value of $contractorx is "Jose Amadeo Alonzo" which is correct. But when I try to display it in an HTML text field using this code:
echo "<p>Contractor: <input type=\"text\" name=\"mdu\" readonly value=$contractorx></p>";
The text displayed on the field is only "Jose". Can anyone please help in identifying why this happens and how to solve it?
Because the result looks like this:
<input type="text" name="mdu" readonly value=Foo Bar Baz>
The attribute value has the single value Foo, followed by two more attributes Bar and Baz. You need to use quotes and HTML escape the value correctly, should it itself contain quotes:
printf('<input type="text" name="mdu" readonly value="%s">',
htmlspecialchars($contractorx, ENT_COMPAT));
Then the result will correctly look like:
<input type="text" name="mdu" readonly value="Foo Bar Baz">
Add quotes to the "value"
value=\"$contractorx\"
Use htmlspecialchars():
$contractorx = htmlspecialchars($row['Contractor']);
and do not forget about quotes:
echo "<p>Contractor: <input type=\"text\" name=\"mdu\" readonly value=\"$contractorx\"></p>";
Or use single quotes:
echo '<p>Contractor: <input type="text" name="mdu" readonly value="'.$contractorx.'"></p>';
echo sprintf("<p>Contractor: <input type='text' name='mdu' readonly value='%s' /></p>", $contractorx);
echo '<p>Contractor: <input type="text" name="mdu" readonly value="'.$contractorx.'"</p>';
Related
My query extract a data value from mysql table where are a double quote in the text.
Select mytitle from title_table
The result is: This is my title: "for school"
This value I want to put inside a text field, but is truncated in this position This is my title:
I print the title by this: <?php echo $rows['mytitle']; ?>
How to put the entire title in a text field?
Thanks
Print inside the value of the text field, example:
<input type="text" name="title" value="<?php echo $rows['mytitle']; ?>">
I have resolve this problem. Are easy, but in a first time make a wrong Google search.
I have resolve by this: htmlentities
I think you search for this
<?php
echo '<input type="text" value="' . $rows['mytitle'] . '">';
?>
or in plain HTML with
<input type="text" value="<?=$rows['mytitle'];?>">
Look to, for the second example you must have enabled short_tags in your php.ini or use
<input type="text" value="<?php echo $rows['mytitle'];?>">
try this
<input type="text" name="yourkey" value="<?php echo $rows['mytitle'] ?>" />
I'm getting the variable within textbox as $Dataprefix=hello, but what I want is to show the values within quotes as "hello"
Here's my code.
Data File Prefix Name: <input size="5" name="dataprefix" type="text" value="<?php echo $Dataprefix;?>">
and it shows value within textbox as hello
Then try this way it'll work for you
Data File Prefix Name: <input size="5" name="dataprefix" type="text" value='"<?php echo $Dataprefix;?>"'>
^^ ^^
You missed the echo. Try this
Data File Prefix Name: <input size="5" name="dataprefix" type="text" value="<?php echo $Dataprefix;?>">
Use echo to print variable
value="<?php echo $Dataprefix;?>">
If you want the variable wrapped quotation marks to be displayed then :
echo '"'.$Dataprefix.'"';
If you only want to echo the variable when it satisfies "hello" :
echo ($Dataprefix === 'hello'? $Dataprefix : '' );
If this isn't what you are looking for then I would recommend rephrasing your question.
I have a data stored in database valued xy". When I print this value in tag, or quote sign is visible. But when i output this value in input field, double quote isn't visible(it's still in database).
<input type="text" value="<?php echo $value ?>" />
The interesting thing is, when I use 2 single qoutes, output is equal to the value in database(xy''). Any ideas
Try this one
<input type="text" value="<?php echo htmlspecialchars($value) ?>" />
I'm trying to create a textbox that will be displayed on my website. When displayed, I'd like to show some data within the text box. Here is what I have
echo "<input type=\"text\" size=\"100\" value=\"\">";
All that shows up in the text box is <a href=
And then at the end of the text box, right after the text box I see ">
I know something must be syntactically off, just not sure what.
You must encode <, ", and > chars - they can't be embedded that way. Use:
echo '<input type="text" size="100" value="'.htmlspecialchars('').'">';
You may also use urlencode() function - see which suits you better.
One more tip - use single quotes when string contains HTML-like content. This will save you adding \" everywhere.
php_code ?>
<input type="text" size="100" value="<a href="e;<?=$url;?>"e;></a>\">
<?php
php_code
maybe this will work for you
Think of what the html would look like:
<input type="text" size="100" value="">
^
|
This is where the value attribute ends!
htmlspecialchars should solve it.
You have made some mistake. Your code will result in something like that (also visible in this jsfiddle):
<input type="text" size="100" value="">
Instead you can use something like that:
echo "<input type=\"text\" size=\"100\" value=\"<a href="$url"></a>\">";
or
echo '<input type="text" size="100" value="<a href="' . $url . '"></a>">';
to receive effect visible in this jsfiddle. Is it satisfying enough?
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.'>';