Ok – I have this code.
$formBlock = "<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
<p>First Name: <input name=\"first\" type=\"text\" size=13 maxlength=25 /></p>
<p>Last name: <input name=\"last\" type=\"text\" size=13 maxlength=25 /></p>
<p>City:<input name=\"city\" type=\"text\" size=25 maxlength=50 /></p>
<p>State: '.state_selection().' </p>
<p>Email:<input name=\"email\" type=\"text\" size=50 maxlength=75 /></p>
<p>Birthday:<input name=\"bday\" type=\"text\" size=10 maxlength=10 />(ex 1982-06-26)</p>
<p><input type=\"hidden\" name=\"op\" value=\"ds\" /></p>
<p><input type=\"submit\" name=\"submit\" value=\"Add Contact\" /></p>
</form>";
However – the ‘.state_selection().’ part just comes up with the text ‘.state_selection().’
If I change the string to single quotes, and then remove the \ before the internal double quotes…
everything displays correctly – but when I try to submit the form it tells me that it can’t find the $_SERVER[PHP_SELF] url.
echooutputs as soon as it is encountered, which means that in your case, even though you’re calling your function as part of building a string, the outputechoed by it will already be output. Instead, you should build a string in your function and return it.You should not be escaping your double quotes (
\") within a single quoted string, as it will come out as a literal slash and double quote. Currently, the browser encounters\"text"or\"hidden"as the type attribute, and doesn’t know how to interpret that so reverts to the default type, which is text.In other words,
should be
Single quoted strings do not evaluate variables, so
$_SERVER[PHP_SELF]will be taken literally. Instead, you need to concatenate it outside of your string so that it can be evaluated: