Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a text entry to a drop-down = parse error 2

Status
Not open for further replies.

nedstar1

IS-IT--Management
Mar 2, 2001
127
0
0
US
Hello friends,

I have a page that allows a user to enter a shipping address; it is presently set up with a text field to allow entry for state. However, I have an additional administrative page that collects address info from all completed orders and formats it for upload to my FedEx.com address book.

I'm having some issues because the field for state is a text field, and some folks are entering the full state name. FedEx.com rejects this as invalid - they want the 2 letter abbreviation (eg. IA instead of Iowa). Thus far, I have been manually updating the file before upload. I'm getting tired of doing this, as it is tedious and time-consuming, so I decided to alter the form to use a drop down that shows the states' names, but outputs the 2-letter abbreviation as the value.

Here's the code I had previously:
Code:
<form action='zmax_address_confirmation.php' method='post'>
        <table width='75%'  border='0' cellspacing='0' cellpadding='3'>
          <tr>
            <td width='46%'><span class='style1'>Address 1 *</span></td>
            <td width='54%'><input name='address1' type='text' value='$address1' id='address1' size='50' /></td>
          </tr>
          <tr>
            <td><span class='style1'>Address 2 </span></td>
            <td><input name='address2' type='text' value='$address2' id='address2' size='50' /></td>
          </tr>
          <tr>
            <td><span class='style1'>City *</span></td>
            <td><input name='city' type='text' value='$city' id='city' size='30' /></td>
          </tr>
          <tr>
            <td><span class='style1'>State *</span></td>
            <td><input name='state' type='text' value='$state' id='state' size='30' /></td>
          </tr>
          <tr>
            <td><span class='style1'>Zip or Postal Code *</span></td>
            <td><input name='zip_postal' type='text' value='$zip_postal' id='zip_postal' size='25' /></td>
          </tr>
          <tr>
            <td><span class='style1'>Contact Telephone Number *</span></td>
            <td><input name='telnum' type='text' value='$telnum' id='telnum' size='25' /></td>
          </tr>
          <tr>
          <td>&nbsp;</td>
          <td><input type='submit' value='Submit' /></td>
          </tr>
          </table>
          </form>
          <p><b>NOTE</b>: We cannot deliver to P.O. Boxes.</p>
          </form>

So far so good - it's worked consistently. So I decide to create the drop-down on a blank page and copy it over; I typically use dreamweaver, and the origianl designer used a templating tool, so the form will not display graphically in Dreamweaver. The page is generated dynamically using a template class.

So I create the following code in dreamweaver:

Code:
<select name="state" id="state" value='$state'>
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
//etc etc etc
    <option value="WI">Wisconsin</option>
    <option value="WY">Wyoming</option>
  </select>

to replace this:

Code:
<input name='state' type='text' value='$state' id='state' size='30' />

And I get an error:

Code:
Parse error: parse error, unexpected T_STRING in address.php on line 27

Line 27 in my code is the first option value line. I'm sure it should be easy to figure out, and yet, my brain is starting to sting a little, and there's blood coming out my left ear. I'm sure it has to do with the variable - do I declare and set the variable AFTER declaring the checkbox and possible values?

I'd appreciate a fresh set of eyes.

Regards,
Nedstar1
 
Hi sleipner214,

Thanks for the quick reply.

Here's the full code of the page that works:

Code:
<?php
        include("include.php");
        extract($_GET);
         
        $page_title = "Address Entry";
        $content_title = "Please Enter Your Delivery Address";
        
        $content = "       
        <span class='style4'>(Fields Marked With * Are Required)</span></p>
        <form action='address_confirmation.php' method='post'>
        <table width='75%'  border='0' cellspacing='0' cellpadding='3'>
          <tr>
            <td width='46%'><span class='style1'>Address 1 *</span></td>
            <td width='54%'><input name='address1' type='text' value='$address1' id='address1' size='50' /></td>
          </tr>
          <tr>
            <td><span class='style1'>Address 2 </span></td>
            <td><input name='address2' type='text' value='$address2' id='address2' size='50' /></td>
          </tr>
          <tr>
            <td><span class='style1'>City *</span></td>
            <td><input name='city' type='text' value='$city' id='city' size='30' /></td>
          </tr>
          <tr>
            <td><span class='style1'>State *</span></td>
            <td><input name='state' type='text' value='$state' id='state' size='30' /></td>
          </tr>
          <tr>
            <td><span class='style1'>Zip or Postal Code *</span></td>
            <td><input name='zip_postal' type='text' value='$zip_postal' id='zip_postal' size='25' /></td>
          </tr>
          <tr>
            <td><span class='style1'>Contact Telephone Number *</span></td>
            <td><input name='telnum' type='text' value='$telnum' id='telnum' size='25' /></td>
          </tr>
          <tr>
          <td>&nbsp;</td>
          <td><input type='submit' value='Submit' /></td>
          </tr>
          </table>
          </form>
          <p><b>NOTE</b>: We cannot deliver to P.O. Boxes.</p>
          </form>";
          
          $make = $template->make_page($page_title, $content_title, $content);
          
