dongnei3634 2016-02-07 13:53
浏览 70
已采纳

如何在php中比较表单数据

HTML/PHP:

<form method="post" name="contact" id="frmContact" action="sM.php">
    <img id="main-img" src="theimage/img1.png" name="imageval" />
    <div style="clear: both; padding: 10px 0 0 0; overflow: hidden;">
        Please enter the number(s) from the image above: <input type="text" id="tNum" placeholder="Enter Number(s)" name="numval" />
    </div>
    <input type="submit" value="Send" id="submit" name="submit" class="submit_btn" />
</form>

PHP:

$arrImg = array("img1", "img2", "img3", "img4", "img5", "img6");
$arrImgText = array("56", "342", "34534", "12", "444", "652");

$imgval = trim(strip_tags(stripslashes($_POST['imageval']))); //get the image source that was displayed in the form
$numval = trim(strip_tags(stripslashes($_POST['numval']))); //get the number that the user entered

//if ({arrImg[imgval] == arrImgText[numval]}) {
    //do something;
//}

The image that is displayed in the form has some numbers. When the user hit send, I would like to compare the number that was entered that was displayed in the image and compare.

How can I do that.

  • 写回答

2条回答 默认 最新

  • dongyongju9560 2016-02-07 13:58
    关注

    In your form, create hidden input:

    <input type="hidden" name="imageval" value="img1" />
    

    In your PHP file you can have now two $_POST variables:

    $secretImg = $_POST['imageval'];
    $token = $_POST['numval'];
    

    Now you need to find key of image:

    $imgKey = array_search($secretImg, $arrImg);
    

    Using the key value, check the proper token:

    if ($arrImgText[$imgKey] === $token) {
        // Token is valid
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?