943,865 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 5836
  • PHP RSS
Sep 9th, 2009
0

echo in form input value

Expand Post »
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:


PHP Syntax (Toggle Plain Text)
  1.  
  2. <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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
andym67 is offline Offline
14 posts
since Sep 2009
Sep 9th, 2009
0

Re: echo in form input value

Hi andym67 and welcome to DaniWeb

A better approach is this:
php Syntax (Toggle Plain Text)
  1. if(isset($_SESSION["namevalue"]))
  2. $val = $_SESSION["namevalue"];
  3. else
  4. $val = "";
  5. // maybe do some validation on $val here
  6. echo "<input name='fname' type='text' class='textfield' id='fname' value='$val' />";
or:
php Syntax (Toggle Plain Text)
  1. <?php
  2. if(isset($_SESSION["namevalue"]))
  3. $val = $_SESSION["namevalue"];
  4. else
  5. $val = "";
  6. // maybe do some validation on $val here
  7. ?>
  8. <input name="fname" type="text" class="textfield" id="fname" value="<?php echo $val; ?>" />;
Last edited by darkagn; Sep 9th, 2009 at 8:54 pm.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Sep 9th, 2009
0

Re: echo in form input value

Look at your value attribute,:
php Syntax (Toggle Plain Text)
  1. value = <?php echo $_SESSION["namevalue"]; ?> />

This should be:
php Syntax (Toggle Plain Text)
  1. value="<?php echo $_SESSION["namevalue"]; ?>" />
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Sep 9th, 2009
0

Re: echo in form input value

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="" />
Last edited by almostbob; Sep 9th, 2009 at 9:31 pm.
Reputation Points: 562
Solved Threads: 368
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
Sep 10th, 2009
0

Re: echo in form input value

Hey All - Thanks for the several different suggestions. I am using
PHP Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
andym67 is offline Offline
14 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: php/mysql search display
Next Thread in PHP Forum Timeline: Wampserver and Tomcat Apache ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC