Hi. I am new in using PHP and I am having a Parse Error: syntax error $end on the last number which is empty. Please, I need your help to help me find out where I did wrong or missing. I read all the posts here and did all possibility to correct but still giving me the problem. Maybe you can find it although I will keep trying. I will post the code twice as this is too long. Thanks.

<?php

function helper_checked($bool) {
  return $bool ? 'checked="checked"' : '';
}
function helper_selected($bool) {
  return $bool ? 'selected="selected"' : '';
}
function helper_disabled($bool) {
  return $bool ? 'disabled="disabled"' : '';
}
function safeEcho($fld) {
  echo htmlspecialchars($fld,ENT_QUOTES,'UTF-8');
}
function helper_error($fld) {
  global $error;
  if (!empty($error[$fld])) {
    echo '<p class="error">'.$error[$fld]."</p>\n";
  }
}

function highlight($fld) {
  global $error;
  return !empty($error[$fld]) ? 'error' : '';
}

include('templates/auw_test/forms/defaultregistration.php');

global $form, $error;
if (empty($form)) {
  $form = $defaults;
}


?>
<?php if (!empty($error)) : ?>
<div class="error">
<?php if (!empty($error['transaction'])) : ?>
<h3><?php echo $error['transaction']; ?></h3>
<?php else : ?>
<h3>There are errors.</h3>
<p>We are unable to process your request due to the following errors on the form.  Please correct them and resubmit the form.</p>
<ul>
<?php foreach ($error as $fld => $msg) : ?>
  <li><a href="#<?php echo $fld; ?>"><?php safeEcho($msg); ?></a></li>
<?php endforeach; ?>
</ul>
<p>Click on a link to be taken to the field with the problem.</p>
<?php endif; ?>
</div>
<?php endif; ?>
<fieldset clas="formsection">
<h2>2009 Annual Recognition Luncheon - April 17, 2009</h2>
<p>Please note: You cannot register online without paying by credit card.</p>
<h3>Early Bird by March 25th</h3>
<fieldset id="Guest_number" class="checkbox <?php echo highlight('Guest_number'); ?>">
    <legend>$50 per individual or $500 per table of ten if payment is received by March 25th</legend>
    <?php foreach ($preset_amounts as $amount) :?>
   <label><input name="Guest_number" class="checkbox" type="checkbox" 
                  value="<?php echo $Guest_number; ?>" <?php echo helper_checked($form['Guest_number'] == $amount) ?> 
                  onclick="$('Guest_number').disabled=true;" />
I wish to make reservations for 
<select id="Guest_number2" name="Guest_number2">
  <?php foreach ($number_select as $select => $prompt):?>
  <option value ="<?php echo $select; ?>" <?php echo helper_selected($select == $form['Guest_number2']);?>>
  <?php safeEcho($prompt);?>
  </option> 
  <?php endforeach;?> 
</select> 
persons at $50 per person.<?php echo $select; ?></label><?php endforeach;?>
<?php helper_error('Guest_number2');?>
<br />
<?php foreach ($preset_numbers as $Guest_number2) : ?>
<label><input name="Guest_number2" class="checkbox" type="checkbox" 
                  value="<?php echo $Guest_number2; ?>" <?php echo helper_checked($form['Guest_number2'] == $Guest_number2) ?> 
                  onclick="$('Guest_number2').disabled=true;" />
I wish to make reservations for 
<select id="Guest_number2" name="Guest_number2">
  <?php foreach($number_select as $code =>$prompt) : ?>
  <option value = "<?php echo $code; ?>" <?php echo helper_selected($code == $form['Guest_number']); ?>>
  <?php safeEcho($prompt); ?>
  </option>  
