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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记