dousui4577 2014-11-30 21:35
浏览 46
已采纳

使用AJAX加载表单将在提交失败后松散表单和值

I have a PHP page that loads several parts of a form using AJAX. For instance, first check if the user is already registered, if so the script loads (with AJAX) the rest of the form. The form will not be submited using AJAX what can be a problem when the user submits the form (without AJAX) - imagine there are some errors - the form will loose all values. I'm wondering if CSS hiding part of the form and after the successful login use JS to display the rest of the form, would be better.

Here some code:

<form action="some_action.php">
    Email: <input type="text" name="email" id="email"> <br />
    Password: <input type="password" name="password" id="password"> <br />
    <button id="vrf_login">Verificar</button>

    <div id="rest_form">

    </div>
</form>  

AJAX: - CHECK login: if email and password matches then - LOAD the form for div with id "rest_form" (it is in another file, for instance:

<input type="text" name="place" id="place">
<input type="text" name="age" id="age">
<input type="submit" name="submit" value="submit">

)

The problem is if I submit the form (without AJAX) and there are errors I will loose the form loaded with AJAX

EDIT (again)

Thank you all for your constructive suggestions: The solution I adopted is close to the first Alkis's suggestion:

  • almost all the form is hidden (CSS)

  • after some logic choices the (part of the) form is turned visible (jQuery) - to "remember" what parts should be visible in case of submission failed (server side validation) some session variables hold the information (AJAX) - and then, after the submission (failed) use jQuery to restore the prior form structure (get the session variables with JS this way: var xpto = "<?php echo $_SESSION['prior_xpto']; ?>" ; )

  • the fields of the form will remember theirs values (with PHP)

  • 写回答

2条回答 默认 最新

  • douci6541 2014-11-30 22:19
    关注

    You have 3 options.

    1. Stop loading the whole form by ajax. Hide it with css and show it if the the conditions are met. If the page is shown after some validation error, just show it (change the css inline or give it a different class)
    2. Have a condition and every time the page loads check if it is a first load or if the page is shown after some validation error occured. If the latter is true then load again the form with ajax. This condition can be a hidden field that takes its value from the server and you check it on the client every time you serve the page.
    3. The second solution can be done on the server too. Have the condition be checked on the server. If it's a first load, then don't populate the form and let it be populated from ajax as you do now. If it's after a validation error then pre-populate the form. It's just an if/else clause.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料