immortalfirefly Posted April 1, 2011 Share Posted April 1, 2011 Hi Group, I looked through the internet and couldn't find an answer, but I was wondering on the appropriate syntax on inserting PHP code into an input element. See below. <input type="text" name="id" value="<?php echo $main['id']; ?>" size="10" /> Now this is purely an example (and I'm aware of php short tags, just don't want to use them). I only ask cause I've used CodeLobster and Eclipse and they don't really know what to do with the added php syntax. CodeLobster and Eclipse wouldn't finish marking up the rest of the line in the appropriate colors (all the text was black after the < in <?php like how I showed it above), however Eclipse's built in debug system thinks this is a syntax error. It runs just fine in the browser and does what I want it to, but I'd like the appropriate systax to get rid of this "error". Thanks in advance, ImmortalFirefly Quote Link to comment https://forums.phpfreaks.com/topic/232456-appropriate-syntax-for-inserting-php-code-within-element/ Share on other sites More sharing options...
Pikachu2000 Posted April 1, 2011 Share Posted April 1, 2011 You should at least make sure the element isn't empty before trying to use it. Keeps warnings from being generated. The below syntax uses the ternary operator, btw. <input type="text" name="id" value="<?php echo !empty($main['id']) ? $main['id'] : ''; ?>" size="10" /> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/232456-appropriate-syntax-for-inserting-php-code-within-element/#findComment-1195716 Share on other sites More sharing options...
Pikachu2000 Posted April 1, 2011 Share Posted April 1, 2011 And as far as short tags go, you shouldn't be using them anyhow if you want your code to retain portability. Quote Link to comment https://forums.phpfreaks.com/topic/232456-appropriate-syntax-for-inserting-php-code-within-element/#findComment-1195720 Share on other sites More sharing options...
immortalfirefly Posted April 1, 2011 Author Share Posted April 1, 2011 That is good to know.....maybe it'd help if I supplied the error that Eclipse is giving me.... "Start tag (<input>) not closed." "Invalid character used in text string (<?php echo echo $main['id']; ?>" size="10" />)." As soon as I get rid of the inserted php code, both errors go away. ImmortalFirefly Quote Link to comment https://forums.phpfreaks.com/topic/232456-appropriate-syntax-for-inserting-php-code-within-element/#findComment-1195724 Share on other sites More sharing options...
PFMaBiSmAd Posted April 1, 2011 Share Posted April 1, 2011 You are attempting to validate php source code as though it was HTML. It is not. Quote Link to comment https://forums.phpfreaks.com/topic/232456-appropriate-syntax-for-inserting-php-code-within-element/#findComment-1195728 Share on other sites More sharing options...
Pikachu2000 Posted April 1, 2011 Share Posted April 1, 2011 If it's interpreting the file as purely html, I can see it giving that warning, otherwise I'd say it's been smoking crack. Quote Link to comment https://forums.phpfreaks.com/topic/232456-appropriate-syntax-for-inserting-php-code-within-element/#findComment-1195729 Share on other sites More sharing options...
immortalfirefly Posted April 1, 2011 Author Share Posted April 1, 2011 Well the main file is a PHP file and I've included this html file that loads when a condition is met and inserts it into the php file. So it is a html file all by itself, but it's being included into a php file. IFF Quote Link to comment https://forums.phpfreaks.com/topic/232456-appropriate-syntax-for-inserting-php-code-within-element/#findComment-1195735 Share on other sites More sharing options...
PFMaBiSmAd Posted April 1, 2011 Share Posted April 1, 2011 If it's got php code in it, no matter what you name it or how you request it, it IS server-side scripting. It's not actually HTML until it has been processed on a web server and sent out to the browser. Any HTML validation errors you are getting from your editor don't apply until after the php code in it has been parsed, tokenized, and interpreted. Quote Link to comment https://forums.phpfreaks.com/topic/232456-appropriate-syntax-for-inserting-php-code-within-element/#findComment-1195739 Share on other sites More sharing options...
immortalfirefly Posted April 3, 2011 Author Share Posted April 3, 2011 For those that are glued to their screens in anticipations of an answer (since so many people have this problem....) I changed the file from .html to .php and left it exactly the way it was. I include that file which works great, and it got rid of my errors in Eclipse. Quote Link to comment https://forums.phpfreaks.com/topic/232456-appropriate-syntax-for-inserting-php-code-within-element/#findComment-1196222 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.