tags:

views:

33

answers:

4

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.

+2  A: 

Instead of using echo in the function, return the values.

function state_selection()
{
    $str = '<select name="state">';
    $states = array(
            'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'District of Columbia', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
        );
    foreach ($states as $state_name)
    {
        $str .= '<option value="'.$state_name.'">'.$state_name.'</option>';
    }
    $str .= '</select>';

    return $str;
}
shamittomar
+2  A: 
  1. echo outputs 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 output echoed by it will already be output. Instead, you should build a string in your function and return it.

  2. 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,

    <p><input type=\"hidden\" name=\"op\" value=\"ds\" /></p>
    should be

    <p><input type="hidden" name="op" value="ds" /></p>

  3. 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:

    $formBlock = '<form method="post" action="' . $_SERVER[PHP_SELF] . '"> ...

Daniel Vandersluis
About #2: It always amazes me that even after so many malformed things, HTML displays stuff nicely.
shamittomar
OK - If I remove all of the \'s in the entire string then everything displays perfectly. However when I get submit the form it tells me that the $_SERVER[PHP_SELF] url can't be found. If I change the string to " and escape all of the " as I had originally... the form submits fine but the states don't show up it just displays the '.state_selection().'
Jason
@Jason I updated my answer.... and you've completely changed your question to the point where my answer doesn't really apply anymore. You should really create a second question if you have a new question.
Daniel Vandersluis
perfect thank you so much!
Jason
A: 

Using "error_reporting(E_ALL)" at the top of your php script (after the php tags) will help you debug errors.

alexy13
A: 

Try changing the '.state_selection().' into ".state_selection()."

Kranu