Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7172767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:41:05+00:00 2026-05-28T15:41:05+00:00

I’ve a doubt. I’ve 3 textboxes and each is having checkboxes next to it.

  • 0

I’ve a doubt. I’ve 3 textboxes and each is having checkboxes next to it. I want to display
the values of only those textboxes whose respective checkboxes are clicked. Following is the attached HTML and PHP codes:

<html>
    <head>
    </head>
        <body>
            <form name="f" method="post" action="4.php">
                <table>
                  <tr>
                    <th> Facility </th> 
                  </tr>
                  <tr>
                    <td><input type="text" name="a1" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Hostel"></td>
                  </tr> 
                  <tr>  
                    <td><input type="text" name="b1" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Transport"></td>
                  </tr> 
                  <tr>
                    <td><input type="text" name="c1" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Food"></td>
                  </tr> 
                  <tr>
                    <td colspan="3"><input type="submit" value="submit" /></td>
                  </tr>
                 </table>   
               </form>
          </body>
</html>

and below is the PHP part.

<?php
    $a=$_POST['a1'];
    $b=$_POST['b1'];
    $c=$_POST['c1'];

    $facilityArray = $_POST['facility'];
    $facility = "";
    if(count($facilityArray) > 0)
    {
        foreach($facilityArray as $fac)
        {
            $facility .= " " . $fac;
        }
    }

    echo $facility; echo "<br>"; 
    echo $a; echo "<br>";
    echo $b; echo "<br>";
    echo $c;
?>

With the help of following codes I am able to display all the values of checked checkboxes. I am also able to display the values of all the textboxes. But I actually want to display the values of only those textboxes whose respective checkboxes are clicked. I know it may be a very basic question but please help me grow in PHP. Thanks in advance… 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:41:06+00:00Added an answer on May 28, 2026 at 3:41 pm

    Your textboxes should also be in an array post to achieve this.

    To achieve this change the input lines as:

    <td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
    

    From php you’ll be getting the posted textboxes in an array as:

    $textbox=$_POST['textboxes'];
    

    You should then loop through the checkboxes array and if the corresponding checkbox is "on" (clicked), then display the textboxes value. To do this you would also need a counter to make sure you are on the same array index for both checkboxes and textboxes:

     if(count($facilityArray) > 0)
    {
        $i = 0;
        foreach($facilityArray as $fac)
        {
            if($fac == "on")
            {
                echo $textbox[$i] . "</br>";
            }
            $i ++;
        }
    }
    

    I’ve also added a name to your submit button so you only check the form when it is submitted.
    Your page should now look something like this:

            <?php
        if(isset($_POST['submit']))
        {
            $textbox=$_POST['textboxes'];
        
            $facilityArray = $_POST['facility'];
            
            if(count($facilityArray) > 0)
            {
                $i = 0;
                foreach($facilityArray as $fac)
                {
                    if($fac == "on")
                    {
                        echo $textbox[$i] . "</br>";
                    }
                    $i ++;
                }
            }
        
        }
        ?>
        <form name="f" method="post" action="4.php">
                    
        
            <table>
                          <tr>
                            <th> Facility </th> 
                          </tr>
                          <tr>
                            <td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
                          </tr> 
                          <tr>  
                            <td><input type="text" name="textboxes[]" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
                          </tr> 
                          <tr>
                            <td><input type="text" name="textboxes[]" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
                          </tr> 
                          <tr>
                            <td colspan="3"><input name="submit" type="submit" value="submit" /></td>
                          </tr>
            </table>
        </form>
    

    UPDATE:

    To make sure that the $_POST variable exists before assigning it to a variable we use the isset(). In your case just update the php segment as:

    <?php
        if(isset($_POST['submit']))
        {
            if(isset($_POST['textboxes']))
            {
                $textbox=$_POST['textboxes'];
                
                if(isset($_POST['facility']))
                {
                    $facilityArray = $_POST['facility'];
                    
                    if(count($facilityArray) > 0)
                    {
                        $i = 0;
                        foreach($facilityArray as $fac)
                        {
                            if($fac == "on")
                            {
                                echo $textbox[$i] . "</br>";
                            }
                            $i ++;
                        }
                    }
                }
            }
        
        }
    ?>
    

    Where the only changes are the addition of another two if statements that take a boolean flag from the isset() function according to whether the $_POST variable has been posted successfully

    if(isset($_POST['textboxes']))
    

    AND

    if(isset($_POST['facility']))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
i want to parse a xhtml file and display in UITableView. what is the
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.