good_boy Posted May 30, 2011 Share Posted May 30, 2011 Hi guys! I'm new to PHP and I need help from you to create a simple form with checkboxes. Experts here will be able to solve it easily. I'm posting a part of the code below. form.html Interests<br /> Sports<input type="checkbox" name="interests[]" value="Sports" /> <br /> Reading<input type="checkbox" name="interests[]" value="Reading" /> <br /> Surfing<input type="checkbox" name="interests[]" value="Surfing" /> <br /> output.php <?php $inter = $_POST['interests']; ?> I read somewhere that it is now submitted as an array to to the output.php file but I don't know how to play with it.I want php to out put the values of $inter in the following manner. "You are interested in {$inter}." Please help me. Is this a very complicated code to write? Quote Link to comment Share on other sites More sharing options...
cssfreakie Posted May 30, 2011 Share Posted May 30, 2011 try out: <form action="" method="post"> Interests<br /> Sports<input type="checkbox" name="interests[]" value="Sports" /> <br /> Reading<input type="checkbox" name="interests[]" value="Reading" /> <br /> Surfing<input type="checkbox" name="interests[]" value="Surfing" /> <br /> <input type="submit" value="submit" name="submit" /> </form> <?php if(isset($_POST['submit'])&& isset($_POST['interests'])){ // if someone presses submit and 'interest is also set var_dump($_POST['interests']); // var_dump just show how the array is build up, just use for testing foreach($_POST['interests'] as $value) {// for each loop since it's an array echo 'you are interested in '.$value.'<br />'; //echo out each value that is inside of the array }//end foreach }// end if clause Quote Link to comment Share on other sites More sharing options...
good_boy Posted May 30, 2011 Author Share Posted May 30, 2011 It worked!!!Thanks a million.I have a few more doubts which I'm posting below. 1.I wanted PHP to process & output the names from my form in the 'first letter of the word in uppercase & the rest in lowercase' method. I did this because if the user inputs the same name in some other way(like all caps),php will consider it as another name.And secondarily the output will look weird like 'JOhn' if the user makes a mistake. I used the following method to overcome this issue $name = strtolower($name); $name = ucwords($name); Is this the correct method or is there any direct method to make PHP identify it as a name? 2.Is there any method to restrict the input type of an html form to numbers using html/php? 3.At the moment,if a user submits my form without completing all the fields in it,he is greeted with an error by php.I wish to prevent my form.html page from submitting the form until al the fields are filled. Thank you once again. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted May 30, 2011 Share Posted May 30, 2011 1. yes that is correct, though you may only need to do $name=ucwords($name). 2. Yes, there are quite a few ways. You can use regex if you wanted to, but I would suggest the built in function is_numeric(). You can use this like so if (!is_numeric($_POST['some_input'])){ die ("invalid input into 'some_input' form field"); } of course you want a more graceful way of handling these kind of errors, but you get the idea. 3. If you wish to check that all forms are filled in BEFORE a form submit, you need javascript. Specifically, you need to research into how Javascript accesses the HTML DOM. Quote Link to comment Share on other sites More sharing options...
good_boy Posted May 30, 2011 Author Share Posted May 30, 2011 Thanks a lot mikesta707.The 'die' and 'is_numeric' functions are a new addition to my brain's database .What is regex? I don't think that the ucwords function alone would do the job.What if the user types in his/her name in all caps? I realize that my knowledge about functions is very limited.How can I improve my functions vocabulary ? Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted May 30, 2011 Share Posted May 30, 2011 Read the manual and lurk these forums. Tutorials also help a lot, and of course practice practice practice. Regex is shorthand for regular expressions. It is sort of an advanced topic, but do a google search for regular expressions if you wish to find out more Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted May 30, 2011 Share Posted May 30, 2011 is_numeric accepts some values as valid that you may not want. For example, both exponential: 4.25E381 and hexadecimal: 0xAF90DB will be considered valid. If all you want is numeric characters, you can use ctype_digit. To address the all caps issue, just convert the string to all lower before using ucwords on it. $string = 'THIS IS A STRING OF UPPERCASE WORDS.'; echo ucwords( strtolower($string) ); // RETURNS: This Is A String Of Uppercase Words. If what you really want is to allow the string to be formatted in either upper or lower case, and match a comparison irrespective of the case, you can use strcasecmp. $string = 'THIS IS A STRING OF UPPERCASE WORDS.'; $string2 = 'THIS IS A STRING OF UppERcAsE woRdS.'; if( strcasecmp($string, $string2) === 0 ) { echo 'Strings match'; } else { echo 'Strings are different'; } // RETURNS: Strings Match Quote Link to comment Share on other sites More sharing options...
good_boy Posted May 30, 2011 Author Share Posted May 30, 2011 Thanks Pikachu2000. I'm experiencing a problem with the code posted by cssfreakie.If $value has two solutions it returns both the values along with the "you are interested in" statement.I just want it to echo something like this : "You are interested in first_value, second_value & third_value." Quote Link to comment Share on other sites More sharing options...
good_boy Posted May 30, 2011 Author Share Posted May 30, 2011 Guys,I found some help here: http://www.smartwebby.com/PHP/Phptips2.asp.What do you think?Will it be helpful. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted May 30, 2011 Share Posted May 30, 2011 Replace this: foreach($_POST['interests'] as $value) {// for each loop since it's an array echo 'you are interested in '.$value.'<br />'; //echo out each value that is inside of the array }//end foreach with this $interests = implode( ', ', $_POST['interests']; echo "You are interested in $interests."; Quote Link to comment Share on other sites More sharing options...
good_boy Posted May 31, 2011 Author Share Posted May 31, 2011 Thanks.Finally, things are working. 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.