donglun2024 2016-08-25 11:51
浏览 30

使用php或Jquery / Javascript隐藏/显示表单

I have a problem here which I have solved with two different solutions. I would like to know:

  • Which solution is the most secure?
  • Which solution is more efficient, basically which one requires the least bandwith for user?
  • Which solution in the long run is easiest to maintain?
  • Is there a possibility that any of the solutions might get outdated in the near future? Example if they change how jQuery .hide() works or anything else, whatever.

Here are my solutions:

Solution 1 with jQuery and some php.

jQuery Code:

$(document).ready(function(){

  $.get( "hide_forms_until_logged_in.php", function(  data ) {
    console.log(data.response);
    if(data.response == "false") {
      $("form").hide();
    } else {
      $("form").show();
    }
  }, "json");
});

HTML markup:

<form action="" method="POST" enctype="multipart/form-data">
  <input type="file" name="file" /> <br/>
  <input type="submit" name="submit" value="Upload"/>
</form>

hide_forms_until_logged_in.php:

include 'session.php';
if (isset($_SESSION['login_user'])) {
  echo json_encode(array("response"=>"true"));
} else {
  echo json_encode(array("response"=>"false"));
}

Solution 2 with mostly php:

hide_forms_until_logged_in.php:

include 'session.php';
if (isset($_SESSION['login_user'])) {
  echo "<form action='' method='POST'
  enctype='multipart/form-data'>
   <input type='file' name='file' /> <br/>
   <input type='submit' name='submit' value='Ladda upp'/>
  </form>";
} else {
  echo "Logga in for att ladda upp filer";
}

And inside the page:

  <?php include "hide_forms_until_logged_in.php" ?>

I know I could have made a function and call it inside the include there, for the sake just put the include there.

The form itself before posting anything to database has a check if the user is logged in or not.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥15 stable diffusion
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