2014-11-30 21:35
浏览 46


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">


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)

图片转代码服务由CSDN问答提供 功能建议

我有一个PHP页面,使用AJAX加载表单的几个部分。 例如,首先检查用户是否已经注册,如果是,则脚本加载(使用AJAX)表单的其余部分。 使用AJAX不会提交表单当用户提交表单时没有问题(没有AJAX) - 想象有一些错误 - 表单将丢失所有值。 我想知道CSS是否隐藏了表单的一部分和 在成功登录后使用JS来显示表单的其余部分,会更好。


 &lt; form  action =“some_action.php”&gt; 
电子邮件:&lt; input type =“text”name =“email”id =“email”&gt;  &lt; br /&gt; 
密码:&lt; input type =“password”name =“password”id =“password”&gt;  &lt; br /&gt; 
&lt; button id =“vrf_login”&gt; Verificar&lt; / button&gt; 
&lt; div id =“rest_form”&gt; 
&lt; / div&gt; 
&lt; / 形式&GT;  

AJAX: -检查登录:如果电子邮件和密码匹配则 - LOAD表格为id为“rest_form” 的div(它位于 另一个文件,例如:

 &lt; input type =“text”name =“place”id =“place”&gt; 
&lt; input type =“text”name  =“age”id =“age”&gt; 
&lt; input type =“submit”name =“submit”value =“submit”&gt; 


编辑 (再次)

谢谢大家的建设性意见: 我采用的解决方案接近第一个Alkis的建议:

  • 几乎所有表单都被隐藏(CSS)

  • 经过一些逻辑选择后,(部分)表单变为可见(jQuery) - “记住”在提交失败的情况下哪些部分应该可见(服务器端验证)一些会话变量保存信息(AJAX) - 然后,在提交(失败)之后使用jQuer y恢复先前的表单结构(以这种方式用JS获取会话变量: var xpto =“&lt;?php echo $ _SESSION ['prior_xpto']; ?&gt;“;

  • 表单的字段将记住他们的值(使用PHP) < / ul>

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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.
    解决 无用
    打赏 举报

相关推荐 更多相似问题