I have a section in my PHP page containing this bit of code:

echo '<div style="float:left;"><h3><strong>Edit User Account</strong></h3>
                                Date Registered: ' . $db_regdate . '<br>
                                User Type: ' . $usertype . '<br><br>
                            <form class="right_aligned" id="form1" name="form1" method="post" action="">
          				      <label>First Name*</label>
                				  <input name="firstname" type="text" id="firstname" value="' . $db_firstname . '"/>
                        <label>Last Name*</label>
                				  <input name="lastname" type="text" id="lastname" value="' . $db_lastname . '" />
                			     <label>Email Address*</label>
                				  <input name="email" type="text" id="email" value="' . $db_email . '" size="32" />                 			  
                				<label>New Password</label>
                                    <input name="password" type="password" id="password" size="10" />
                			    <label>Verify Password</label>
                			      <input name="verifypass" type="password" id="verifypass" size="10" />
        	                   <label>User Type </label>
                                  <select name="usertype" class="header" id="usertype">
                                    <option>Admin</option>
                                    <option>Staff</option>
                                    <option selected="selected">Client</option>
                                  </select>
                				<label></label>
                				  <input type="submit" name="submit_edit" id="submit_edit" value="save" />
                            </form>';

As you can see, I have concatenated (or whatever the appropriate term is) some essential variables within the form. This is basically an edit form so the variables basically load content from the database and display is on the form as 'initial values'.

What I'm trying to do it relocate the form to a file named edit_user.html. HOWEVER, if I try to use PHP include to include the form from an external file I do not get the same result. Instead of displaying the content of the variables, it does not parse the variables as a variable and instead it just displays:

' . $variable . ' in each field.

Where am I going wrong?

I am using this instead of the echo statement:

include('modules/forms/edit_user.html');

If I try to echo the include, I get the same result :(

Dani AI

Generated

The literal text "' . $variable . '" usually means the template file is being treated as plain text, not as PHP. In other words, the string you originally built with PHP concatenation was copied straight into an external file (or that file isn’t being parsed for PHP), so the dots and single quotes remain literal rather than being evaluated as PHP code. and are on the right track: the page that gets included must be parsed as PHP and must contain executable code (or a clean template) that actually outputs your variables.

Quick troubleshooting checklist:

  • Inspect the included file and remove any leftover concatenation syntax copied from an echo string; the file should contain real template markup (or placeholders) that PHP can replace at runtime.
  • Make sure the server parses that file as PHP (file handler/configuration or file extension). A simple PHP test inside the template will confirm whether the server runs it.
  • Verify the variables exist and are in scope before the include. If the include happens inside a function or before the variables are set, the template won’t see them.
  • Don’t wrap the include call inside another echo (include emits the template output and returns a success value); that can confuse what you see.
  • Escape values used inside value attributes to avoid breaking HTML and to prevent XSS.

For safer long‑term structure, separate logic and presentation: prepare variables in the parent script, keep a simple PHP‑parsing template or placeholder template, and sanitize output. For details on how include works and how PHP decides what to execute, see the PHP manual on include (PHP include manual) and on PHP tags (PHP tags).

Recommended Answers

All 4 Replies

i think the problem is you've saved your code in an html file.
As it's php code, you need to save it as a php file not html.

include('modules/forms/edit_user.php');

Thanks for your reply, but it still seems to bring the same problem :(

Thanks for your reply, but it still seems to bring the same problem :(

This worked for me just now on a test, you will need to change the sql details to suit your needs but...
in the main php and sql page

$sql  = "select * from users where username='emhmk1'";

$q = mysql_query($sql);
$r = mysql_fetch_array($q);
include ('name.html');

bear with me wth the code, it's very cruedly written!!
then in the edit html page (i called mine name.html)

<form action="" name="form" method="post">
Name:<br />
<input type="text" name="name" value="<?php echo  $r['username'];?>" />
</form>

That echo's out my username in the value of the input box.

Hope it helps

commented: Thanks. +1

This worked for me just now on a test, you will need to change the sql details to suit your needs but...
in the main php and sql page

$sql  = "select * from users where username='emhmk1'";

$q = mysql_query($sql);
$r = mysql_fetch_array($q);
include ('name.html');

bear with me wth the code, it's very cruedly written!!
then in the edit html page (i called mine name.html)

<form action="" name="form" method="post">
Name:<br />
<input type="text" name="name" value="<?php echo  $r['username'];?>" />
</form>

That echo's out my username in the value of the input box.

Hope it helps

Ahh, that makes more sense. Thanks a lot, I really appreciate it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.