简体   繁体   English

如何使用 php 或 jquery 将文本添加到特定的 html 元素?

[英]How to add text to a particular html element using php or jquery?

I have a loginin page login.php.我有一个登录页面 login.php。 When user clicks submit button it goes to another page loginprocess.php and checks whether user is valid or not.当用户单击提交按钮时,它会转到另一个页面 loginprocess.php 并检查用户是否有效。 if user is valid then it again displays the same form and layout as the login.php page but with a message "Incorrect username and password" in ap element with class="errmsg" which is within the form below the form heading.如果用户有效,则它再次显示与 login.php 页面相同的表单和布局,但在 ap 元素中显示一条消息“用户名和密码不正确”,其中 class="errmsg" 位于表单标题下方的表单中。 So how do i edit the p element to display the error message ?那么如何编辑 p 元素以显示错误消息?

您可以使用.text().html()方法,

$(".errmsg").text("invalid username");

I guess you can use jquery to append text to a div but this only works when you do the checking in the same page.我想您可以使用 jquery 将文本附加到 div 中,但这仅在您在同一页面中进行检查时才有效。 You could also try to send a true/false value back to login.php and then use jquery to append text to the div or just echo the error with php.您还可以尝试将真/假值发送回 login.php,然后使用 jquery 将文本附加到 div 或仅使用 php 回显错误。

Redirect user from loginprocess.php to loginprocess.php by appending a error flag at the end of the URL.通过在 URL 末尾附加错误标志,将用户从 loginprocess.php 重定向到 loginprocess.php。

Example:
if (...) {
 header('Location : loginprocess.php?err=1');
 exit();
}

In loginprocess.php write below code before rendering在 loginprocess.php 中,在渲染前写下面的代码

tag :标签 :

<?php
$class = '';
if(isset($_GET['err']) {
  if($_GET['err'] == 1) {
    $class = 'errmsg';
  }
}
?>
<p class="<?php echo $class;?>"></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM