Jump to content

checkbox multi selection


web-service-learner
 Share

Recommended Posts

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

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

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

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

:) ... 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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...