Jump to content

newbie here:how to render form field errors in html


newToWebStuff

Recommended Posts

Attempting a small site, not using javascript, using php to validate my form.  I'm posting back to the same .php page with  code checking to see if the form has been submitted. If submitted and fields are invalid then down in the html I'm inserting some php code and dynamically building the html to show the error message(s).  Please tell me there is a better way to do this as this is turning out to be a bear, and I only have a handful of pages?  If this isn't the correct forum, I apologize, could you point me in the right direction. Thanks

Link to comment
Share on other sites

I have successfully used Ajax as the interface between the client web page and the server and thereby am able to display my error messages in javascript alerts.

BTW You said "not using javascript" why not?

When I was a newbie(not too long ago) I was also frightened off by Javascript & Ajax but in fact I have subsiquently found both easy to use and using Ajax made my coding life much simplier.

Link to comment
Share on other sites

You make an interesting statement "I was staying away from javascript because I think it sucks and is hard to use".

On what do you base this statement?

Why do you feel that it "sucks" and how do you know that it is hard to use if you have not tried it ?

Link to comment
Share on other sites

I think his later statement is the best one. Always do validation on the server side even if you use javascript. So it's a good call to do the PHP validation.

 

I wrote a class that validates forms. If there are any error messages they are stored in an associative array with the key being the field and the value being the error message. So then when I render the form... if there is an error I can echo it back out.

Link to comment
Share on other sites

Well you are rigth about the users that have their javascript disabled, in any cases with javascript you can give the user a most friendly interface... In any of the cases you should always use server side validation (in this case php validation)...

 

You can do something like this...

<?php
#Processig form
foreach($_POST as $i => $v) $$i = $v;
if($field1==""){
$error['field1'] = "Error on field 1";
}
if($field2 == ""){
$error['field2'] = "Error on field 2";
}
if(!isset($error)){
#Send the form data to where ever you want
}
?>
<form>
<fieldset>
	<p>
		<span class="feedback" id="field1_fb"><?php echo isset($error['field1'])?$error['field1']:""?></span>
		<label for="field1">Field 1</label>
		<input type="text" name="field1" id="field1" />
	</p>
	<p>
		<span class="feedback" id="field2_fb"><?php echo isset($error['field2'])?$error['field2']:""?></span>
		<label for="field2">Field 2</label>
		<input type="text" name="field2" id="field2" />
	</p>
	<input type="submit" value="Submit" />
</fieldset>
</form>

 

Hope this helps!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.