Oooh I know how to do this! I try to help people on this forum but most questions are too complicated for me. I'm only intermediate at PHP.
Anyhooooooooo. Here goes. Okay, say you have a checkbox question that asks "What colors do you like?" And then you have a checkbox for blue, one for red, one for yellow, and one for green, right? Well, you're going to give all those checkboxes the same name and put a [] after it. Watch:
PHP Code:
<strong>What colors do you like?</strong>
<br /><br />
<input type='checkbox' name='color[]' value='red' /> Red
<br /><input type='checkbox' name='color[]' value='blue' /> Blue
<br /><input type='checkbox' name='color[]' value='yellow' /> Yellow
<br /><input type='checkbox' name='color[]' value='green' /> Green
Now, lets turn our attention to how the form will be submitted. Do you recognize the following tag?
PHP Code:
<form action='somepage.php' method='get'>
That goes at the beginning of a form. See method='get'? That means the information entered in the form will be submitted through the URL. If you don't want to do this, use "post" in place of get, like this: method='post'
Now turn your eyes to action='somepage.php'. Replace somepage.php with the page you want to process the form. Let's just call it formprocessor.php for now.
Okay, when you submit the form, the information will go to formprocessor.php If you used "get" as your method the information will be put inside a variable called "$_GET", if you used "post", the info will be in "$_POST".
To access something in $_GET or $_POST you use what you named the field. So, like, for our "color" field, if we submitted it for $_GET it's in $_GET['color'] and likewise if it was with post then $_POST['color'].
Because we used the [] things, if somebody checks multiple boxes and submits the form, PHP is going to put those multiple answers into an array. Like if they picked blue, green, and red, $_GET['color'] is going to have an array like this in it:
Array
(
[0] => red
[1] => blue
[2] => green
)
If you want to see this for yourself, go to formprocessor.php and do this:
(If you used post then replace GET with POST.) Go to your page with the form, check multiple answers, and submit it.
Erase the code I just showed you because you're not going to actually use that code to process your script. This is just a nice way of looking at what is in that array.
Anyhoo, now, having multiple answers combined into an array is well and good, but how do we store it in the database? Well, it kind of depends on what you want. Do you want to store them in one field or multiple fields? Storing them in one field is easiest for me to explain so I'll show you that.
You can use the function implode() to compress an array into a string. I don't know how to explain what a string is except you are going to hear that word a lot and it, is like, just a collection of characters like letters, number, spaces, etc. It's nothing fancy like an array. Anyway, do this with $_GET['color']:
PHP Code:
$colorCompressed = implode(',', $_GET['color']);
$colorCompressed was an arbitrary name I chose. $_GET['color'] contains the original array and I wanted somewhere convenient to stick the imploded one. The process is the same with $_POST.
So the array, now turned into a string, looks like this:
red,blue,green
It's ready to stick in the database.
Later on, if you get it back out of the database, and want to turn it back into an array, you can use explode() So, okay, let's say you got it out of the database and ended up sticking it in the varible $color; Well here is what you would do:
$color = explode($color);
This would take "red,blue,green" in color, turn it into an array just like it was before, and stick it back in $color.
Now, radio buttons are different from checkboxes. You can't select multiple answers. At least, you shouldn't be able to. The only way HTML can tell that a set of radio buttons go together is if they all have the same name. So like if you ask the question "Which color is your favorite?" you name each radio button the same thing, "color" for instance.
Since only one answer is selected, you don't have to worry about messing with arrays or anything. The answer is going to be submitted just anything else in the form, e.g. If you had <input type='text' name='firstName'> and somebody entered "Larry", that would create $_GET['firstName'] or $_POST['firstName'] with the value "Larry"
So, you could just use $_GET['color'] or $_POST['color'] to access the value.
Hope this helps. Let us know if there are any more problems.
The better I get at programming, the more I appreciate arrays. Handy dandy things they are.
hmm... how actually you want to store your data. you may store those checkbox values as a,b,c using implode or in a json array or a simply serialize and store
would love to see your latest code to understand and reply better.
$getUSer =mysql_query("SELECT(*) from bel_iza where butiran_pesakit='$nama'");
$userExist = mysql_fetch_type($getUSer);
if ($userExist[0]>0) {
$status = "<font color=red>User already exist \"$nama\" .<br>Record is not save.</font>";
echo $status ;
} else {
$addUser = mysql_query("INSERT INTO user (nama,kpdokumendiri,tarikh)
VALUES ('$nama','$kpdokumendiri','$tarikh')");
if ($addUser) {
?>
<header>Tambah Pesakit</header>
<title><h2> Pesakit Sudah Ditambah </h2></Title>
<body>
Bookmarks