在if语句中使用$ _POST

时间:2016-08-04 08:20:17

标签: php html

假设我有以下html代码:

<FORM name="myForm" action="confirmsold.php" method="post" onsubmit="return validateFormCash()">
    <table border=0 width="300" cellspacing=1 cellpadding=3 bgcolor="#353535" align="center">
    <tr>
    <br>

    <tr>
        <td bgcolor="#ffffff" colspan=2 width="30%"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Input Payment Details (CASH)<b></td> 
        </td>
    </tr>

    <tr>
        <td bgcolor="#ffffff">Payment Type:</td>
        <td bgcolor="#ffffff"><input type="text" name="paytype" value="Cash" disabled></td>
    </tr>

    <tr>
        <td bgcolor="#ffffff">Date Sold:</td>
        <td bgcolor="#ffffff"><input type="date" name="date" value="<?php echo date('Y-m-d'); ?>"></td>
    </tr>

    <tr>
        <td bgcolor="#ffffff">Cash Amount:</td>
        <td bgcolor="#ffffff"><INPUT type="TEXT" name="cashamount" size="10" maxlength="9" onkeypress="if ( isNaN( String.fromCharCode(event.keyCode) )) return false;"></INPUT></td>
    </tr>

    <td bgcolor="#ffffff" colspan=2 align="center">
        <INPUT type="submit" name="submit" value="Submit">
        </td>
    </tr></form>

如您所见,它的默认值为&#34; CASH&#34;。现在这将提交到带有if语句的php页面:

if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['paytype'] == 'Cash') { 

这是我用于表单的Javascript:

function validateFormCash() {
    var x = document.forms["myForm"]["cashamount"].value;
    if (x == null || x == "") {
        alert("Please input Cash Amount");
        return false;
    }
}

但页面会返回一个错误,说明未定义的paytype,我在这里缺少什么?有人请帮忙。

谢谢

1 个答案:

答案 0 :(得分:-1)

将此内容写入if条件:

if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['submit']) {

为了能够捕获此$_POST,您必须在表单中输入输入字段。例如:

<form action="" method="POST">
    <!-- some HTML here, including <table> tag -->
    <tr>
        <td bgcolor="#ffffff">Payment Type:</td>
        <td bgcolor="#ffffff"><input type="text" name="paytype" value="Cash"></td>
    </tr><tr>
<td bgcolor="#ffffff" colspan=2 align="center">
        <INPUT type="submit" name="submit" value="Submit">
        </td>
    </tr>
    <!-- Some more HTML here, including </table> end tag -->
</form>