Can ignore other post not sure why i closed it, not solved lol, not enough coffee too much looking at php anyway.

I have this form I'm working on and found something interesting

attempt1
this way works exactly how I want, which is nothing in the input boxes and no error messages on loading the registration form, once submit button is pressed the empty fields have a message appear next to them, the only problem is it does not validate on the w3 website

<label>Username</label><br />
<input name="name" type="text"  value="<?php if(isset($_POST['name'])) echo $_POST['name'] ?>"/>
<?php if (!empty($arrErrors['name'])) echo '<span class="errortext">'.$arrErrors['name'].'</span>';?>
<br />

error message is, all errors underline the " after the word value

#  Error  Line 78, Column 47: XML Parsing Error: Unescaped '<' not allowed in attributes values

…nput name="name" type="text"  value="<?php if(isset($_POST['name'])) echo $_P

✉
# Error Line 78, Column 47: XML Parsing Error: attributes construct error

…nput name="name" type="text"  value="<?php if(isset($_POST['name'])) echo $_P

✉
# Error Line 78, Column 47: XML Parsing Error: Couldn't find end of Start Tag input line 78

…nput name="name" type="text"  value="<?php if(isset($_POST['name'])) echo $_P

attempt 2
this code validates

<label>Username</label><br />
<input name="name" type="text"  value="&lt;?php if(isset($_POST['name'])) echo $_POST['name'] ?&gt;"/>
 <?php if (!empty($arrErrors['name'])) echo '<span class="errortext">'.$arrErrors['name'].'</span>';?>
<br />

however on loading the form this code appears in the text area which makes it look very ugly, it does this because of the < being changed to &lt; and > to &gt; and simply treats it as input

<?php if(isset($_POST['name'])) echo $_POST['name'] ?>

attempt 3
which validates

<label>Username</label><br />
<?php echo "<input name=\"name\" type=\"text\"  value=\"" if(isset($_POST['name'])) echo $_POST['name'], "\" />";
 if (!empty($arrErrors['name'])) echo '<span class="errortext">'.$arrErrors['name'].'</span>';
echo "<br />";
?>

but throws the following parse error, with line 79 being the line with input tag

Parse error: parse error, expecting `','' or `';'' in C:\wamp\www\music good copies\register.php on line 79

what i need is to make the outcome of attempt 1 which is empty form on load and error messages when empty fields are attempted to be submitted with the code like attempt 3 so that it validates

validation is not critical as its for a php site that is hosted locally, however for future projects I would like to know the correct way.

cheers

Recommended Answers

All 4 Replies

Try attempt 4 the following:

<label>Username</label><br />
<input name="name" type="text"  value="<?php if(isset($_POST['name'])) echo $_POST['name'] ?>"/>
<?php echo (!empty($arrErrors['name']))? '<span class="errortext">'.$arrErrors['name'].'</span>':''; ?>
<br />

Try attempt 4 the following:

<label>Username</label><br />
<input name="name" type="text"  value="<?php if(isset($_POST['name'])) echo $_POST['name'] ?>"/>
<?php echo (!empty($arrErrors['name']))? '<span class="errortext">'.$arrErrors['name'].'</span>':''; ?>
<br />

works how I want except it doesn't validate

value="<?php if(isset($_POST['name'])) echo $_POST['name'] ?>

this line is the problem and in particular the " after value=, it throws the same 3 errors that attempt 1 did.

Member Avatar for diafol

You don't have a ';' at the end of the line.

works how I want except it doesn't validate

value="<?php if(isset($_POST['name'])) echo $_POST['name'] ?>

this line is the problem and in particular the " after value=, it throws the same 3 errors that attempt 1 did.

I didn't spot that piece of php code. Should be as follows:

<label>Username</label><br />
<input name="name" type="text"  value="<?php echo (isset($_POST['name']))? $_POST['name']:''; ?>"/>
<?php echo (!empty($arrErrors['name']))? '<span class="errortext">'.$arrErrors['name'].'</span>':''; ?>
<br />

or

<label>Username</label><br />
<input name="name" type="text"  value="
<?php echo (isset($_POST['name']))? $_POST['name']:''; ?>
"/>
<?php echo (!empty($arrErrors['name']))? '<span class="errortext">'.$arrErrors['name'].'</span>':''; ?>
<br />
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.