简体   繁体   English

PHP表单-存在问题

[英]PHP Form - Having Issues

I've been having some serious problems getting my form to work properly. 我遇到一些严重的问题,无法正常使用我的表格。 Below are first the PHP calculations (attireCalculate.php), then the PHP processing file (processAttire.php), and finally my XHTML (attireForm.php). 以下是首先的PHP计算(attireCalculate.php),然后是PHP处理文件(processAttire.php),最后是我的XHTML(attireForm.php)。 Sorry for how long this all is, but I've been going through it over and over and over and looking all over the web to no avail. 很抱歉,这一切持续了多长时间,但我已经一遍又一遍地遍历整个网络,却无济于事。 I just can't find the problem(s). 我只是找不到问题。 Nothing happens when I click the submit button. 单击提交按钮没有任何反应。

I should mention that I am new to programming and PHP, so it may be that whatever I am missing is really quite simple. 我应该提一下,我是编程和PHP的新手,所以可能我所缺少的东西确实非常简单。 Thanks for your help. 谢谢你的帮助。 I really appreciate it. 我真的很感激。

attireCalculate.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);
}
?>

Here is the processAttire.php file. 这是processAttire.php文件。 I commented out the bit on tax exemption since there really is no point getting into that if I can't even get the thing to work as it is! 我评论了免税的问题,因为如果我什至无法使事情按原样进行,那真的没有意义!

<?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);
*/              

        ?>

And here is the XHTML: 这是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>        

And here is the JavaScript file containing validateAttireForm(): 这是包含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;
    }
}

Instead of mixing PHP and Javascript in the submit action of your form, call processAttire.php in the Javascript itself: 不要在表单的提交操作中混合使用PHP和Javascript,而是在Javascript本身中调用processAttire.php:

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

That way you'll have control what order your scripts are executed in. Note: You'll need to use JQuery for this. 这样,您就可以控制脚本的执行顺序。注意:您需要为此使用JQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM