使用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.

dongyuan7981
dongyuan7981 谢谢@GauravParashar这是一个非常好的观点!因此,从长远来看,它更容易维护。
大约 4 年之前 回复
doubei8168
doubei8168 我不确定你的问题是否真的适合SO,它可能更适合code.review虽然我建议在发布你的问题之前阅读他们的旅游/要问的问题。
大约 4 年之前 回复
doufan6886
doufan6886 与第一个一起使用,因为如果你想改变后端部分你可以轻松改变。首先,你可以单独使用前端和后端
大约 4 年之前 回复
dqk94069
dqk94069 我可以在发布之前添加表单本身,检查用户是否已登录。
大约 4 年之前 回复
dougou5844
dougou5844 你应该用PHP和ajax来做
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