<?php endforeach;?> </select> table(s) at $500 per table.<?php echo $code; ?></label><?php endforeach;?> <?php helper_error('Guest_number');?><?php helper_error('Guest_number2');?> </div> </fieldset> </fieldset> <h3>After March 25th:</h3> <fieldset id="Guest_number" class="checkbox <?php echo highlight('Guest_number'); ?>"> <legend>$55 per inidividual or $550 per table of ten if payment is received by March 25th.</legend> <?php foreach ($preset_numbers as $Guest_number): ?> <label><input name="Guest_number" class="checkbox" type="checkbox" value="<?php echo $Guest_number; ?>" <?php echo helper_checked($form['Guest_number'] == $number) ?> onclick="$('Guest_number').disabled=false;" /> I wish to make reservations for <select id="Guest_number2" name="Guest_number2"> <?php foreach($number_select as $code => $prompt): ?> <option value = "<?php echo $code;?>" <?php echo helper_selected($code == $form['Guest_number']);?>> <?php safeEcho($prompt);?> </option> <?php endforeach;?> </select> persons at $55 per person.</label> <?php helper_error('Guest_number');?> <br /> <?php foreach ($person_numbers as $Guest_number) : ?> <label><input name="Guest_number" class="checkbox" type="checkbox" value="<?php echo $Guest_number; ?>" <?php echo helper_checked($form['Guest_number'] == $number) ?> onclick="$('Guest_number').disabled=false;" /> I wish to make reservations for <select id="Guest_number3" name="Guest_number3"> <?php foreach ($number_select as $code => $prompt) : ?> <option value ="<?php echo $code; ?>" <?php echo helper_selected($code == $form['Guest_number3']);?>> <?php safeEcho($prompt);?> </option> <?php endforeach; ?> </select> table(s) at $550 per table.</label> <?php helper_error('Guest_number3');?> </div> </fieldset> <fieldset id="fieldset-credit_card" class="formsection"> <legend class="hide">Credit Card Information</legend> <h2>Please enter your credit card details.</h2> <p>Online donations can only be accepted via credit or debit cards. Your details will be treated in the strictest confidentiality.</p> <fieldset id="registration_cc_type" class="radiogroup <?php echo highlight('registration_cc_type'); ?>"> <legend>Credit Card Type</legend> <label><input name="registration_cc_type" class="radio" type="radio" value="visa" <?php echo helper_checked($form['registration_cc_type'] == 'visa') ?> /> Visa</label> <label><input name="registration_cc_type" class="radio" type="radio" value="mastercard" <?php echo helper_checked($form['registration_cc_type'] == 'mastercard') ?> /> Master Card</label> <label><input name="registration_cc_type" class="radio" type="radio" value="amex" <?php echo helper_checked($form['registration_cc_type'] == 'amex') ?> /> AMEX</label> <?php helper_error('registration_cc_type'); ?> </fieldset> <div id="fld-registration_cc_number" class="formfield <?php echo highlight('registration_cc_number'); ?>"> <label for="registration_cc_number">Credit Card number:</label> <input id="registration_cc_number" name="registration_cc_number" class="text" type="text" maxlength="19" value="<?php safeEcho($form['registration_cc_number'])?>" /> <?php helper_error('registration_cc_number'); ?> </div> <div id="fld-registration_cc_code" class="formfield <?php echo highlight('registration_cc_code'); ?>"> <label for="registration_cc_code">Credit card security code: <span class="explain">(<a href="#">what is this?</a>)</span></label> <input id="registration_cc_code" name="registration_cc_code" class="text" type="text" maxlength="4" value="<?php safeEcho($form['registration_cc_code'])?>" /> <?php helper_error('registration_cc_code'); ?> </div>

    This is why there is a sticky thread harping on code format. Aside from the lack of readability, the error you are recieving is usually due to an un-closed loop or conditional statement. This can usually be easily avoided if you use and align opening and closing brackets.

    I refuse to go over your entire code, but here is an example of how it might look with properly formatted brackets:

    function highlight($fld)
      { 
        global $error; 
        return !empty($error[$fld]) ? 'error' : ''; 
      } 
    
    include('templates/auw_test/forms/defaultregistration.php'); 
    
    global $form, $error; 
    if (empty($form))
      { 
        $form = $defaults; 
      } 
    
    
    ?> 
    <?php 
    if (!empty($error))
      { ?> 
      <div class="error"> 
      <?php if (!empty($error['transaction'])) : ?> 
      <h3><?php echo $error['transaction']; ?></h3> 
    <?php
      }
    else 
      {
    ...?> 

    Do you see where this is going? If you bury your endifs and endfors, then you risk the unexpected $end issue. The code formatting should help you avoid such problems.

    One other suggestion on readability: if you've got to throw some html around some PHP output, just add it to your echo string. You'll find that it is much leaner and cleaner than jumping in and out of PHP constantly.

    Example (notice how single & double-quotes can be nested):

    <?php 
    if (!empty($error))
      {
      echo '<div class="error">';
      if (!empty($error['transaction'])) 
        echo "<h3>".$error['transaction']."</h3>";
      }
    else 
      {
    ...
    ?>
    

    Note: I'm not even sure I placed the brackets where they should be, but note how I skip the brackets ONLY when there is only one statement to execute. Otherwise, I'd stick with the bracket concept.

      Looks like you have at least 2 foreaches that are not closed with corresponding endforeaches at lines 94 and 109. You are also discovering how intermingling PHP with HTML in any but the most trivial of scripts can start to make your code very difficult to read and thus to maintain.

        I was going to help but that just kills my eyes. Try keep codes seperate, and inserting the vars into the html, that way you don't have functions and statements inside html. It kills the head switching back and forth between code so much to find a small issue as such.

          Not to mention the mix of if{...} and if: ... endif;.

          ixalmida wrote:

          This is why there is a sticky thread harping on code format.

          Namely here.

            Hi All,

            Thanks a lot for all your help. I am trying to fix it and I'll get back soon to let you know. Thanks for all your advices.

              Hi, I fix the $end error but there is a message showing an unexpected '}' in line 47 of my code. I checked how many times and I don't know why it is showing. I need that bracket because I have an if..else statement inside and like what I said, I don't know that much of php and I am still learning. I know my code is messy but I just need you to see part of the code where I am showing an error. Thanks for all your help and I hope to hear from you today.

              
              <?php 
              if (!empty($error)) 
                { ?> 
                <div class="error"> 
                <?php if (!empty($error['transaction'])) : ?> 
                <h3><?php echo $error['transaction']; ?></h3> 
              <?php 
                } /* this is the line that gives me error with the bracket on */
              else 
                { 
              
              <h3>There are errors.</h3>
              <p>We are unable to process your request due to the following errors on the form.  Please correct them and resubmit the form.</p>
              <ul> 
                } : ?>
              
              

                Remember what I said before about not jumping in and out of PHP all the time because it is confusing? I believe you've even confused the interpreter this time. Try the following:

                <?php 
                if (!empty($error)) 
                  {
                  echo '<div class="error">';
                  if (!empty($error['transaction']))
                    echo "<h3>".$error['transaction']."</h3>";
                  }
                else 
                  {
                ?>
                <h3>There are errors.</h3> 
                <p>We are unable to process your request due to the following errors on the form.  Please correct them and resubmit the form.</p> 
                <ul> 
                <?php
                  } 
                ?>
                  ixalmida wrote:

                  I believe you've even confused the interpreter this time

                  if (!empty($error))
                  {
                    if (!empty($error['transaction'])) :
                    echo $error['transaction'];
                  }
                  
                  Weedpacket wrote:

                  Not to mention the mix of if{...} and if: ... endif;.

                    Weedpacket pegged it as usual, but my code is still valid.

                    Maybe I'm a little anal, but I feel like if I have huge chunks of code enclosed by an if-statement, then maybe I should be writing a function instead.

                      Thanks a lot for your help. I finally made it work.

                        Write a Reply...