Jump to content

Appropriate Syntax for Inserting PHP Code Within <input> Element


immortalfirefly

Recommended Posts

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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.