web-service-learner Posted August 18, 2008 Share Posted August 18, 2008 Hi guys, I got a little problem with contact form which I've using for a while now.But it’s the first time I use checkbox in my form, the problem is when the visitor choose few of the checkboxes the php code sends only the last value with the message. For example the last value of the checkboxes override the previous ones please any solutions? I searched a lot really there wasn't a good code I could try. below is the html doc. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd"><html><head> <link href="files/style.css" rel="stylesheet" type="text/css"> <title>example checkboxes</title></head><body><div id="toppan"></div><div id="logo"></div><div id="topnav"></div><div id="contactform"><br><br><br><form action="handle_contact.php" method="post"> <label for="name">Full Name:</label><br> <input type="text" name="name" value="" size="30"><br> <br> <label for="email">Email:</label><br> <input type="text" name="email" value="" size="30"><br> <br><br><p>How do you get paid for your service?</p>Cash<input type="checkbox" name="paymenttype" value="cash">Cheque<input type="checkbox" name="paymenttype" value="cheque"> Eftpos<input type="checkbox" name="paymenttype" value="eftpos">Direct Debit<input type="checkbox" name="paymenttype" value="directdebit"> Credit Card<input type="checkbox" name="paymenttype" value="creditcard"> Bank Transfers<input type="checkbox" name="paymenttype" value="banktransfer"> Trade In<input type="checkbox" name="paymenttype" value="tradein">Voucher S<input type="checkbox" name="paymenttype" value="voucher"></fieldset><br><br> <label for="comments">Comments:</label><br> <textarea name="comments" rows="10" cols="40" size="40" /></textarea><br> <br> <input type="submit" value="Send"></form></div> </body></html> Link to comment Share on other sites More sharing options...
web-service-learner Posted August 18, 2008 Author Share Posted August 18, 2008 Link to comment Share on other sites More sharing options...
Ingolme Posted August 18, 2008 Share Posted August 18, 2008 Are the users supposed to be able to click on more than one? If not, use radio buttons.If they are supposed to, then you should name them like this:name="paymenttype[]"It will return an array in PHP, like this: echo $_POST['paymenttype'][0]; // Returns "cash" if selected, otherwise it returns an empty stringecho $_POST['paymenttype'][1]; // Returns "cheque" if selected, otherwise it returns an empty string... Link to comment Share on other sites More sharing options...
web-service-learner Posted August 19, 2008 Author Share Posted August 19, 2008 hi , thanks for your help.do i have to use the arrays in the html form? like this>>>Cash<input type="checkbox" name="paymenttype[0]" value="cash">Cheque<input type="checkbox" name="paymenttype[1]" value="cheque">and is this the right php code to post the arrays >> echo $_POST['paymenttype'][0]; ?thanks mate, Link to comment Share on other sites More sharing options...
Synook Posted August 19, 2008 Share Posted August 19, 2008 No - just use the push operator ([]), and checked boxes will be pushed onto the array. Link to comment Share on other sites More sharing options...
web-service-learner Posted August 20, 2008 Author Share Posted August 20, 2008 hi thanks again,thats a new thing now , what you mean by saying use the push operator? Link to comment Share on other sites More sharing options...
Synook Posted August 20, 2008 Share Posted August 20, 2008 If you were to write $item = $array[]; as a function, it would look something like push($array, $item); where $item is "pushed" (i.e. augmented) onto the end of the $array. So when you write paymenttype[] as the name of the checkbox, think of it as augmenting each value onto the end of the paymenttype array. Link to comment Share on other sites More sharing options...
web-service-learner Posted August 20, 2008 Author Share Posted August 20, 2008 hey synook,would you send me a form with for au $20?a working form with checkboxes more than 5 checkboxes though?would ya? are we allowed to send our email address in this forum? Link to comment Share on other sites More sharing options...
Synook Posted August 20, 2008 Share Posted August 20, 2008 ... did you try using the brackets [] on your checkboxes' names?Or why don't you just use a <select> if you have trouble with the checkboxes? Link to comment Share on other sites More sharing options...
web-service-learner Posted August 21, 2008 Author Share Posted August 21, 2008 yes i used the [ ] next to the name of the checkbox, now i get the email like this > Payment Type: Array.before i started using the > [ ] i used to get the Payment type:last option.if i use the select thing its like radio buttons 1 option can be sent, thats why i like to learn the checkbox thing.thanks Link to comment Share on other sites More sharing options...
Ingolme Posted August 22, 2008 Share Posted August 22, 2008 You can't send an array directly. In PHP you'll have to access each Payment Type array element individually, like this: foreach ($_POST['paymenttype'] as $PT) { // For each element $in the $_POST['paymenttype'] array, do the following echo $PT . "\n";} And like I said earlier:Are clients really supposed to be able to select more than one option?If that's not the case, use radio buttons or a select menu instead. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.