?>

And the full code of the modified page that doesn't work:

Code:
<?php
        include("include.php");
        extract($_GET);
         
        $page_title = "Address Entry";
        $content_title = "Please Enter Your Delivery Address";
        
        $content = "       
        <span class='style4'>(Fields Marked With * Are Required)</span></p>
        <form action='address_confirmation.php' method='post'>
        <table width='75%'  border='0' cellspacing='0' cellpadding='3'>
          <tr>
            <td width='46%'><span class='style1'>Address 1 *</span></td>
            <td width='54%'><input name='address1' type='text' value='$address1' id='address1' size='50' /></td>
          </tr>
          <tr>
            <td><span class='style1'>Address 2 </span></td>
            <td><input name='address2' type='text' value='$address2' id='address2' size='50' /></td>
          </tr>
          <tr>
            <td><span class='style1'>City *</span></td>
            <td><input name='city' type='text' value='$city' id='city' size='30' /></td>
          </tr>
          <tr>
            <td><span class='style1'>State *</span></td>
            <td><select name='state' type='select' id='state'>
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
    <option value="AZ">Arizona</option>
    <option value="AR">Arkansas</option>
    <option value="CA">California</option>
    <option value="CO">Colorado</option>
    <option value="CT">Connecticut</option>
    <option value="DE">Delaware</option>
    <option value="DC">District of Columbia</option>
    <option value="FL">Florida</option>
    <option value="GA">Georgia</option>
    <option value="HI">Hawaii</option>
    <option value="ID">Idaho</option>
    <option value="IL">Illinois</option>
    <option value="IN">Indiana</option>
    <option value="IA">Iowa</option>
    <option value="KS">Kansas</option>
    <option value="KY">Kentucky</option>
    <option value="LA">Louisiana</option>
    <option value="ME">Maine</option>
    <option value="MD">Maryland</option>
    <option value="MA">Massachusetts</option>
    <option value="MI">Michigan</option>
    <option value="MN">Minnesota</option>
    <option value="MS">Mississippi</option>
    <option value="MO">Missouri</option>
    <option value="MT">Montana</option>
    <option value="NE">Nebraska</option>
    <option value="NV">Nevada</option>
    <option value="NH">New Hampshire</option>
    <option value="NJ">New Jersey</option>
    <option value="NM">New Mexico</option>
    <option value="NY">New York</option>
    <option value="NC">North Carolina</option>
    <option value="ND">North Dakota</option>
    <option value="OH">Ohio</option>
    <option value="OK">Oklahoma</option>
    <option value="OR">Oregon</option>
    <option value="PA">Pennsylvania</option>
    <option value="RI">Rhode Island</option>
    <option value="SC">South Carolina</option>
    <option value="SD">South Dakota</option>
    <option value="TN">Tennessee</option>
    <option value="TX">Texas</option>
    <option value="UT">Utah</option>
    <option value="VT">Vermont</option>
    <option value="VA">Virginia</option>
    <option value="WA">Washington</option>
    <option value="WV">West Virginia</option>
    <option value="WI">Wisconsin</option>
    <option value="WY">Wyoming</option>
  </select></td>
          </tr>
          <tr>
            <td><span class='style1'>Zip or Postal Code *</span></td>
            <td><input name='zip_postal' type='text' value='$zip_postal' id='zip_postal' size='25' /></td>
          </tr>
          <tr>
            <td><span class='style1'>Contact Telephone Number *</span></td>
            <td><input name='telnum' type='text' value='$telnum' id='telnum' size='25' /></td>
          </tr>
          <tr>
          <td>&nbsp;</td>
          <td><input type='submit' value='Submit' /></td>
          </tr>
          </table>
          </form>
          <p><b>NOTE</b>: We cannot deliver to P.O. Boxes.</p>
          </form>";
          
          $make = $template->make_page($page_title, $content_title, $content);
          
?>

So yeah, I sort of do have html in the php, but it's defined as a variable and used in the template procedure. Any idea why the second code doesn't work? Is there something wrong withthe way I'm creating (or rather trying to create) the drop-down? I'll take an RTFM if you can point me to the page I need to read. I honestly want to learn, not to have someone else fix my problems . . . I've been fiddling with it for hours, and I'm just frustrated.

