drnpwmq4536 2010-01-31 05:56
浏览 57
已采纳

将简单的Javascript脚本转换为PHP

I have a simple javascript function to validate a form (RSform!Pro in Joomla) on submit. Problem is bots are able to submit the form without the validate script running when they have javascript disabled. The only option I can think of to prevent this case from happening is to use php instead of javascript. Could someone help me with this conversion or offer an alternative?

    function validateEntreeCount(theForm)
    {
     if (parseInt(document.getElementById('Attendees').value) != 
parseInt(document.getElementById('Beef').value) + 
parseInt(document.getElementById('Chicken').value) + 
parseInt(document.getElementById('Vegetarian').value))
     {
      alert('The total amount of attendees does not match the total amount of entrees selected'); 
      return false;
     } 
     else
     {
      return true; 
     }
    }

Thanks for everybody's input!

  • 写回答

5条回答 默认 最新

  • duanlu0075 2010-01-31 06:28
    关注

    In order for the conversion to work, I will have to assume a few things, so let me show you a conversion with my general assumptions:

    My assumed HTML page, containing a form with a few input types, total number of attendees, a number indicating how many orders of what type of dinner and a submit button. Notice the method of the form is a post and the action is yourfile.php.

    <form name="my_form" method="post" action="yourfile.php">
       <input type="text" value="Attendees"></input>
       <input type="text" value="Beef"></input>
       <input type="text" value="Chicken"></input>
       <input type="text" value="Vegetarian"></input>
       <input type="submit" value="submit"></input>
    </form>
    

    Your PHP page that will retrieve the form values:

    <?php
      function validateEntreeCount() {
       // used for validation purposes
       if (!isset($_POST['submit']) {
           // form was submitted without data, silly bots.
         echo "We've encountered a problem, please re-enter your data";
          // possible redirect back to the page.
       }       
       // convert the data into integers.
       $total   = intval($_POST["Attendees"]);
       $beef    = intval($_POST["Beef"]);
       $chicken = intval($_POST["Chicken"]);
       $veg     = intval($_POST["Vegetarian"]);
    
        // if all is well, everything should be equal
         if ($total == ($beef + $chicken + $veg)) {
           return true;
        }  
       // something went wrong
           echo "The total amount of attendees does not match the total amount of entrees selected";
           return false;
      }
    ?>
    

    This hasn't been tested or compiled, just demonstrating how the comparison function would look like in PHP. There are many things to work on, especially validating input and error checking.

    Hope this helps you out, or points you in the right direction.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c