0

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">
5
  • 2
    You're not putting any quotes to output for your value. Commented Apr 4, 2017 at 12:22
  • So what does $blogname contain? Commented Apr 4, 2017 at 12:23
  • name'neil' certainly will not be magically changed to name='neil' by the php fairies. Commented Apr 4, 2017 at 12:25
  • Actually I seriously doubt you really get what you claim you get. Makes no sense. It might be that some browser engine cuts in to try to fix your invalid html markup. But what you post is not what php outputs. Commented Apr 4, 2017 at 12:26
  • @Neil Dhakal consider approving the answer that helped you out, so that it helps the community. Commented Apr 5, 2017 at 18:33

3 Answers 3

4

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 " "

Sign up to request clarification or add additional context in comments.

Comments

0

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."'/>";

7 Comments

$blogname contains single string. like $blogname = it's mine
/ is not really necessary in an input tag according to the HTML5 standards .
@NeilDhakal - actually I missed that $blogname contained a single quote, so Shakti's answer is better
@ShaktiPhartiyal - hence "if you really want to", but yes - you're right.
@markdwhite the OP does not need a useless suggestion. No issues otherwise.
|
0

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']}'>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.