Yes, it only show Other value of the radio box but does not insert the text box values. Did not notice that earlier.
The php code is as below where do i enter "name="inp6" so it enters the values entered in the text box along with the valus of petathome[]
<?php
$host="host"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="dbname"; // Database name
$tbl_name="tblname"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$FullName=mysql_real_escape_string($_POST['FullName']);
$Number=mysql_real_escape_string($_POST['Number']);
$EmailAddress=mysql_real_escape_string($_POST['EmailAddress']);
$Address=mysql_real_escape_string($_POST['Address']);
$petathome=$_POST['petathome'];
// Loop through the array of checked box values ...
$pets="";
$flag=0;
foreach($petathome as $entry){
$pets .= $entry.", ";
$flag=1;
}
if($flag==1){
$pets=rtrim($pets);
}
mysql_real_escape_string($pets);
// Insert data into mysql
$sql="INSERT INTO $tbl_name(FullName, Number, EmailAddress, Address, petathome)VALUES('$FullName', '$Number', '$EmailAddress', '$Address', '$pets')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='testpage.html'>Back to main page</a>";
}
Yes, there would one radio option selected at any given point of time.
Along with the list of animals i have one radio box with text field next to it. Only on clicking this radio button the text field is highlighted. Clicking on this radio box the customer can any thing in it which will be inserted into petathome.
Html code is:
<!DOCTYPE html>
<html>
<body background="blue">
<form action="test_post.php" method="post">
Php code is:
<?php
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="dbname"; // Database name
$tbl_name="tblname"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$FullName=mysql_real_escape_string($_POST['FullName']);
$Number=mysql_real_escape_string($_POST['Number']);
$EmailAddress=mysql_real_escape_string($_POST['EmailAddress']);
$Address=mysql_real_escape_string($_POST['Address']);
$petathome=$_POST['petathome'];
// Loop through the array of checked box values ...
$pets="";
$flag=0;
foreach($petathome as $entry){
$pets .= $entry.", ";
$flag=1;
}
if($flag==1){
$pets=rtrim($pets);
}
mysql_real_escape_string($pets);
// Insert data into mysql
$sql="INSERT INTO $tbl_name(FullName, Number, EmailAddress, Address, petathome)VALUES('$FullName', '$Number', '$EmailAddress', '$Address', '$pets')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='testpage.html'>Back to main page</a>";
}
// Get values from form
$FullName=mysql_real_escape_string($_POST['FullName']);
$Number=mysql_real_escape_string($_POST['Number']);
$EmailAddress=mysql_real_escape_string($_POST['EmailAddress']);
$Address=mysql_real_escape_string($_POST['Address']);
$petathome=$_POST['petathome'];
$otherpet=$_POST['otherpet'];
$other_text=mysql_real_escape_string($_POST['inp6']);
// Loop through the array of checked box values ...
$pets=$petathome;
if($otherpet=="Others"){
$pets .= "|".$other_text;
}
mysql_real_escape_string($pets);
I suggested as well a more normalized solution to the database for ease (although without example).
I would use the above structure, except I would drop the id from MEMBERS_PETS and use a composite key instead. I don't like the surrogates at all, so I'd probably take it off the PETS, but likely leave it on the members (or shift it to email assume it has to be unique).
When you click a radio button, an animal is selected.
But then you can't remove the choice ... you can only
select a different one.
I don't understand the specs.
Can I select a Tiger AND select "other" ... typing Monkey in the text box?
Or can I only select an animal OR select "other", but not both?
Still ... stumped on the radio button thing.
The relationship between the animals and the "other" button is odd.
Normalizing the database is pretty far-fetched ... he can't even
figure-out the radio button snafu. I can't figure-out what the site
is even supposed to do.
Mo, I think you'll have to go through some online tutorials.
The radio button options are:
() Dog
() Cat
() Lion
() Tiger
() Leopard
() Others [____________] <<Text box
The user has the option to select any one of the radio button options. If the animal is not part of the listed options, the user will click on "Others" radio button and enter the animal name.
Which every is the selected value:
Dog or Cat or Lion or Tiger or Leopard or the value user has entered.
So the user can only select one option at any given time which gets entered into the database.
There is no reason to enable or disable the text box.
The text box value does nothing unless the "Others" radio button is selected.
Whether there is text or not makes no difference if any other animal is selected.
<?php
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="dbname"; // Database name
$tbl_name="tblname"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$FullName=mysql_real_escape_string($_POST['FullName']);
$Number=mysql_real_escape_string($_POST['Number']);
$EmailAddress=mysql_real_escape_string($_POST['EmailAddress']);
$Address=mysql_real_escape_string($_POST['Address']);
$petathome=$_POST['petathome'];
$inp6=mysql_real_escape_string($_POST['inp6']);
// Insert data into mysql
$sql="INSERT INTO $tbl_name(FullName, Number, EmailAddress, Address, petathome)VALUES('$FullName', '$Number', '$EmailAddress', '$Address', '$pets')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='testpage.html'>Back to main page</a>";
}
The radio button options are:
() Dog
() Cat
() Lion
() Tiger
() Leopard
() Others [____________] <<Text box
The user has the option to select any one of the radio button options. If the animal is not part of the listed options, the user will click on "Others" radio button and enter the animal name.
Which every is the selected value:
Dog or Cat or Lion or Tiger or Leopard or the value user has entered.
So the user can only select one option at any given time which gets entered into the database.
The above example is why a table for pets will make things so much better. Take the DB structure I posted for the PETS table.
Code:
PETS
pet_id | pet
001 | Dog
002 | Cat
003 | Lion
004 | Tiger
005 | Leopard
The above would generate your radio buttons, plus your others option. Someone picks others and enters Hamster. Hamster gets entered into the PETS table and is added to the radio buttons automatically, you can also output the radio buttons alphabetically to make selections easier to find. Any typos you would manually fix and if it matches an existing entry it doesn't get entered again.
Updated PETS table after Hamster is entered in the others text box.
Based on mlseim's code for the radio button form I added some PHP with an array of pets.
PHP Code:
<form action="">
<?php
//php sorted array - inplace of mysql db for demo purposes.
$pets = array('Dog','Cat','Lion','Tiger','Leopard',);
sort($pets);
//////////////////////////////////////////////////////////