2010-05-22 44 views
0
<?php 

function VerifyForm(&$values, &$errors) 
{ 

    if (strlen($values['fname']) == 0) 
    $errors['fname'] = 'Enter First Name'; 

    if (strlen($values['lname']) == 0) 
    $errors['lname'] = 'Enter Last Name'; 

    if (strlen($values['mname']) == 0) 
    $errors['mname'] = 'Enter Middle Name'; 

    if (strlen($values['address']) == 0) 
    $errors['address'] = 'Enter Address'; 

    if (strlen($values['terms']) == 0) 
    $errors['terms'] = 'Please Read Terms and Agreement and Check the box.'; 

    if (!ereg('.*@.*\..{2,4}', $values['email'])) 
    $errors['email'] = 'Email address invalid'; 

    else if (strlen($values['email']) < 0) 
    $errors['email'] = 'Enter Email Address'; 

    return (count($errors) == 0); 
} 


function DisplayForm($values, $errors) 
{ 
    ?> 
    <!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> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>GIA Soap » Products » Customer Informations</title> 
    <link href="stylesheet/style.css" rel="stylesheet" type="text/css" /> 
    <script type="text/javascript" src="js_files/jquery.js"></script> 
    <script type="text/javascript" src="js_files/sliding_effect.js"></script> 
    <script type="text/javascript" src="js_files/slideshow.js"></script> 
    </head> 
<body> 
    <div class="bg_top"> 
    <div class="bg_bottom"> 
    <div class="wrapper"> 
    <div class="header"> 
    <div class="logo"> 
    </div> 
    <div class="logo_text"> 
    <div class="logo_head_text">Gia Soap Making</div> 
    <div class="logo_sub_text">Sub text here</div> 
    </div> 
    </div> 
    <div class="h_nav"> 
    <div class="h_nav_dash"> 

    </div> 
    </div> 
    <div class="container"> 
    <div class="content_term"> 
    <div class="content_terms"> 
    <br /> 
    <h1><p>Customer Information</p></h1><br /> 
    <p>Please the following correctly.</p> 
    <div class="customer_info"> 

    <?php 

    if (count($errors) > 0) 
    echo "<p>There were some errors in your submitted form, please correct them and try again.</p>"; 

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

    <!-- hidden values --> 

    <input type="hidden" value="<?php echo $papaya; ?>" name="papaya" /> 
    <input type="hidden" value="<?php echo $carrot; ?>" name="carrot" /> 
    <input type="hidden" value="<?php echo $guava; ?>" name="guava" /> 

    <label for="customer_fname">First Name (<i>Required</i>)</label> 
    <input type="text" class="textbox" id="customer_fname" name="customer_fname" value="<?= htmlentities($values['fname']) ?>" /> 
    <span class="error_msg"><?= $errors['fname'] ?></span> 

    <label for="customer_lname">Last Name (<i>Required</i>)</label> 
    <input type="text" class="textbox" id="customer_fname" name="customer_fname" value="<?= htmlentities($values['lname']) ?>" /> 
    <span class="error_msg"><?= $errors['lname'] ?></span> 

    <label for="customer_mname">Middle Name (<i>Required</i>)</label> 
    <input type="text" class="textbox" id="customer_fname" name="customer_fname" value="<?= htmlentities($values['mname']) ?>" /> 
    <span class="error_msg"><?= $errors['mname'] ?></span> 

    <label for="customer_add">Address (<i>Required : Complete Address Please</i>)</label> 
    <input type="text" class="textbox" id="customer_add" name="customer_add1" value="<?= htmlentities($values['address']) ?>" /><br /> 
    <input type="text" class="textbox" id="customer_add" name="customer_add2" /><br /> 
    <input type="text" class="textbox" id="customer_add" name="customer_add3" /> 
    <span class="error_msg"><?= $errors['address'] ?></span> 

    <label for="customer_email">Email Address (<i>Required</i>)</label> 
    <input type="text" class="textbox" id="customer_email" name="customer_email" value="<?= htmlentities($values['email']) ?>" /> 
    <span class="error_msg"><?= $errors['email'] ?></span> 

    <label for="customer_phone">Phone Number </label> 
    <input type="text" class="textbox" id="customer_phone" name="customer_phone" /> 

    <label for="customer_mobile">Mobile Number </label> 
    <input type="text" class="textbox" id="customer_mobile" name="customer_mobile" /> 

    <br /><br /> 

    <div class="terms"> 
    <center> 
    <h1>Terms and Agreement</h1><br /> 
    <p>Please read the following.</p><br /> 
    </div> 
    <br /> 

    <input type="checkbox" name="terms" value="<?= htmlentities($values['terms']) ?>" /> I Read the Terms and Agreement<br /><br /> 
    <span class="error_msg"><?= $errors['terms'] ?></span> 
    <input type="submit" value="Send Order" class="prod_subbtn" /> 

    </center> 

    </form> 
    </div> 
    </div> 
    </div> 
    <div class="clear"></div> 
    </div> 
    <?php include ('includes/footer.php'); ?> 
    </div> 
    </div> 
    </div> 
    </body> 
    </html> 
<?php 

} 


function ProcessForm($values) 
{ 
    $papaya = $_POST['papaya']; 
    $carrot = $_POST['carrot']; 
    $guava = $_POST['guava']; 
    $fname = $_POST['fname']; 
    $lname = $_POST['lname']; 
    $mname = $_POST['mname']; 
    $address = $_POST['address']; 
} 

if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
    $formValues = $_POST; 
    $formErrors = array(); 

    if (!VerifyForm($formValues, $formErrors)) 
    DisplayForm($formValues, $formErrors); 
    else 
    ProcessForm($formValues); 
} 
else 
    DisplayForm(null, null); 

?> 

的输出是:
Screenshot of output形式PHP代码被输出到屏幕

问题
是应该放在字段值能够由用户看到的PHP代码。

+0

可能重复的[PHP代码没有被执行(我可以看到它的页面源代码)](http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed- i-can-see-it-on-source-code-of-code) – Gajus 2014-04-10 06:36:46

回答

9

机会是short_open_tags已关闭。使用<?php echo ...; ?>代替<?=... ?>,像这样:

<?php echo htmlentities($values['lname']); ?> 
+0

从PHP 5.4.0开始,'<?='指令总是*可用,不再受'short_open_tags'影响。 – 2017-04-16 19:15:51

0

该指令的短标签被设置为关闭在php.ini。这不允许<? $phpcode ?><?=$monkey?>

允许的只有一个是<?php $monkeybusiness ?>

0

要么改变<?=<?php echo或在php.ini

+0

我不会推荐后者。由于可移植性等原因,这通常不是一个好主意。 – 2015-02-05 09:59:54

1

<?= $errors['fname'] ?>等于<?php echo $errors['fname'] ?>short_open_tags = on
<?=被称为“短标签”,从php中删除(不赞成使用)。
使用<?php echo $errors['fname']; ?>查看实际变量值。

+0

'<?='指令从PHP 5.4.0开始始终可用,不再受['short_open_tags'](http://php.net/manual/en/ini.core.php#ini)影响。短的开放式标签)。 – 2017-04-16 20:04:56