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

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:

 
         
             x  
             
             Sign in
             
             
Cancel Log in Don't have an account? Register here.

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[]; }

Chủ Đề