Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 4.50 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-08-2012, 03:25 AM   PM User | #16
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
but now, in your PHP script you need to do something with this: name="inp6"

"inp6" is the value of the text box, but is not part of the "petathome[]" array.

So your $pets will have a value of "Others", because of the radio button.
It will not have the value of the things they type into the text box.
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
mohammedsali (08-08-2012)
Old 08-08-2012, 03:24 PM   PM User | #17
mohammedsali
New Coder

 
Join Date: Aug 2012
Posts: 22
Thanks: 12
Thanked 0 Times in 0 Posts
mohammedsali is an unknown quantity at this point
Hi mlseim,

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>";
}

else {
echo "ERROR";
}
?>

<?php
// close connection
mysql_close();
?>
mohammedsali is offline   Reply With Quote
Old 08-08-2012, 04:01 PM   PM User | #18
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
You're using radio buttons now instead of checkboxes,
therefore, you will only be entering one animal. You
can't select more than one radio button.

So I don't understand what this means:

quote:
"... the values entered in the text box along with the values of petathome[]"

along with?
it can't be along with.

do you mean "use the text box value instead of the petathome values?



.
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
mohammedsali (08-08-2012)
Old 08-08-2012, 08:12 PM   PM User | #19
mohammedsali
New Coder

 
Join Date: Aug 2012
Posts: 22
Thanks: 12
Thanked 0 Times in 0 Posts
mohammedsali is an unknown quantity at this point
Hi mlseim,

Thank you once again and sorry for the confusion.

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">

Full Name: <input name="FullName" type="text" id="FullName" required="required" /><br />
<br />
Number: <input name="Number" type="text" id="Number" required="required" /><br />
<br />
Email Address: <input name="EmailAddress" type="text" id="EmailAddress" required="required" /><br /><br />
<br/>
Address: <input name="Address" type="text" id="Address" required="required" /><br />
<br />
Which animal do you want to keep?<br />
<script type="text/javascript">
var currentEnabled = null;
function enableElement(elem) {
if (currentEnabled) {
currentEnabled.disabled = true;
}
elem.disabled = false;
currentEnabled = elem;
}
</script>
<form action="">
<input type="radio" name="petathome[]" value="Dog"
onclick="enableElement(this.form.elements['inp1']);"> Dog
<br>
<input type="radio" name="petathome[]" value="Cat"
onclick="enableElement(this.form.elements['inp2']);"> Cat
<br>
<input type="radio" name="petathome[]" value="Lion"
onclick="enableElement(this.form.elements['inp3']);"> Lion
<br>
<input type="radio" name="petathome[]" value="Tiger"
onclick="enableElement(this.form.elements['inp4']);"> Tiger
<br>
<input type="radio" name="petathome[]" value="Leopard"
onclick="enableElement(this.form.elements['inp5']);"> Leopard
<br>
<input type="radio" name="petathome[]" value="Others"
onclick="enableElement(this.form.elements['inp6']);">
Others: <input type="text" name="inp6" disabled="disabled">

<br>
<br />

<input type="submit" name="submit" value="Submit" />

</form>
</body>
</html>

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>";
}

else {
echo "ERROR";
}
?>

<?php
// close connection
mysql_close();
?>
mohammedsali is offline   Reply With Quote
Old 08-08-2012, 09:44 PM   PM User | #20
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
If you're no longer using checkboxes, you no longer need to use arrays ...

So this ... I still don't understand the use of radio buttons, but it is what it is ...

PHP Code:

<input type="radio" name="petathome" value="Dog"
onclick="enableElement(this.form.elements['inp1']);"Dog
<br>
<
input type="radio" name="petathome" value="Cat"
onclick="enableElement(this.form.elements['inp2']);"Cat
<br>
<
input type="radio" name="petathome" value="Lion"
onclick="enableElement(this.form.elements['inp3']);"Lion
<br>
<
input type="radio" name="petathome" value="Tiger"
onclick="enableElement(this.form.elements['inp4']);"Tiger
<br>
<
input type="radio" name="petathome" value="Leopard"
onclick="enableElement(this.form.elements['inp5']);"Leopard
<br>
<
input type="radio" name="otherpet" value="Others"
onclick="enableElement(this.form.elements['inp6']);">
Others: <input type="text" name="inp6" disabled="disabled"

PHP Code:

// 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); 
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
mohammedsali (08-08-2012)
Old 08-08-2012, 09:57 PM   PM User | #21
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
Below is a db structure that I think would make updating and changing the info easier. It has three tables: MEMBERS, PETS and MEMBERS_PETS.

MEMBERS
member_id | name | email | address

0001 | Ben | ben@yahoo.com | 1234 Main St.
0002 | Brian | brian@hotmail.com | 12 Elm St.
0003 | Jill | jill_smith@yahoo.com | 546 12th Avenue
0004 | Kevin | k@gmail.com | 111 Spruce St.
0005 | Jackie | Jackie@yahoo.com | 7777 5th St.
0006 | Ben W | ben.w@gmail.com | 1284 West St.


PETS
pet_id | pet
001 | Dog
002 | Cat
003 | Lion
004 | Tiger
005 | Leopard


MEMBERS_PETS
id | pet_id | member_id
0001 | 005 | 0002
0002 | 005 | 0006
0003 | 001 | 0002
0004 | 003 | 0001
0005 | 002 | 0005
0006 | 002 | 0002
0007 | 002 | 0001