Regards,
Nedstar1
 
It's because you're using a doublequote inside of a doublequoted string.

Your string begins:

$content = "
<span class='style4'>(Fields Marked With * Are Required)</span></p>

goes on for multiple lines and then stays:

<option value="AL">Alabama</option>

PHP thinks the the quotes just before 'AL' are the end of the string, so it sees the 'AL' itself as a bare string. Hence the "unexpected T_STRING" parse error.

I recommend that you get in the habit, when outputting HTML from PHP, to use singlequotes to delineate strings. That way the internal doublequotes you use around HTML tag attribute values won't interfere with the string termination and you won't have to escape them:

Code:
        $content = '       
        <span class="style4">(Fields Marked With * Are Required)</span></p>
        <form action="address_confirmation.php" method="post">
        <table width="75%"  border="0" cellspacing="0" cellpadding="3">
          <tr>
            <td width="46%"><span class="style1">Address 1 *</span></td>
            <td width="54%"><input name="address1" type="text" value="$address1" id="address1" size="50" /></td>
          </tr>
          <tr>
            <td><span class="style1">Address 2 </span></td>
            <td><input name="address2" type="text" value="$address2" id="address2" size="50" /></td>
          </tr>
          <tr>
            <td><span class="style1">City *</span></td>
            <td><input name="city" type="text" value="$city" id="city" size="30" /></td>
          </tr>
          <tr>
            <td><span class="style1">State *</span></td>
            <td><select name="state" type="select" id="state">
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
    <option value="AZ">Arizona</option>
    <option value="AR">Arkansas</option>
    <option value="CA">California</option>
    <option value="CO">Colorado</option>
    <option value="CT">Connecticut</option>
    <option value="DE">Delaware</option>
    <option value="DC">District of Columbia</option>
    <option value="FL">Florida</option>
    <option value="GA">Georgia</option>
    <option value="HI">Hawaii</option>
    <option value="ID">Idaho</option>
    <option value="IL">Illinois</option>
    <option value="IN">Indiana</option>
    <option value="IA">Iowa</option>
    <option value="KS">Kansas</option>
    <option value="KY">Kentucky</option>
    <option value="LA">Louisiana</option>
    <option value="ME">Maine</option>
    <option value="MD">Maryland</option>
    <option value="MA">Massachusetts</option>
    <option value="MI">Michigan</option>
    <option value="MN">Minnesota</option>
    <option value="MS">Mississippi</option>
    <option value="MO">Missouri</option>
    <option value="MT">Montana</option>
    <option value="NE">Nebraska</option>
    <option value="NV">Nevada</option>
    <option value="NH">New Hampshire</option>
    <option value="NJ">New Jersey</option>
    <option value="NM">New Mexico</option>
    <option value="NY">New York</option>
    <option value="NC">North Carolina</option>
    <option value="ND">North Dakota</option>
    <option value="OH">Ohio</option>
    <option value="OK">Oklahoma</option>
    <option value="OR">Oregon</option>
    <option value="PA">Pennsylvania</option>
    <option value="RI">Rhode Island</option>
    <option value="SC">South Carolina</option>
    <option value="SD">South Dakota</option>
    <option value="TN">Tennessee</option>
    <option value="TX">Texas</option>
    <option value="UT">Utah</option>
    <option value="VT">Vermont</option>
    <option value="VA">Virginia</option>
    <option value="WA">Washington</option>
    <option value="WV">West Virginia</option>
    <option value="WI">Wisconsin</option>
    <option value="WY">Wyoming</option>
  </select></td>
          </tr>
          <tr>
            <td><span class="style1">Zip or Postal Code *</span></td>
            <td><input name="zip_postal" type="text" value="$zip_postal" id="zip_postal" size="25" /></td>
          </tr>
          <tr>
            <td><span class="style1">Contact Telephone Number *</span></td>
            <td><input name="telnum" type="text" value="$telnum" id="telnum" size="25" /></td>
          </tr>
          <tr>
          <td>&nbsp;</td>
          <td><input type="submit" value="Submit" /></td>
          </tr>
          </table>
          </form>
          <p><b>NOTE</b>: We cannot deliver to P.O. Boxes.</p>
          </form>';

Also, I strongly recommend against using extract() the way you do. It can lead to exactly the same security problems warned about here:
Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Hi sleipnir214,

I think that's going to work - thanks for the tip. I did not develop the site, but I can rework it to use the format you suggest. I take it that I am actually inverting the use of single and double quotes? Correct me if I am wrong.

