Jump to content

confirm boxes, allow many boxes to be displayed in one


Sharkadder

Recommended Posts

Ok i have a basic HTML/PHP form which will get validated with some javascript.Currently if a file is validated a separate php file will check that everything is all ok, then user will press back button and they will refill the form in again.What i need is for is the user presses the submit button it displays a message box saying which fields are not filled in. Quite simple as you will agree as i know how to display message boxes in javascript.My question is, if many elements on form are invalid, how can i get the values to be displayed within 1 message box instead of all separate? so the message box will have on separate lines which elements are invalid.Currently i only use a message box for agreeing to terms and conditions which i can modify for me needs. I recon i could just use a + and add another element onto the next line but what if all elements are correct apart from the last one in the form? the message box will be bigger than it needs to be if you get my drift.So for example if the first element is incorrect and the rest are:surname is invalid OK(the line spaces will be because other elements are all in working order), what will i need to add to my display message code so that if an element is correct don't continue onto another line? if you'd like me to explain more i can, if you get what i mean then great.some basic button code is shown here:<head><script type="text/javascript">function disp_alert(){alert("I am an alert box!!");}</script></head><body><input type="button" onclick="disp_alert()" value="Display alert box" /></body>I somehow need the code modifying so it displays more alerts within the 1 message box and also so that if an element isn't incorrect it will not reserve a line for it within the message box.

Link to comment
Share on other sites

You can add a new line using \n in the string eg:

<head><script type="text/javascript">function disp_alert(){alert("I am an alert box!!\nThis is a second line");}</script></head><body><input type="button" onclick="disp_alert()" value="Display alert box" /></body>

You might want to have a variable that holds the text for the message box, and append each message to it followed by \n

Link to comment
Share on other sites

Ok i have managed to sort everything on my form, now all i need is 1 thing.If the user types in some incorrect data and presses submit, a message box appears telling them which elements are type in incorrectly. The fields which have incorrect values then have the borders changed to red, otherwise they change the green to show that they are ok.Now the problem i have is for another form. This form is a contact us form, if the user types in an illegal value what happens is the validation page once submit is pressed tells the user to go back to the previous page and correct the data. the problem i have is, when the user presses the back button or the back hyperlink on the validation page, the form get's reset.How can i stop the form from being reset once back is pressed? I have tried the following basic code for going back to previous page in java script:<a href="java script:history.go(-1);">back</a>and<a href="java script:history.back(-1);">back</a>Both seem to totally reset the form on the previous page. any way i can keep the initial values within the form on previous page once i press back?I could just put the validation on that i have for my sign up form but i've wanted to know this for a while now, i can use it for future reference. I searched google and found nothing about it, it was all about reset buttons etc.thanks

Link to comment
Share on other sites

How can i stop the form from being reset once back is pressed?
Use PHP to refill the values on the form. From what I know, JavaScript can't do it since you are changing pages and going back, effectively reseting (or losing) the variables in your JavaScript code. Otherwise, you could just keep the page from changing in the first place.http://www.w3schools.com/php/php_forms.asp
Link to comment
Share on other sites

that it, i was wondering why.See the form is now validated in javascript but i have a php validation backup script behind that one. If you press back with my php code currently the form resets. Before the form used to keep initial values and now i can see why. So what i will have to do is either as you say, stop the page from changing and keep the php script there as backup. Or i just soley use php and the values shouldn't be reset.Hmmm, i think in my php code when the page loads i must be resetting all values to 0 characters because the form effectively resets itself still. I will look into it more and try it out, will probs figure t out before long but at least i know theres no javascript solution.thanks

Link to comment
Share on other sites

Why not have the (PHP) validation before the form on the same page, so that you can do the validation first and if it fails continue to display the form, using the POST values to fill up the fields?

Link to comment
Share on other sites

thats the one my man. I knew i did something else before. What i had was 2 web pages into 1 if you like.Basically if the validation failed underneath that code i started a new html page which had the invalid values please go back. Because it was part of the same code, when the user pressed the back button or the back hyperlink, all values would automatically be entered. I'm sure that is what i did but i could be wrong until i test it thourely.so what your saying is if i do like i have with my javascript, have all the validation in the head before the form is submitted. Then once i press submit i have the php validation working and i cancel the javascript validation out all together?I know it seems a bit stupid having 2 validations, but people have recommended i have 1 validation on the form and then a server side validation such as php as well. I guess i could have both in the head of the page and then try and keep valid fields as valid as possible. The problem with solely using javascript is it has it's limits with things like this. PHP seems to give you more choice.But hey, is that what you mean?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...