Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Robert Walker
Robert Walker
17,146 Points

Integrating Validation Errors Help

Wondered if anyone knew how to apply the same checks to a radio button and return the users input for the radio button selection if there are errors.

   if (empty($name)){
      $Err = "Error you must enter your name!";

   }

   if (empty($sex)){
      $Err = "Error you must select a gender!";

   }
<input type="text" placeholder="Robert" name="FirstName" value="<?php if(isset($_POST['name'])){ echo $name ;}?>"></input>          

So this works great, if I get an error my page is loaded again and the text the user entered into the input field is returned for them to fix but I want to be able to store the radio button too so they don't have to check it again when posting the form again.

Ive tried the checked="checked" but on submitting the form again it brings an error of empty.

1 Answer

What's the exact error?

Robert Walker
Robert Walker
17,146 Points

It just brings up the error:

Error you must select a gender!

So lets say you type your name "Robert" and select Male but you miss out your last name, the form validation will load your name back into the input field and say you must enter your last name. You then enter your last name and hit submit again but then you get back the empty error for the gender selection.

So of course this is because your selection is not being stored, so I want to store that so you don't have to select gender again if you get an error the first time round. (excluding it if infact the error was for not selecting it the first time round)