dox90448 2014-05-05 13:13
浏览 23
已采纳

如果出现错误声明,则更容易在php中的文本字段中保留数据

I have a simple xhtml form with textboxes asking the user for their name, id, address etc. I have to use php to validate the data. For example in the ID field exactly 6 numerical values are allowed, if thats not the case, it shows an error to the user asking them to fix it. What i want do is, when the user clicks submit with the incorrect data, i want to keep the data in the field when the error shows up. I am currently doing that by:-

 <td><label for="customerid">*Customer ID: </label></td>
    <td><input type="text" value="
<?php if(($_POST['customerid'] != $id) || ($_POST["customerfname"] != $fname) 
||    ($_POST["customerlname"] != $lname)){echo $_POST['customerid'];} ?>"

id="customerid" name="customerid"/>

I have just been modyifying that statement for all textboxes but as you can imagine it will get a little complicated if i have to do that for 10 boxes. i was wondering if it was an easier way to do this.

bold UPDATED

<td> <select name="state" id="state"  value="<?php echo (isset($_POST["state"]) ?  : ''); ?>"> 
    <option value="--">--</option>
    <option value="ACT">ACT</option>
    <option value="NSW">NSW</option>
    <option value="NT">NT</option>
    <option value="QLD">QLD</option>
    <option value="SA">SA</option>
 </select>
</td>
  • 写回答

3条回答 默认 最新

  • douhuangjie4503 2014-05-05 13:19
    关注

    Something like this?

    <input type="text" name="customerid" value="<?php echo (isset($_POST['customerid']) ? $_POST['customerid'] : ''); ?>">
    

    Update - run your checks before the form is re-generated

    <?php
    
    if(($_POST['customerid'] != $id) || ($_POST["customerfname"] != $fname) || ($_POST["customerlname"] != $lname))
    {
        // do nothing
    }
    else
    {
        $_POST['customerid'] = '';
    }
    
    ?>
    
    <input type="text" name="customerid" value="<?php echo (isset($_POST['customerid']) ? $_POST['customerid'] : ''); ?>">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计