HTML-PHP表单,需要建议

时间:2017-02-24 14:50:24

标签: php html forms function

我正在创建一个接受用户输入的程序,然后检查输入是否是有效的ISBN10。

我已经在PHP中创建了一个基本的HTML表单和一个函数来检查ISBN验证。我还没有开始任何输入验证,但每次我输入数据并提交它时,我最终会再次输入一个空白表格,而不是我的消息声明输入是否是有效的ISBN。

如果您能查看我的代码,看看是否有任何明显的我/我没有这样做,我将不胜感激。

提前致谢。

<!DOCTYPE html>

<html>

<head>

<?php

if (isset ($_POST['isbn']))

{

    $isbn = $_POST['isbn'];


function validate_isbn($isbn) 

{

if (strlen != 10) 

{
    return false;

    echo "Please enter a ten-digit ISBN.";
}

for ($i = 0; $i < 10; $i ++) 

{
    $check = $check + (10 - $i) * substr ($i, 1);
}

$check = $check % 11;

if (empty ( $check )) 

{
    return true;

    echo $_POST ["isbn"], " is a VALID ten-digit ISBN.";
} 

else 

{
    return false;

    echo $_POST ["isbn"], " is NOT A VALID ten-digit ISBN.";
}
}

}

?>

<form action ="isbnvalid.php" method = "POST">

    This program will validate an ISBN:

    <br>
    <br>


    Please input a ten digit ISBN: <input type="number" name="isbn" size=12> 

    <br>
    <br>

    <input type="submit" value="Validate">

</form>

3 个答案:

答案 0 :(得分:1)

您不是要调用函数validate_isbn($isbn),而是必须调用它。

$isbn = $_POST['isbn'];

if(validate_isbn($isbn)) {
    echo "ISBN is valid!";
}

还要记住:

  

return将程序控制返回给调用模块。执行   在被调用模块的调用之后的表达式中继续。

这意味着你的函数不输出东西,因为你之前返回的东西。

答案 1 :(得分:1)

我认为你需要这种工作

if (isset ($_REQUEST['submit']))

{

    $isbn = $_POST['isbn'];
    if($isbn == "") {
         echo $msg = '<span class="error"> Please fill all the values</span>';
    }else if(!is_numeric($isbn)) {
        echo $msg = '<span class="error"> Data entered was not numeric</span>';
    }else if(strlen($isbn) <= 10) {
        echo $msg = '<span class="error"> The number entered was not 10 digits long</span>';
    } else {
        /* Success */
        //echo "success";
    }




}

?>
<form action="" method="post">

    This program will validate an ISBN:

    <br>
    <br>


    Please input a ten digit ISBN: <input type="text" name="isbn"> 

    <br>
    <br>

    <input type="submit" name="submit" value="Validate">

</form>

答案 2 :(得分:-1)

你必须在返回之前设置回声 - 如果有回复,之后什么都不会被触及......

相关问题