简体   繁体   中英

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

I have a loginin page login.php. When user clicks submit button it goes to another page loginprocess.php and checks whether user is valid or not. 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. So how do i edit the p element to display the error message ?

您可以使用.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. 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.

Redirect user from loginprocess.php to loginprocess.php by appending a error flag at the end of the URL.

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

In loginprocess.php write below code before rendering

tag :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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