为什么我从文件index.php到login.php的验证不起作用?

时间:2013-11-04 23:47:17

标签: php forms validation

我要做的是确保用户在点击提交前输入数量。问题是,用户可以继续登录屏幕,而数量框中没有任何数字输入。我已经尝试粘贴验证码,但它仍然需要我的用户访问login.php屏幕。任何帮助将不胜感激!

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>

        <form action='login.php' method='post'> 

        <?php


//The following arrays contain my products and their information, one product per array.

$hulkhamburger = array('Food' => 'Hulk Hamburger', 'Description' => '...', 'Price' => '$1.00', 'Quantity' => '<input type="text" name="quantity">');
$atomichotdog = array('Food' => 'Atomic Hot Dog', 'Description' => '...', 'Price' => '$2.00', 'Quantity' => '<input type="text" name="quantity">');
$friedchicken = array('Food' => 'Fantastic 4 Fried Chicken', 'Description' => '...', 'Price' => '$3.00', 'Quantity' => '<input type="text" name="quantity">');
$psyonicpizza = array('Food' => 'Psyonic Pizza', 'Description' => '...', 'Price' => '$4.00', 'Quantity' => '<input type="text" name="quantity">');
$marvelmeatloaf = array('Food' => 'Marvel Meatloaf', 'Description' => '...', 'Price' => '$5.00', 'Quantity' => '<input type="text" name="quantity">');

//The following array takes my previous five arrays and puts them into one array for easier coding and reading.

$allfood = array ($hulkhamburger, $atomichotdog, $friedchicken, $psyonicpizza, $marvelmeatloaf);

/*The following code centers my table on the page, makes the table background white,
 makes the table 50% of the browser window, gives it a border of 1 px,
 gives a padding of 2 px between the cell border and content, and gives 1 px of spacing between cells.
 */

echo "<table align=center bgcolor='FFFFFF' width=50% border=1 cellpadding=1
cellspacing=2>";

/*The following code prints my table header.
 * Credit goes to Dr. Kazman; code is from Lecture 10.
 * I used his code because it was easier (and a more efficient way) than doing it all manually like I did for Mini Asst. 2.
 */

echo "<tr>";
$header = array_keys($hulkhamburger);
foreach ($header as $key => $myheader)
{
    echo "<th>$myheader</th>";  
}
echo "</tr>";

//The following code loops through the whole table body and then prints each row.
for($i=0; $i<count($allfood); $i++)
    {
    echo "<tr>";
    foreach ($allfood[$i] as $key => $value)
        {
        echo ("<td align=center>$value</td>");
        }
    }        
//This code ends my table.
echo "</align>";
echo "<br>";   
        ?>
 <tr>
 <td>
 <td>
 <td>
 <td>
 <center><input type='submit' value='submit'></center>   
        </form>
    </body>
</html>

这是我的login.php

<?php//made the user login menu into a nice table that will center the username and password in the middle of the page.?>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">

    <tr>
<form name="login" method="post" action="invoice.php">
    <td>

<table width="100%" border="0" cellpadding="3" cellspacing="1">
    <tr>


    <td colspan="3"><strong>User Login </strong></td>
    </tr>
    <tr>
    <td width="78">Username</td>
    <td width="6">:</td>    
    <td width="294"><input name="myusername" type="text" id="myusername"></td>
    </tr>
    <tr>
    <td>Password</td>
    <td>:</td>
    <td><input name="mypassword" type="text" id="mypassword"></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type='submit' name='login' value='Login'>

<?php
 /*This code will allow a new user to go to the registration page and register for the site
 * before buying anything.
 */
?>
<a href="registration.php"><br>New user registration</a>
    </td>
    </tr>
    </table>
    </td>
</form>
    </tr>
</table>

2 个答案:

答案 0 :(得分:1)

您移动用户

header("Location: http://www.google.de");

您在表单中指定的页面必须进行验证。您可以直接将用户发送到login.php,如果验证失败或者您的index.php执行验证并将验证成功后发送给login.php,或者您创建了第三个发送的脚本,则会将其发送回用户到两个页面之一。

如果您想在将值传递到服务器之前检查这些值,请使用Javascript。

如果您不想自己编写JS代码,请使用框架(例如YII)。

答案 1 :(得分:0)

我没有在您的第一页中看到任何验证代码,这是您应该验证的地方 - 在用户甚至可以进入下一页之前。 Here是一个很好/简单的例子。

如果你想要的是阻止人们根据输入元素的值点击按钮,你可以这样做:

<script>
function validateQuantity() {
    var x = document.getElementById('quantity');
    var y = document.getElementById('submitBtn');
    if(isNumber(x.value)){
        y.disabled = false;
    }else{
        y.disabled = true;
    }
}
function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

这将切换提交按钮的已禁用属性。我承认,这不是最好的答案。您应该在表单页面上进行表单验证,并通过在表单元素中使用onsubmit="return(validate());"这样的内容来触发JS验证代码。

然后,您可以在收到$_POST数据的网页上再次验证结果,以防止任何类型的注入和类似内容。