Bold = Primary Keys
Italic = Foreign Keys
__________________
Leonard Whistler

Last edited by Len Whistler; 08-08-2012 at 10:04 PM..
Len Whistler is offline   Reply With Quote
Old 08-08-2012, 10:59 PM   PM User | #22
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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).
Fou-Lu is offline   Reply With Quote
Old 08-08-2012, 11:49 PM   PM User | #23
mohammedsali
New Coder

 
Join Date: Aug 2012
Posts: 22
Thanks: 12
Thanked 0 Times in 0 Posts
mohammedsali is an unknown quantity at this point
Hi mlseim,

Thank you for your reply

It works fine, expect one small thing.

On the html page when selecting:
Dog
Cat
Lion
Tiger
Leopard

it all is fine, but when click on Others, there are two dots. One on Others and the other or the last selected option.

But the important thing is if any other option is selected after selecting Others the Others field is disable which is great.

Please let me know if this is something that can be fixed?

Thanks & Regards,
mohammedsali
mohammedsali is offline   Reply With Quote
Old 08-09-2012, 12:32 AM   PM User | #24
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Because you're using radio buttons.

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.



.

Last edited by mlseim; 08-09-2012 at 12:36 AM..
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
mohammedsali (08-09-2012)
Old 08-09-2012, 12:59 AM   PM User | #25
mohammedsali
New Coder

 
Join Date: Aug 2012
Posts: 22
Thanks: 12
Thanked 0 Times in 0 Posts
mohammedsali is an unknown quantity at this point
Hi mlseim,

Sorry if any confusion. Will try to clear it up.

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.

Thanks & Regards,
mohammedsali
mohammedsali is offline   Reply With Quote
Old 08-09-2012, 02:58 AM   PM User | #26
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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 Code:

<!DOCTYPE html>
<
html>
<
body background="blue">
<
form action="test_post.php" method="post">

Full Name: <input name="FullName" type="text" id="FullName" required="required" /><br />
<
br />
Number: <input name="Number" type="text" id="Number" required="required" /><br />
<
br />
Email Address: <input name="EmailAddress" type="text" id="EmailAddress" required="required" /><br /><br />
<
br/>
Address: <input name="Address" type="text" id="Address" required="required" /><br />
<
br />
Which animal do you want to keep?<br />
<
script type="text/javascript">
var 
currentEnabled null;
function 
enableElement(elem) {
if (
currentEnabled) {
currentEnabled.disabled true;
}
elem.disabled false;
currentEnabled elem;
}
</script>
<form action="">
<input type="radio" name="petathome" value="Dog"> Dog <br> 
<input type="radio" name="petathome" value="Cat"> Cat <br> 
<input type="radio" name="petathome" value="Lion"> Lion <br> 
<input type="radio" name="petathome" value="Tiger"> Tiger <br> 
<input type="radio" name="petathome" value="Leopard"> Leopard <br> 
<input type="radio" name="petathome" value="Others" 
onclick="enableElement(this.form.elements['inp6']);"> 
Others: <input type="text" name="inp6" disabled="disabled"> <br>
<br />

<input type="submit" name="submit" value="Submit" />

</form>
</body>
</html> 

Php code is:
PHP Code:
<?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']); 

$pets $petathome;
if(
$petathome == "Others"){
$pets $inp6;
}
$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>";
}

else {
echo 
"ERROR";
}
?>

<?php
// close connection
mysql_close();
?>

Last edited by mlseim; 08-09-2012 at 03:04 AM..
mlseim is offline   Reply With Quote
Old 08-09-2012, 08:10 AM   PM User | #27
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
Quote:
Originally Posted by mohammedsali View Post
Hi mlseim,

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.
Code:
PETS
pet_id | pet
001 | Dog
002 | Cat
003 | Lion
004 | Tiger
005 | Leopard
006 | Hamster

----
__________________
Leonard Whistler

Last edited by Len Whistler; 08-09-2012 at 08:16 AM..
Len Whistler is offline   Reply With Quote
Old 08-09-2012, 08:42 PM   PM User | #28
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
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);
//////////////////////////////////////////////////////////

foreach ($pets as $pet) {
echo 
"<input type=\"radio\" name=\"petathome\" value=\"$pet\"> $pet <br> ";
}
?>
<input type="radio" name="petathome" value="Others" 
onclick="enableElement(this.form.elements['inp6']);"> 
Others: <input type="text" name="inp6" disabled="disabled"> <br>
<br />
<input type="submit" name="submit" value="Submit" />
</form>
__________________
Leonard Whistler
Len Whistler is offline   Reply With Quote
Old 08-09-2012, 11:39 PM   PM User | #29
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Len is predicting the next thing ...
Mo will want to populate the radio buttons using the database.
... good one
mlseim is offline   Reply With Quote
Old 08-13-2012, 07:18 PM   PM User | #30
mohammedsali
New Coder

 
Join Date: Aug 2012
Posts: 22
Thanks: 12
Thanked 0 Times in 0 Posts
mohammedsali is an unknown quantity at this point
Hi mlseim,

Sorry for the delayed reply

Thank you for the help, have it working now

Thank you Len Whistler for the suggestions

Regards,
mohammedsali
mohammedsali is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:22 PM.


Advertisement
Log in to turn off these ads.