I have a problem want to be solved.
I have three pages. page1.php, page2.php
page1.php has a form with two text field. one text field is for name and another is for number. there is a submit button and a "next" button too. using session I want show given name into page2.php and create checkboxes that according to given number. (e.g if I write 4 in number text field in page1.php then 4 checkboxes will appear in page2.php, if 8 then 8 checkboxes.

I want solve this problem using session.

page1.php

<body>
<p>
        <?php
session_start(); 
?>
      </p>
      <form id="form1" name="form1" method="post" action="">
        <p>
          <label for="name">Name</label>
          <input type="text" name="name" id="name" />
        </p>
        <p>
          <label for="number">Number</label>
          <input type="text" name="number" id="number" />
        </p>
        <p>
          <input type="submit" name="submit" id="submit" value="SEND" />
        </p> <br/>
        <a href="page2.php"> next </a>
      </form>

<?php      
if(isset($_POST['submit']))
{
$_SESSION['Name'] = $_POST['name'];
$_SESSION['Number'] = $_POST['number'];
}

$nname = $_SESSION['Name'];
$nnumber = $_SESSION['Number'];
echo $nname . $nnumber;

?>

</body>

page2.php

<body>


      <p>
        <?php
session_start(); 



echo $_SESSION['Number'] . $_SESSION['Name'];

$nName = $_SESSION['Name'];
$nNumber = $_SESSION['Number'];

?>
  </p>
      <form name="form1" method="post" action="">
        <input type="text" name="first_name" value="<?php  echo $nName; ?>" />
        <input type="checkbox" name="<?php for($i=0; $i< $nNumber; $i++)
        echo $i;
         ?>" value="<?php for($i=0; $i< $nNumber; $i++)
        echo $i;
         ?>" />
      </form>
      <p> <br/>
        <a href="page1.php">Back</a></p>
</body>

Recommended Answers

All 5 Replies

I didn't fully understand you, so here is what I think
you need to write a name in page1 and number so in page 2 there will be checkboxes as the number you entered ?
here is what I did

page1
<form id="form1" name="form1" method="post" action="page2.php">
<p>
<label for="name">Name</label>
<input type="text" name="name" id="name" />
</p>
<p>
<label for="number">Number</label>
<input type="text" name="number" id="number" />
</p>
<p>
<input type="submit" name="submit" id="submit" value="SEND" />
</p> <br/>
</form>
page2
<?php
if ($_POST['submit']) {
$name = $_POST['name'];
$number = $_POST['number'];
}
?>
<form name="form1" method="post" action="">
<input type="text" name="first_name" value="<? echo $name; ?>" /><br/>
<? for ($i = 1 ; $i<=$number; $i++ ) {
echo ('<input type="checkbox" name="checkbox'.$i.'" value="'.$i.'"> Item '.$i.'<br/>');
}
?>
</form>

tell me if I was wrong .

Member Avatar for diafol

You need a limiter. Posting 100000000000000000000000000... checkboxes :)

Needs to be client-side AND server-side.

You can use a spinnerbox in the form, so that you can limit the min and max values:

<input type="number" min="1" max="10" value="1" />

Some browsers won't support 'number' and will default back to a plain textbox. In addition, you can still type anything you want into the spinnerbox. This and the fact that anybody can send malicious data, e.g. spoofed forms, will not guarantee 1-10 as a value. Therefore you need server-side checks too.

Thanks both of you.
I was actually looking for what "OsaMasw" said. Thanks "OsaMasw"! :) I was wrong in page2 line 10. I am new to php so don't know this simple thing. :)

Hi Friends I have another problem.

I have 5 checkboxes. What I want is.....
I want to check 3 checkboxes so other 2 will be unchecked. After clicking the next button a table will appear and checked checkboxes will show in one column and unchecked will be another column.

Can you give any idea how to solve it. i don't want the code, just idea and functions I can use.

Thanks in advance for your help. :)

Member Avatar for diafol

THis doesn't sound like a good design to me - I may be wrong. If these 'columned' checkboxes are still active then checking one of them should make it belong to the checkboxes in the other column.

If your checkboxes are named similarly (as in an array), then this may be easier:
Excuse the code - I was just messing around...

$checked = array();
$labels = array("People","Animals","Planets","Books","Food");
$no = count($labels); //number of checkboxes to check

if(isset($_POST['submit'])){

    for($x = 0; $x < $no ; $x++){
        if(isset($_POST['chk'][$x])){
            $checked[] = $x;    
        }else{
            $unchecked[] = $x;
        }
    }
    //for checking input    
    echo "<pre>";
    print_r($checked);
    print_r($unchecked);
    echo "</pre>";   
}


function checkIt($num, $checked){
    if(!empty($checked) && in_array($num,$checked)){
        return " checked='checked'";    
    }
}

?>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Blah</title>
</head>
<body>
<form method="post">
<?php
    for($x=0;$x<$no;$x++){
        echo "<input id='chk$x' type='checkbox' name='chk[$x]'" . checkIt($x,$checked) .  "/><label for='chk$x'>{$labels[$x]}</label><br />\n";
    }
?>  
    <input name="submit" type="submit" value="go"/>
</form>
</body>
</html>

That's not a solution of course, just a few things to give you an idea. This is all on one page, but you'd send the form to another page, so the php at the top could be used there.
Your html table columns could be filled by checking the 'checked and 'unchecked' arrays.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.