Rendering issue with PHP... can you help?

Hi All,

I have a registration script, which i’ve included below.

In terms of error handling and validation it’s great. But it doesn’t render properly in IE or Chrome.

Safari and FF look perfectly fine, displaying the errors in a list and the form remains below.

However, the other 2 browsers list the errors correctly, but the form beneath seems to end up all over the place and I cant seem to work out why…

Any ideas?


<?php
include "conn.inc.php";

function account_exists($column, $value) {
    $accounts = mysql_query('SELECT * FROM businesses WHERE ' . $column . ' = "' . $value . '"');

    $return = (mysql_num_rows($accounts) > 0) ? true : false;

    mysql_free_result($accounts);

    return $return;
}

//	Check Email Address courtesy of Dave Child:
//	http://www.ilovejackdaniels.com/php/email-address-validation/
function check_email_address($email) {

    if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email))
        return false;

    $email_array = explode("@", $email);
    $local_array = explode(".", $email_array[0]);

    for ($i = 0; $i < sizeof($local_array); $i++) {
        if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\\.-]{0,63})|(\\"[^(\\\\|\\")]{0,62}\\"))$", $local_array[$i]))
            return false;
    }

    if (!ereg("^\\[?[0-9\\.]+\\]?$", $email_array[1])) {

        $domain_array = explode(".", $email_array[1]);

        if (sizeof($domain_array) < 2)
            return false;

        for ($i = 0; $i < sizeof($domain_array); $i++) {
            if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i]))
                return false;
        }
    }

    return true;
}

$spam_check = ZGF;