With regards to the extract() - I'm not entirely sure if this use is insecure, as include.php has several includes of its own - I'll go through it to see if I can figure it out. I did see in the page you offered that
Code:
<?php
if(!empty($_GET)) extract($_GET);
?>
should solve the insecurity issue, but I'll need to take a few steps back and really look at how $_GET variables work/are handled to fully understand. Right now, I just know they work.

I was also going to argue that there is such a thing as a free lunch, just to be cheeky, but then I figured that it costs something to someone, so . . .

Anyhow,

Thanks a ton for the advice, here's a star for you.

Regards,
Nedstar1
 
Oh it vexes me.

I'm terribly terribly vexed.

The form is fixed now - the drop-down is where it should be etc etc.

The problem now is that the variable name appears as a default value in all other fields.

I inverted my use of single and double quotes.

Is this simply symptomatic of your quotes suggestion? Do I need to use a different method to declare the variable? Should I declare them in the header and go from there?

I'm so very very vexed.

Nedstar1
 
Yes, it actually is a symptom of the change. Since you used to delimit strings via double quotes before, all the php variables inside the quotes were parsed. That is not the case with single quotes. Variables within single quotes do not get parsed and get outputted literally. When you delimit strings with single quotes you need exit the string, concatenate the variable and return to string again. Like so:
Code:
'<td><input name="city" type="text" value="' . $city . '" id="city" size="30" /></td>'
This behaviour is well documented here
 
Vragabond gets a star as well!

The form is now nearly complete - the drop-down box displays as expected, and it records the address the way I need it to.

I have one remaining issue; the address information is redisplayed to allow the user to confirm the information, or alternatively, go back and make any required edits. Here's the code that sends the data back to the address.php page from address_confirmation.php :

Code:
<form name='form1' id='form1' method='post' action='address.php?address1=$address1&address2=$address2&city=$city&state=$state&zip_postal=$zip_postal&telnum=$telnum'>

This code is assigned to a button on address_confirmation by the templating process.

However, it reverts back to displaying "Alabama" instead of the previously selected state. I am guessing that this is due to specific syntax required to cause the drop-down to display the previously selected value. I am investigating this on the PHP site, but if anyone knows the issue at hand
off the top of their head, or knows what, exactly I need to be looking for (alwys happy to RTFM!), I'd be very grateful.

TIA, and best regards,
Nedstar1
 
Nedstar1 said:
I am guessing that this is due to specific syntax required to cause the drop-down to display the previously selected value.
Dead right you are again. The value in the select dropdown that is selected is determined by which option has set selected="selected" to it. As in
Code:
    <option value="UT" selected="selected">Utah</option>
for example. When that attribute is not present, the whole thing reverts to first option in the list being selected.

When you output the list you need to compare it against the value from the url. If the value matches, output the selected attribute, if not skip it.
 
Hi Vragabond,

Thanks for another helpful post. I'll try to rehash to make sure I understand, but I also have a question about methods.

Basically, when the user edits their address by pressing the appropriate button on address_confirmation.php, it passes the variables back to address.php in the URL. All of the text fields repopulate just from that. The drop-down requires that I recode the drop-down, referencing that variable so that, for example, if I live in Greensboro, NC, it does not redisplay as Greensboro, AL. Right? And I do this by adding a selected="selected" statement to the appropriate line after the <option value=> statement?

If that is correct, then I have this question:

Does the "selected" criteria HAVE to be inline with the selected value, or can I set it as I define the dropdown?

I was thinking this might work:

Code:
<tr>
            <td><span class="style1">State *</span></td>
            <td><select name="state" type="select" selected="<?php
if isset($state) echo ' . $state . '
?>"
id="state">
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
    <option value="AZ">Arizona</option>
// yada yada yada
    <option value="WI">Wisconsin</option>
    <option value="WY">Wyoming</option>
  </select></td>
          </tr>


Or does it need to compare each line individually? If so, I'm not sure how to go about this. I think I can get it to run the comparison, but how do you get it to insert the selected statement in the right place? Some kind of loop? I guess my real question is, do you have to re-generate the whole drop-down, or is there a way to replace just the one line, based on the comparison (if the selected has to be inline)?

I'm going to go mess with this. Thanks for all the help folks,

Please let me know if you have thoughts or suggestions.

Regards, and TIA

Nedstar1
 
There are many ways to solve your problems.
I would rather to use \" ... \" technique so my code won't be messy. For example:

$content = "
<span class=\"style4\">(Fields Marked With * Are Required)</span></p>
<form action=\"address_confirmation.php\" method="post">
<table width=\"75%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\"> ...

This way, I can put $var because it's inside " ".
Hope this helps ... :)

--
Mike FN
&quot;8 out of 10 Owners who Expressed a Preference said Their Cats Preferred Web Programmer.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top