PHP表单 - 有问题

时间:2013-12-21 11:21:59

标签: php

我一直有一些严重的问题让我的表单正常工作。下面首先是PHP计算(attireCalculate.php),然后是PHP处理文件(processAttire.php),最后是我的XHTML(attireForm.php)。对不起,这一切都是多久,但我一直在经历它一遍又一遍地看着网络无济于事。我只是找不到问题。单击提交按钮时没有任何反应。

我应该提一下,我是编程和PHP的新手,所以不管我缺少什么都非常简单。谢谢你的帮助。我真的很感激。

attireCalculate.php

<?php
function message($ramHatQ, $teemoHatQ, $blitzHoodieQ, $galioHoodieQ, $datAsheQ, $graggyIceQ)
{
         $cost = sprintf("%.2lf", calculateCost($ramHatQ, $teemoHatQ, $blitzHoodieQ, $galioHoodieQ, $datAsheQ, $graggyIceQ));
         $tax = sprintf("%.2lf", calculateTax($cost));
         $total = sprintf("%.2lf", calculateTotal($cost, $tax));
         $text = "<h1>Product Summary</h1>".
                 "<h3>The cost of items selected is $".$cost.".<br />".
                 "The tax applied is $".$tax.".<br />".
                 "Therefore, the total cost is $".$total.".</h3>";
         return $text;
}

function calculateCost($ramHatQ, $teemoHatQ, $blitzHoodieQ, $galioHoodieQ, $datAsheQ, $graggyIceQ)
{    
         return ($ramHatQ+$teemoHatQ)*14.99+($blitzHoodieQ+$galioHoodieQ)*24.99+($datAsheQ+$graggyIceQ)*19.99;                          
}

function calculateTax($cost)
{
         return $cost*0.075;
}

function calculateTotal($cost, $tax)
{
         return $cost+$tax;
}

function mailSummary($email, $message)
{
         $header  = 'MIME-Version: 1.0' . "\r\n";
         $header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
         $header .= "From: EMAIL\r\n";
         mail($email, "Product Summary from Riot Store Online", $message, $header);
}
?>

这是processAttire.php文件。我评论了关于免税的问题,因为如果我甚至无法让事情发挥作用,那就没有意义了。

<?php
        include 'attireCalculate.php';

        $message = message($_POST['ramHatQ'], $_POST['teemoHatQ'], $_POST['blitzHoodieQ'], $_POST['galioHoodieQ'], $_POST['datAsheQ'], $_POST['graggyIceQ']);
        echo $message;

        if ($_POST['wantMail'])
        {
          mailSummary ($_POST['email'], $message);
            echo "<h2>A summary has been sent to you via e-mail.</h2>";
        }
/*      
        if ($_POST['taxExemptStatus'])
        {
                calculateCost($ramHatQ, $teemoHatQ, $blitzHoodieQ, $galioHoodieQ,
                   $datAsheQ, $graggyIceQ);
                echo "<h2>Your tax-exempt status has been taken into account. Tax has been deducted from total cost.</h2>";
        }
        else
                calculateTax($totalCost);
*/              

        ?>

这是XHTML:

