newToWebStuff Posted October 13, 2007 Share Posted October 13, 2007 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 Quote Link to comment Share on other sites More sharing options...
stockton Posted October 13, 2007 Share Posted October 13, 2007 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. Quote Link to comment Share on other sites More sharing options...
newToWebStuff Posted October 13, 2007 Author Share Posted October 13, 2007 I was staying away from javascript because I think it sucks and is hard to use. Additionally if someone had javascript disabled I still need to validate all fields before inserting into database. Thank you. Quote Link to comment Share on other sites More sharing options...
stockton Posted October 14, 2007 Share Posted October 14, 2007 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 ? Quote Link to comment Share on other sites More sharing options...
dbo Posted October 14, 2007 Share Posted October 14, 2007 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. Quote Link to comment Share on other sites More sharing options...
jorgep Posted October 15, 2007 Share Posted October 15, 2007 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.