Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 51
  1. #16
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.

  2. Users who have thanked mlseim for this post:

    mohammedsali (08-08-2012)

  3. #17
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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();
    ?>

  4. #18
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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?



    .

  5. Users who have thanked mlseim for this post:

    mohammedsali (08-08-2012)

  6. #19
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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();
    ?>

  7. #20
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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); 

  8. Users who have thanked mlseim for this post:

    mohammedsali (08-08-2012)

  9. #21
    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
    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
    Last edited by Len Whistler; 08-08-2012 at 10:04 PM.
    Leonard Whistler

  10. #22
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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).

  11. #23
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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

  12. #24
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.

  13. Users who have thanked mlseim for this post:

    mohammedsali (08-09-2012)

  14. #25
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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

  15. #26
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.

  16. #27
    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
    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

    ----
    Last edited by Len Whistler; 08-09-2012 at 08:16 AM.
    Leonard Whistler

  17. #28
    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
    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

  18. #29
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Len is predicting the next thing ...
    Mo will want to populate the radio buttons using the database.
    ... good one

  19. #30
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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


 
Page 2 of 4 FirstFirst 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •