• 0

Syntax for PHP Echo (including style tags)


Question

K, so I am by no means advanced at PHP. Working on an application page which is basically a long form.

I am trying to spruce up the fields a little bit and ran into an issue. Below is the field I am trying to change. It is where they enter their first name.

<br><b>Legal First Name: *</b>

		<?php
if(empty($_POST['firstname']) and empty($row['FIRSTNAME'])){
	echo "Required";
}
if (!empty($row)) {
	echo "<br><input type='text' name='firstname' value=" . $row['FIRSTNAME'] . "><br>";
}
else {
	echo "<br><input type='text' name='firstname' value=" . $_POST['firstname'] . "><br>";
}
?>

I am needing to add the following attributes, such as class and style to the above field. I don't know if I'm just too tired or trying the wrong syntax but not wanting to go.

<input type="text" name="firstname" id="userid" style="vertical-align:middle; margin-bottom:3px;" class="placeholder" rel="First Name">

Thanks!

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

OR you could do this...

<br><b>Legal First Name: *</b>

<?php

if( empty($_POST['firstname'] . $row['FIRSTNAME'])):
	echo 'Required';
endif;

if (!empty($row)):
	$val = $row['FIRSTNAME'];
else:
	$val = $_POST['firstname'];
endif;

echo '<br><input id="userid" class="placeholder" type="text" name="firstname" style="vertical-align: middle; margin-bottom: 3px;" value="'. $val . '" rel="First Name"><br>';

?>

Link to comment
Share on other sites

  • 0

I personally would do this..

<br /><strong>Legal First Name: *</strong>

<?php

if(empty($_POST['firstname']) and empty($row['FIRSTNAME'])) echo "Required";

echo "<br /><input type='text' name='firstname' value='" . ($_POST) ? $_POST['firstname'] : $row['FIRSTNAME'] . "' /><br />";

?>

Is less code & easier to read.

Link to comment
Share on other sites

  • 0

Something I learned quite a long time ago now is that it is best practice to use single quotes rather than double quotes, unless you have variables being parsed inside the quotes. With single quotes PHP treats it as plain text, but for double quotes it starts looking through the text for a variable that may or may not exist.

<?php
echo 'Single quotes for plain text.';
echo "Double quotes where a $variable is inside.";
echo 'Single quotes when you join '. $variables .' like this (defeats the point of the join to use double quotes).';
?>

The performance difference is probably negligible these days, but because I'm used to doing it this way I do find I avoid a lot of ugly \" code and parsing issues (as you have run in to) when working with HTML in PHP.

Link to comment
Share on other sites

  • 0

how about

<br />
<label><strong>Legal First Name: *</strong></label>

<?php

if( empty($_POST['firstname'] . $row['FIRSTNAME']) ):
	echo 'Required';
endif;

if ( !empty($row) ):
	$val = $row['FIRSTNAME'];
else:
	$val = $_POST['firstname'];
endif;
?>

<br />
<input id="userid" class="placeholder" type="text" name="firstname" style="vertical-align: middle; margin-bottom: 3px;" value="<?php echo $value; ?>" rel="First Name">
<br />

Link to comment
Share on other sites

  • 0

Something to note:

  • The $_POST variables aren't always set. You probably should check if the variable is set (with isset()) before trying to use it.
  • The empty() checks if the variable has empty or zero value, not just if the value is empty as in empty string. Probably isn't problem in this case as you are looking for a name, but keep that in mind.
  • Remember to escape the incoming $_POST data with htmlspecialchars() (or such) before using it in the HTML. You want to escape HTML that might be sent with the form.

how about

Note that empty only works with variables, not with functions nor strings. That code won't work with out checking the variables individually.

Link to comment
Share on other sites

  • 0

Wow thanks for all the responses guys. I appreciate the advise and will try your suggestions when I am back at work on Monday and let you know how it goes.

Have a great new years!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.