This is driving me crazy and should be very easy. I am trying to pre-fill an html form with session data using echo. I can compile and run but the form field contains a / if the session data is blank. How can I get rid of the /

Heres the code:

<input name="fname" type="text" class="textfield" id="fname" value = <?php echo $_SESSION["namevalue"]; ?> />

I have tried several combinations of '"` etc and it seems I am just missing the obvious. Thanks in advance - anyone.

Recommended Answers

All 4 Replies

Hi andym67 and welcome to DaniWeb :)

A better approach is this:

if(isset($_SESSION["namevalue"]))
   $val = $_SESSION["namevalue"];
else
   $val = "";
// maybe do some validation on $val here
echo "<input name='fname' type='text' class='textfield' id='fname' value='$val' />";

or:

<?php
if(isset($_SESSION["namevalue"]))
   $val = $_SESSION["namevalue"];
else
   $val = "";
// maybe do some validation on $val here
?>
<input name="fname" type="text" class="textfield" id="fname" value="<?php echo $val; ?>" />;

Look at your value attribute,:

value = <?php echo $_SESSION["namevalue"]; ?> />

This should be:

value="<?php echo $_SESSION["namevalue"]; ?>" />

Agree with Xan's code fix


embedding php inside html, it is important to get all the quotes in.
only add a usefula(maybe) caveat to the code he provided, a recommendation to change whatever editor used to something that code highlights
It becomes easy to see when something is missing if the text is some bright colour when a(the) quote(s) Brace(s) Bracket(s) < > is(are) missing
Notepad++ Notepad2 & a bunch of good code highlighting editors

without the quotes output html for a nul field is value= /> your visble backslash
with quotes a nul gives value="" />

Hey All - Thanks for the several different suggestions. I am using

if(isset($_SESSION["namevalue"]))

but was able to determin that the previously mentioned part was where I was messing up so hence my limited code post.

I am still getting used to PHP and NetBeans both so the color scheme/code completion and the exact ;,'"` where have you was throwing me off.

Thanks again.!

Andy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.