if (isset($_POST['submit'])) {
    $business_name = ($_POST['business_name']);
    $url = ($_POST['url']);
    $category = ( $_POST['category']);
    $email = ( $_POST['email'] );
    $description = ($_POST['description'] );
    $location = ($_POST['location'] );
    $spam_check = $_REQUEST['spam_check'];
    $accept = $_REQUEST['accept'];
  



    if (empty($business_name)) {
        $errors[] = "You must enter your Business Name<br>";
    } else if (account_exists('business_name', $business_name)) {
        $errors[] = "The Business Name <b>'$business_name'</b> already exists<br>";
    }

    if (empty($category)) {
        $errors[] = "You must enter your category<br>";
    }

    //	Email address checks
    if (empty($email)) {
        $errors[] = "You must enter your Email Address<br>";
    } else if (!check_email_address($email)) {
        $errors[] = "The Email Address appears to be invalid<br>";
    }

    if (empty($description)) {
        $errors[] = "You must enter a Description<br>";
    }
    if (empty($spam_check)) {
        $errors[] = "You must enter an anti-spam answer<br>";
    }
    if ($spam_check != "ZGF") {
        $errors[] = "Your spam entry was invalid<br>";
    }
 if (empty($accept)) {
        $errors[] = "You must accept the terms of use!<br>";
    }


    if (!isset($errors)) {
        @mysql_query("INSERT INTO `businesses`(business_name, url, category,email,description,location,date_registered) VALUES ('$business_name', '$url','$category','$email','$description', '$location', CURDATE())") or die(mysql_error());
        {//send email
            $business_name = stripslashes($_POST['business_name']);
            $url = stripslashes($_POST['url']);
            $email = stripslashes($_REQUEST['email']);
            $category = stripslashes($_REQUEST['category']);
            $description = stripslashes($_REQUEST['description']);
            $location = stripslashes($_REQUEST['location']);
            
            
            $email_message = "A business has just registered on Business-Reg. You can find their details below: \
 \
 \
 Business Name: \
 {$business_name} \
 \
 Business URL:\
 {$url} \
\
  Email Address: \
 {$email} \
 \
  Category: \
 {$category} \
 \
 Description: \
 {$description} \
 \
 Location: \
 {$location}";

            mail('info@theyorkshireweb.com', 'Business Reg Submission', $email_message, 'From: ' . $email . "\\r\
");
            
        }

        header('location: thanks.php');
        exit;
    }
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

    <head>

        <title>business-reg | Register your business with business-reg.com </title>

        <link rel="stylesheet" type="text/css" href="stylesheets/styles.css" />

        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />




    </head>

    <body>

        <div id="container">



            <div id="header">	</div>

            <div id="navigation">

                <ul>
                    <li><a href="index.php">Home </a></li>
                    <li><a href="search.php">Search </a></li>
                    <li><a href="about.php">About Us </a></li>
                    <li><a href="faq.php">FAQ </a></li>
                    <li><a href="contact.php">Contact </a></li>
  
                </ul>



            </div>




            <div id="left_content">


                <table>

                    <tr>
                        <td><img src="/images/footer.jpg" alt="lightbulb" title="Testimonial - Chat Button"> </td>
                        <td> <a href="index.php"> Home </a>

                        </td>
                    </tr>

                    <tr>
                        <td><img src="http://www.sitepoint.com/forums/images/lightbulb_off.png" alt="lightbulb" title="Testimonial - Chat Button"> </td>
                        <td> <a href="advertise.php"> Advertise With Us </a>

                        </td>
                    </tr>

                    <tr>
                        <td><img src="http://www.sitepoint.com/forums/images/lightbulb_off.png" alt="lightbulb" title="Testimonial - Chat Button"> </td>
                        <td> <a href="why.php"> Why PAT Test? </a>

                        </td>
                    </tr>

                    <tr>
                        <td><img src="http://www.sitepoint.com/forums/images/lightbulb_off.png" alt="lightbulb" title="Testimonial - Chat Button"> </td>
                        <td> <a href="pricing.php"> Pricing Guide  </a>

                        </td>
                    </tr>

                    <tr>
                        <td><img src="http://www.sitepoint.com/forums/images/lightbulb_off.png" alt="lightbulb" title="Testimonial - Chat Button"> </td>
                        <td> <a href="faqs.php"> FAQ's </a>

                        </td>
                    </tr>

                </table>


            </div>



            <div id="main_content">

                <h3> Hello and Welcome  </h3>

                <?php
//	Error report goes here if there are errors :)

                if (isset($errors)) {

                    echo "<div id=errors";

                    echo "\	\	\	\	", '<p class="form_text">The following errors occurred while processing your request. Please try again.</p>', "\
\
";
                    echo "\	\	\	\	", "\
\
";


                    foreach ($errors as $error)
                        echo "", $error, "\
";

                    echo "\	\	\	\	", "\
\
";

                    echo "</div";
                }
                ?>





                <div id="stylized" class="myform">	
                    <fieldset>
                        <form  action="index.php" method="post">

                            <h1>Business Registration</h1>
                            <p>Please enter the form below to register your business with business-reg</p>

                            <label>Business Name
                                <span class="small">Add your business name</span>
                            </label>
                            <input type="text" name="business_name" id="business_name" value="<?php echo $business_name; ?>" />

                     <label>Web Address
                                <span class="small">Eg: http://business-reg.com</span>
                            </label>
                            <input type="text" name="url" id="url" value="http://" />
                            
                            <label> Category
                                <span class="small">Please add a category</span>
                            </label>
                            <select name="category" size="1" id="category">
                                
                                <option selected="selected">Please Select</option>  
                                <option value="accomodation">Accomodation</option>
                                <option value="accountancy">Accountancy</option>
                                <option value="accupuncture">Accupuncture</option>
                                <option value="building">Building</option>
                                <option value="computing">Computing</option>
                                <option value="dental care">Dental Care</option>
                                <option value="electrical">Electrical</option>
                                <option value="fabricators">Fabricators</option>
                                <option value="gardening">Gardening</option>
                                <option value="government">Government</option>
                                <option value="health">Health</option>
                                <option value="insurance">Insurance</option>
                                <option value="journalism">Journalism</option>
                                <option value="livery">Livery</option>
                                <option value="mechanic">Mechanic</option>
                                <option value="nursing">Nursing</option>
                                <option value="plumbing">Plumbing</option>
                                <option value="rescue">Rescue</option>
                                <option value="mechanic">Mechanic</option>


                            </select>

                            <label>Business Email
                                <span class="small">Add a valid address</span>
                            </label>
                            <input type="text" name="email" id="email" value="<?php echo $email; ?>" />

                            <label>Description
                                <span class="small">Please describe your business</span>
                            </label>
                            <textarea cols="22" rows="10" id="description" name="description"><?php echo $description; ?></textarea>

                            <label>Location/City
                                <span class="small">The location of your business</span>
                            </label>
                            <select name="location" size="1" id="category">
                                
                                <option selected="selected">Please Select</option>  

                                <option value="bath">Bath</option>
                                <option value="birmingham">Birmingham</option>
                                <option value="bradford">Bradford</option>
                                <option value="brighton and hove">Brighton and Hove</option>
                                <option value="bristol">Bristol</option>
                                <option value="cambridge">Cambridge</option>
                                <option value="canterbury">Canterbury</option>
                                <option value="carlisle">Carlisle</option>
                                <option value="chester">Chester</option>
                                <option value="chichester">Chichester</option>
                                <option value="coventry">Coventry</option>
                                <option value="derby">Derby</option>
                                <option value="durham">Durham</option>
                                <option value="ely">Ely</option>
                                <option value="exeter">Exeter</option>
                                <option value="gloucester">Gloucester</option>
                                <option value="hereford">Hereford</option>
                                <option value="kingston upon hull">Kingston upon Hull</option>
                                <option value="lancaster">Lancaster</option>
                                <option value="leeds">Leeds</option>
                                <option value="leicester">Leicester</option>
                                <option value="lichfield">Lichfield</option>
                                <option value="lincoln">Lincoln</option>
                                <option value="liverpool">Liverpool</option>
                                <option value="city of london">City of London</option>
                                <option value="manchester">Manchester</option>
                                <option value="newcastle upon tyne">Newcastle upon Tyne</option>
                                <option value="norwich">Norwich</option>
                                <option value="nottingham">Nottingham</option>
                                <option value="oxford">Oxford</option>
                                <option value="peterborough">Peterborough</option>
                                <option value="plymouth">Plymouth</option>
                                <option value="portsmouth">Portsmouth</option>
                                <option value="preston">Preston</option>
                                <option value="ripon">Ripon</option>
                                <option value="salford">Salford</option>
                                <option value="salisbury">Salisbury</option>
                                <option value="sheffield">Sheffield</option>
                                <option value="southampton">Southampton</option>
                                <option value="st albans">St Albans </option>
                                <option value="stoke-on-trent">Stoke-on-Trent </option>
                                <option value="sunderland">Sunderland </option>
                                <option value="truro">Truro </option>
                                <option value="wakefield">Wakefield </option>
                                <option value="wells">Wells </option>
                                <option value="westminster">Westminster </option>
                                <option value="winchester">Winchester </option>
                                <option value="wolverhampton">Wolverhampton </option>
                                <option value="worcester">Worcester </option>
                                <option value="york">York </option>
                            </select>
                            
                            

                            <label>Spam Checker
                                <span class="small">Enter the last 3 characters</span>
                                <img src="http://www.sitepoint.com/forums/images/spam.jpg">
                            </label>
                            <input type="text" name="spam_check" id="spam_check" </input>
                            
                                   
                              <button type="submit" name="submit" id="submit"></button>
                              
                                <input type="checkbox" name="accept" id="accept" value="accept" title="accept" </input>
                                <span class="small">I accept that registering will display my business information submitted above to any user of business-reg.</span> 
                    
                                <div class="spacer"></div>

                        </form>

                    </fieldset>
                    
                    




                </div>

            </div>


            <div id="footer"> <p> © business-reg 2011 <br />
					 Website Design: theyorkshireweb.com <br />
                </p> 



            </div>
        </div>

    </body>

</html>

That depends on your database structure.

Do you have a field in the users table which contains the date at which people signed up?


$rs = mysql_query("SELECT username FROM users ORDER BY registration_date DESC LIMIT 5", $link);

$newly_registered = array();
while($row=mysql_fetch_assoc($rs)) {
    $newly_registered[] = $row['username'];
}
echo "New users: " . implode(', ', $newly_registered);

D’OH…

I worked it out. the php:


<?php
//	Error report goes here if there are errors 

if (isset($errors)) {

echo "<div id=errors";

echo "\	\	\	\	", '<p class="form_text">The following errors occurred while processing your request. Please try again.</p>', "\
\
";
echo "\	\	\	\	", "\
\
";


foreach ($errors as $error)
echo "", $error, "\
";

echo "\	\	\	\	", "\
\
";

echo "</div";
}
?>

had the closing div’s (>) missing. looked ok in previous blowsers.

Now it’s sorted you can ignore this!! :slight_smile: Thanks to anyone who already started to investigate! :slight_smile:

I was just about to post that :slight_smile:

A tip for future questions: wrap your code in [ code] and [/ code] (without the spaces) or [ php] and [/ php] (again, without the spaces). Makes it lots easier to read!

Thanks, will do.

SIde note really, but if I wanted to display the last say, 5 users to have registered, what’s the easiest way to do it?

I was going to re-use some php/mysql to do this, but not done this bit before…


SELECT foo, bar FROM table ORDER BY date_registered DESC LIMIT 5

:slight_smile:

Off Topic:

Immerse beat me to it with a much better example

Yep, that works… Cheers.

I’d written this: As a learning curve, what’s wrong with it?

$mysqlquery = mysql_query( "SELECT * FROM businesses order by date_registered limit 5" ) or die( mysql_error());

	  $row =  mysql_fetch_assoc($mysqlquery);
  
  {
  

		echo $row['business_name']." </b>"; 
                echo $row['location']." </b>"; 
            
  
  }



It won’t run? :wink:

ha - well, it does, but only returns the 1st entry in the table.

The other post solves my problem, but I just needed to know where mine goes wrong so I can learn from it.

Many thanks :0

You have your curly braces in place for a loop, but no loop code actually in place (e.g. for, foreach, while or whatever). :slight_smile:

It won’t, but anyway. :slight_smile:


<?php
$sql = 'SELECT * FROM businesses ORDER BY date_registered DESC LIMIT 5;';
$res = mysql_query($sql);

if(!is_resource($res)){
  echo 'Query Failed: ', $sql, PHP_EOL;
  echo 'Error: ', mysql_error(), PHP_EOL;
  exit;
}

while($business = mysql_fetch_assoc($res)){
  echo $business['business_name']; #etc
}

I stand corrected! I apologise whole heartedly. This is valid code, well I never.


<?php
$foo = true;

{
  $foo = false;
}

var_dump($foo);
/*
  bool(false)
*/