<form id="attireForm" onsubmit="return validateAttireForm()"
        action="scripts/processAttire.php" method="post">
      <fieldset>
        <legend>Headwear</legend> 
        <table id="attireTable" summary="Headwear">
          <tr>
            <td>Rammus Hat</td>
                        <td>$14.99</td>
                        <td><label for="ramHatType">Type: </label></td>
                        <td><select id="ramHatType" name="ramHatType">
                            <option>Original</option>
                            <option>Ninja</option>
                        </select></td>  
            <td><label for="ramHatQ">Quantity: </label></td>
            <td><input id="ramHatQ" type="text" name="ramHatQ" size="1" value="0" /></td>
          </tr>
                    <tr>
                      <td>Teemo Hat</td>
                        <td>$14.99</td>
                        <td></td>
                        <td></td>
                        <td><label for="teemoHatQ">Quantity: </label></td>
                        <td><input id="teemoHatQ" type="text" name="teemoHatQ" size="1" value="0" /></td>
                    </tr>       
         </table>
      </fieldset>
      <fieldset>
        <legend>Hoodies</legend> 
        <table id="attireTable" summary="Hoodies">
          <tr>
                      <td>Blitzcrank Hoodie</td>
                        <td>$24.99</td>
                        <td><label for="blitzHoodieQ">Quantity: </label></td>
                        <td><input id="blitzHoodieQ" type="text" name="blitzHoodieQ" size="1" value="0" /></td>
          </tr>
          <tr>
            <td>Galio Hoodie</td>
                        <td>$24.99</td>
                        <td><label for="galioHoodieQ">Quantity: </label></td>
                        <td><input id="galioHoodieQ" type="text" name="galioHoodieQ" size="1" value="0" /></td>
          </tr>
        </table>
      </fieldset>
            <fieldset>
              <legend>T-Shirts</legend>
                <table id="attireTable" summary="T-Shirts">
                  <tr>
                      <td>"Dat Ashe" T-Shirt</td>
                        <td>$19.99</td>
                        <td><label for="datAsheQ">Quantity: </label></td>
                        <td><input id="datAsheQ" type="text" name="datAsheQ" size="1" value="0" /></td>
                    </tr>
                    <tr>
                      <td>"Graggy Ice" T-Shirt</td>
                        <td>$19.99</td>
                        <td><label for="graggyIceQ">Quantity: </label></td>
                        <td><input id="graggyIceQ" type="text" name="graggyIceQ" size="1" value="0" /></td>
                    </tr>
                </table>            
            </fieldset> 
            <fieldset>
              <legend>Record?</legend>
                <table summary="Record?">
                  <tr>
                      <td><label for="wantMail">Would you like a record sent to you by e-mail? </label></td>
                        <td><input id="wantMail" type="checkbox" name="wantMail" value="yes" /></td>
                    </tr>
                    <tr>
                      <td><label for="email">E-mail Address: </label></td>
                        <td><input id="email" type="text" name="email" size="25" /></td>
                    </tr>   
                </table>
            </fieldset> 
            <fieldset>
        <legend>Processing</legend> 
        <table summary="Processing">
                  <tr>
                      <td><label for="taxExemptStatus">Do you have tax-exempt status? </label></td>
                        <td><input id="taxExemptStatus" type="checkbox" name="taxExemptStatus" value="yes" /></td>
                    </tr>
          <tr>
            <td><input type="submit" value="Add to Cart" /></td>
            <td><input id="resetAttire" type="reset" value="Reset Selections" /></td>
                    </tr>
        </table>
      </fieldset>
        </form>        

这是包含validateAttireForm()的JavaScript文件:

//attireValidate.js

function validateAttireForm()
{
     var attireFormObj = document.getElementById("attireForm")
     var ramHatQ = attireFormObj.ramHatQ.value;
     var teemoHatQ = attireFormObj.teemoHatQ.value;
     var blitzHoodieQ = attireFormObj.blitzHoodieQ.value;
     var galioHoodieQ = attireFormObj.galioHoodieQ.value;
     var datAsheQ = attireFormObj.datAsheQ.value;
     var graggyIceQ = attireFormObj.graggyIceQ.value;
     var email = attireFormObj.email.value;

     var quantities = new Array(ramHatQ, teemoHatQ, blitzHoodieQ, galioHoodieQ, datAsheQ, graggyIceQ); 

     for (q in quantities)
     {
         quantityOK = validateQuantity(quantities[q]);
     if (quantityOK = false)
         {
         break;
         }
     }

     if (attireFormObj.wantMail.checked)
        emailOK = validateEmail(email);
     else
        emailOK = true;

    return quantityOK && emailOK;
}

function validateQuantity(quantity)
{
    if (isNaN(quantity))
    {
        alert("Error: One or more quantities are abnormal. Please input a number for quantity.")
        return false;
    }

    if (quantity < 0 || quantity > 100)
    {
        alert("Error: Quantity must be in the range 0-100 units.")
        return false;
    }

    return true;
}

function validateEmail(address)
{
    var p = address.search(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})$/);
    if (p == 0)
        return true;
    else
    {
        alert("Error: Invalid e-mail address.")
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

不要在表单的提交操作中混合使用PHP和Javascript,而是在Javascript中调用processAttire.php:

$.get('scripts/processAttire.php', function(data) {
   eval(data);
});

这样你就可以控制你的脚本执行的顺序。注意:你需要使用JQuery。