php concatenating string with dynamic variable - php

I'm not getting full text while concatenation the string with dynamic variable from database. My code is:
$page ="<input type='text' name='neil' value=".$blogname.">";
I'm getting output like this:
<input type="text" name="neil" value="My" test's="">
Expected output:
<input type="text" name="neil" value="My test's">

The correct way to go about it is:
$page ='<input type="text" name="neil" value="'.$blogname.'">';
You need to enclose the value of the html element within quotes " "

Enclose value in single quotes, and add a = after name. Put a / before > if you really want to too:
$page ="<input type='text' name='neil' value='".$blogname."'/>";

You need to quote your value for your HTML. The nice thing about double quotes is that you don't need to break out of them to add in your variable, and you're able to notice the missing quotes easier. So your string can look like this:
$page ="<input type='text' name='neil' value='$blogname'>";
Or
$page ="<input type='text' name='neil' value='{$blogname}'>";
The second one is great for when you're using array values, like so:
$page ="<input type='text' name='neil' value='{$row['blogname']}'>";

Related

My <input> box won't display single quotes

If it has a single quote in it, any string that I try to enter into my HTML input box is truncated in the input box once it is submitted. Its POST value comes thru unchanged, but the string shows as truncated in the input box, whether I use htmlspecialchars() or not. A noobie question, no doubt, but I've tried hard to figure it out and run out of ideas. Thanks for any help.
<!DOCTYPE html>
<body><title> Self-inserting input_box_SO.php </title>
<?php
// POST form initiation.
ECHO "<form action='input_box_SO.php' method='post'>";
// GET POSTed value and escape it for HTML use
$Caption_htmlspecialchars=$_POST['Caption_htmlspecialchars'];
$Caption_htmlspecialchars=htmlspecialchars($Caption_htmlspecialchars);
ECHO "The echo of the variable <em> \$Caption_htmlspecialchars </em> looks like this:<br>";
ECHO "<b> $Caption_htmlspecialchars </b><br>";
ECHO "But in the input box, \$Caption_htmlspecialchars is truncated by a single quote: <br>";
// ETA: Bad old line that caused the problem, now commented:
// ECHO "<input type='text' name='Caption_htmlspecialchars' size=100 value='$Caption_htmlspecialchars' maxlength = 100 required /><br><br>";
// ETA: Newly added line that fixes the problem:
echo '<input type="text" name="Caption_htmlspecialchars" size=100 value="'.$Caption_htmlspecialchars.'" maxlength = 100 required /><br><br>';
// SUBMIT button. Submits back to the same page: input_box.php
echo "<b><input type='Submit' name='submit' value='Submit'/></b></br></br>";
?>
</body></html>
Here is what Inspect Elements > Elements shows for the input element:
input_box_SO.php
The echo of the variable $Caption_htmlspecialchars looks like this: test with special chars. & " < > and a single quote ('), which causes truncation in the input box. But in the input box, $Caption_htmlspecialchars is truncated by a single quote: and a single quote (" ),="" which="" causes="" truncation="" in="" the="" input="" box.="" '="" maxlength="100" required="">
With the Source looking like this: value='test with special chars. & " < > and a single quote ('), which causes truncation in the input box. '
You need to change your sequence of single quotes nad double quotes to display string. change your echo <input as below
echo '<input type="text" name="Caption_htmlspecialchars" size=100 value="'.$Caption_htmlspecialchars.'" maxlength = 100 required /><br><br>';
Try to use the addslashes and do it like
$Caption_htmlspecialchars = addslashes($Caption_htmlspecialchars);

ng-true-vale Non-Constant Expression

i am printing checkbox value something following by php
<input type='checkbox' ng-false-value='' ng-true-value='$row->option'>
i am looping this dynamically,
but getting following error
https://docs.angularjs.org/error/ngModel/constexpr?p0=ngTrueValue&p1=Canel
in html this ng-true-value='$row->option' printing like ng-true-value="Canel"
is there any way to do like this ng-true-value="'Canel'", thats mean doublequte then single quote then value then single quote then double quote (something like "'Canel'")
<?php
echo "<input type='checkbox' ng-false-value='' ng-true-value=\"'$row->option'\">";
If $row->option is test it will output:
<input type='checkbox' ng-false-value='' ng-true-value="'test'">

Php add value to input field?

Is it possible to add a value to a dynamically created input field?
Im trying something like this: echo "<input value="$row['test']">" but that gives me a syntax error. It has to be in my php file since im calling it via ajax and want to keep my html and php seperate.
Im getting content via ajax and I need to set many field names as there are records in the database.
You can do it like this:
echo '<input value="' . $row['test'] . '">';
Alternatively escape the " (not recommended if not needed, because it is hardly readable):
echo "<input value=\"" . $row['test'] . "\">";
Otherwise you’re mixing quotation marks, which is a syntax error. The . is to combine strings with variables (in this case).
You can also Use:
<input value="<?php echo htmlspecialchars($row['test']); ?>" >
OR
echo "<input name='test' value=".$row['test'].">";
OR
echo "<input name='fred' value='{$row['test']}'>";
Reference
When using certain php values within a quoted string, such as the array syntax in the question, you should either escape from the quotes or encapsulate the variable in curly braces. Also, as the string was quoted with double quotes you should use single quotes around attributes and values.
echo "<input name='fred' value='{$row['test']}'>";
<input type="text" name="post_title" class="form-control" value="<?php
if(isset($post_title))echo $post_title;
?>">
If you want to add it into the HTML

php echo outputs only part of a variable in a form

I'm loading a variable from a database like:
$adres = $row['adres']; //(= "Hoge Filterweg")
Then using it in a echo like:
echo input type='text' name='adres' value='{$adres}'
It displays on the form only the first part of the adress ( "Hoge"), but not the whole adress.
What could I do now?
Single quotes surrounding the inline variable like value='{$adres}' dit the trick.
thanks
You need double quotes to pass params with space.
This code should help you:
echo "<input type=\"text\" name=\"adres\" value=\"{$adres}\" />"

Text containing single quote as input value [PHP]

I try to show this string : «let's go» in a the value of an input tag
i wrote:
$chaine="let's go";
echo "<input type=text name=test value='".$chaine."'>";
result: let
What can i do to show the correct string in this case ?
use htmlspecialchars
echo "<input type=text name=test value='".htmlspecialchars($chaine, ENT_QUOTES)."'>";
You can look at htmlentities()
htmlentities($chaine, ENT_QUOTES) ;
This produces
<input type=text name=test value='let's go'>
You can see, that for HTML (--> your Browser) the value ends after "let". Everything after that is invalid (but ignored). Escape
$chaine = "let\'s go";
However, in HTML double quotes are prefered and omitting the quotes is also no good style
$chaine="let's go";
echo '<input type="text" name="test" value="'.$chaine.'">';
In this case you must escape every double quote.

Categories