Click here to Skip to main content
16,022,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have remember value code that passes a session to a text field, it passes only the part before the white space. How can the full string be passed (ie pass "100 main st." instead of "100")

<input type="text" name="address1" placeholder="My address 1" <?php $rememberAddress1 = "value=" . $_SESSION['rememberAddress1']; $rememberAddress12 = !empty($_SESSION['rememberAddress1']) ?  $rememberAddress1 : ''; print ($rememberAddress12); ?> required>


// Full value is in session "100 Main St." but shows just 100 and not the rest
<h2><?php print_r($_SESSION['rememberAddress1']); ?></h2>


What I have tried:

I tried htmlspecialchars and stripslashes
Posted
Updated 15-Jul-24 10:47am

1 solution

You need to surround the attribute value with quotes, and make sure you encode any quotes within the value itself.

Currently, you are generating HTML that looks like:
HTML
<input ... value=This is a test>
which gets interpreted as:
HTML
<input ... value="This" is="" a="" test="">
You need to generate HTML that looks more like:
HTML
<input ... value="This is a test">
 
Share this answer
 
Comments
mcbain19 16-Jul-24 15:10pm    
I tried many variations with the last one showing \""value=" . $_SESSION['rememberAddress1']; $rememberAddress12 = !empty($_SESSION['rememberAddress1']) ? $rememberAddress1 : ''\" But I am still stuck. I see I need to encode the value I just keep hitting a wall.
Richard Deeming 17-Jul-24 3:30am    
As I said, you need to enclose the attribute value in quotes. For example:
<input type="text" name="address1" placeholder="My address 1" value="<?php print(htmlspecialchars($_SESSION['rememberAddress1'])); ?>" required>
mcbain19 17-Jul-24 16:08pm    
Richard, I had that above initially but I was trying to get it to not print a value if blank. But it's not worth having an issue for a minor detail like you are saying. Thanks for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900