0

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.

4
Contributors
4
Replies
22
Views
7 Years
Discussion Span
Last Post by andym67
0

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; ?>" />;

Edited by darkagn: n/a

0

Look at your value attribute,:

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

This should be:

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

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

Edited by almostbob: n/a

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.