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条)

报告相同问题?

悬赏问题

  • ¥20 数学建模数学建模需要
  • ¥15 c语言数据结构实验单链表的删除
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决