954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

echo in form input value

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.

andym67
Newbie Poster
14 posts since Sep 2009
Reputation Points: 10
Solved Threads: 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; ?>" />;
darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

Look at your value attribute,:

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


This should be:

value="<?php echo $_SESSION["namevalue"]; ?>" />
Will Gresham
Master Poster
755 posts since May 2008
Reputation Points: 96
Solved Threads: 125
 

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

almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

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

andym67
Newbie Poster
14 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You