How can change html element value in php?

I have a basic form, which i need to put some validation into, I have a span area and I want on pressing of the submit button, for a predefined message to show in that box if a field is empty.

Something like

if ($mytextfield = null) {
    //My custom error text to appear in the spcificed #logggingerror field
}

I know i can do this with jquery (document.getElementbyId('#errorlogging').innerHTML = "Text Here"), but how can I do this with PHP?

Bit of a new thing for me with php, any help greatly appreciated :)

Thanks

Rusty Fausak

7,2051 gold badge27 silver badges38 bronze badges

asked Sep 25, 2011 at 19:14

1

You could do it it a couple of ways. You can create a $error variable. Make it so that the $error is always created (even if everything checks out OK) but it needs to be empty if there is no error, or else the value must be the error.

Do it like this:


FORM

answered Sep 25, 2011 at 19:22

How can change html element value in php?

If you want change it dynamically in client-side, there is no way but ajax. PHP works at server-side and you have to use post/get requests.

answered Sep 25, 2011 at 19:22

masoudmasoud

53.8k16 gold badges134 silver badges203 bronze badges

Form fields sent to php in a $_REQUEST, $_GET or $_POST variables...

For validate the field param you may write like this:

if(strlen($_REQUEST['username']) < 6){
 echo 'false';
}
else{
 echo 'true';
}

answered Sep 25, 2011 at 19:21

You can't do anything client-side with PHP. You need Javascript for that. If you really need PHP (for instance to do a check to the database or something), you can use Javascript to do an Ajax call, and put the return value inside a div on the page.

answered Sep 25, 2011 at 19:18

RijkRijk

10.8k3 gold badges29 silver badges45 bronze badges

6

OK, thanks. I understand what you are saying, but still this does seem inordinately difficult. What I suppose I must do is have a form that:

a) has a type =submit button,

b) does not have an action for the form, as I don't want a new page shown,

c) has preventDefault applied as I don't want the current page refreshed. Hopefully this will still cause the data to be submitted by the form (?),

d) calls an AJAX procedure to log in and get the info I need to set what I want on the page with javascript.

The first problem I am getting is that this still causes the page to refresh. Do I need to convert my php file to a function, and if so how? Here's my latest code:

 php if (login_check($mysqli) == false) :  ?>
          id="loginform" data-iziModal-group="grupo1">
              data-iziModal-close class="icon-close">x  
             
href="" id="signin">Sign in
method="post" name="login_form" id="login_form"> type="text" placeholder="Email" name="email" class="login_email" id="login_email"> id="password" type="password" placeholder="Password" name="password" class="login_password">
data-iziModal-close>Cancel class="submit" data-iziModal-close type="submit" onclick="hashandlogin(this.form, this.form.password);">Log in style="text-align: center; color: #444; font-size: 16px; font-family: Lato; padding-top: 70px; font-weight: 500;"> Don't have an account? style="font-size: 16px; font-weight: 500; font-family: Lato;" href="register.php">Register here.

type="text/javascript"> document.getElementById("login_form").addEventListener("submit", function (event) { event.preventDefault() }); function hashandlogin(a, b) { formhash(a, b); var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { console.log("done"); } }; xmlhttp.open("GET", "process_login.php", true); xmlhttp.send(); }
php

include_once 'db_connect.php';
include_once 'functions.php';

sec_session_start(); // Our custom secure way of starting a PHP session.

if (isset($_POST['email'], $_POST['p'])) {
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
    $password = $_POST['p']; // The hashed password.

    if (login($email, $password, $mysqli) == true) {
        $_SESSION["email"] = $email;
        saveLogin($_SESSION['user_id'],$mysqli);
		echo ("something");
        // Login success
      //  header("Location: ../account.php");  I don't want the account page shown just cos user has logged in. But what should go here? If nothing or index.php then get a blank page with process_login.php as the address.
        exit();
    } else {
        // Login failed
  //      header('Location: ../login.php?error=1');
        exit();
    }
} else {
    // The correct POST variables were not sent to this page.
    header('Location: ../error.php?err=Could not process login');
    exit();
}