1

Okay, the code is long and boring but I am at a loss.

Basically I have a form, and the form gets put into an array of objects. The object name is Person().

However if they have not filled out the form correctly, it will display an error, such as "Email cannot be left blank".

The problem is that if there are errors in the form, the page dies at a very specific place.

At this point, the object looks like this:

Array
(
    [0] => Person Object
        (
            [firstname:Person:private] => Richard
            [lastname:Person:private] => Gert
            [gender:Person:private] => 
            [age:Person:private] => 
            [add1:Person:private] => 
            [add2:Person:private] => 
            [add3:Person:private] => 
            [add4:Person:private] => 
            [postcode:Person:private] => 
            [country:Person:private] => 
            [phone:Person:private] => 3299394
            [email:Person:private] => right@left.com
            [price:Person:private] => 67.5
            [additional:Person:private] => 
            [active:Person:private] => 1
            [ref:Person:private] => c75af
            [ticketref:Person:private] => 0acbc
            [org:Person:private] => RA
            [type:Person:private] => wc
        )

)

And that's fine. The page is dying though at this bit:

<tr><td class="top left" width="200px">First Name</td>
<td class="top right">
<input type="text" class="txt" name="firstname" value="<?=$people[0]->firstname();?>">
</td></tr>

And the function for firstname is:

function firstname()        {if($this->firstname) return($this->firstname);}

Same as every other function really in the person class.

But why is it failing here even when there is a firstname to output? It gives no errors, in fact the HTML at this point looks like this:

<tr><td class="top left" width="200px">First Name</td><td class="top right">

The page halts or dies even before it writes the words <input....

I cannot figure out why.

*Error reporting is most definitely turned on. Also - If the form is filled out correctly (email, telephone number etc) the code runs absolutely fine. The object is filled and the user is moved on to stage 2. *

Chud37
  • 4,907
  • 13
  • 64
  • 116

2 Answers2

2

Short tags?

It is generally not a good idea to use the short tags (they are turned off automatically from php 5.3, you have to enable them in the php.ini: are you sure the short tags are enabled?, see also this link: PHP echo vs PHP short tags

Otherwise it might not be a bad idea to rewrite in the generally accepted php 5.3+ way:

<?php echo $people[0]->firstname(); ?>

Error reporting

If this does not give a statisfactory answer, try enabling error reporting in php:

on the fly (somewhere at the beginning of your script)

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>

in php.ini (probably different for php and cli)

error_reporting = E_ALL
display_errors = 1

For more information see:

Apache logs

If all else fails, try using the apache logs (error logs), errors which are not logged to the browser may still end up in your apache (or lighttp/nginx/iss) error log.

Community
  • 1
  • 1
Arend
  • 3,741
  • 2
  • 27
  • 37
  • Those error reporting lines are already there. The short tags definitely work on this server too. – Chud37 Jul 05 '13 at 12:08
  • What about the webserver? Anything? What does the log say? 200 request? Error 500? – Arend Jul 05 '13 at 12:15
-2

It is generally not good practice to mix code and layout (google for MVC).

My suggestion:

Generate your form with a regular HTML-page, that looks something like this:

<span class="error">{error}</span>

<table>
    <tr><td class="top left" width="200px">First Name</td>
    <td class="top right">
        <input type="text" class="txt" name="firstname" value="{firstname}">
    </td></tr>
</table>

The vars in curly braces are placeholders that you're going to replace in the next step. {error} will hold your error-message, when the user submits the form and you detect an error.

In your PHP script load the HTML with something like:

$template = file_get_contents('template.html');

On first call to the form, replace the vars in curly braces with empty values in a loop:

foreach($this->person as $key=>$value){
    $template = str_replace('{'.$key.'}', '', $template);
}

// Replace the error-placeholder with an empty value

$template = str_replace('{error}', '', $template);

Output the form:

echo $template;

Now, when the user submits the form, check the postvars for errors and if an error occurs, replace the values in the form with the postvars, instead of empty values and finally replace the error-placeholder with your error-message:

foreach($this->person as $key=>$value){
    $template = str_replace('{'.$key.'}', $_POST[$key], $template);
}

// Replace the error-placeholder with an empty value

$template = str_replace('{error}', $myErrorMessage, $template);

This will preserve the user's entries in the form.

Hope you get the concept. By using this approach, it will be much easier to find errors in your code and you won't have to worry about short-tags being on and other issues that occur when you mix HTML/PHP.

Swissdude
  • 3,486
  • 3
  • 35
  • 68
  • This is nice, and I understand about MVC, but it doesnt solve this problem. It just re-writes the entire site. – Chud37 Jul 05 '13 at 12:52
  • But it would help you finding out where your error occurs... The problem you have would most likely not even occur when you followed this pattern. – Swissdude Jul 05 '13 at 13:02
  • Yes, and I like the pattern, but problem solving cannot be simply 'start again a different way'. – Chud37 Jul 05 '13 at 13:19
  • ;) well, it's your code and project. All I suggest is that by using a different approach, you won't have to deal with such problems in the first place and thus make your coding-life easier. When problems arise from the coding-pattern you use, it'd be definitely worth the thought of changing the pattern. To get to your original problem, though, did you try using firstname();?> instead of the short-tag? What happens if you change your function and echo $this->firstname? Like: function firstname(){echo $this->firstname; if($this->firstname) return($this->firstname);} – Swissdude Jul 05 '13 at 13:31