0

我想要做的是确保用户在点击提交之前输入一个数量。问题是,用户可以继续登录屏幕,而无需在数量框中输入任何数字。我尝试粘贴验证码,但它仍然将我的用户带到 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>
4

2 回答 2

1

你移动你的用户

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

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

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

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

于 2013-11-05T00:24:37.817 回答
0

我在您的第一页中看不到任何验证代码,这实际上是您应该验证的地方 - 在用户甚至可以继续到下一页之前。 是一个很好/简单的例子。

如果您只想根据输入元素的值阻止人们单击按钮,则可以执行以下操作:

<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以防止任何类型的注入和类似的东西。

于 2013-11-05T00:28:41.143 回答