7 Replies - 806 Views - Last Post: 14 June 2011 - 07:06 PM Rate Topic: -----

#1 ithril  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-June 11

NEED HELP in forms! Please help!

Posted 14 June 2011 - 02:30 PM

So,i'm trying to create something like this.
My first page has a form that allows the user to enter a few names separated by commas. Now these names and separated through some inbuilt function. Now i want to make a Checkbox form and each checkbox belongs to a particular name. Now,what i need to ask is, how do i create checkboxes through a loop. I mean obviously the user can enter as many names as he wants. So,i dont know the exact number of checkboxes to make,so how do i pull something off like this? Please help.
and heres the code that i tried to make,which, obviously isn't working.
<html>
<body>
<?php

$fields=$_POST['fields'];
$dfield=split(",",$fields);
$i=0;
<form action=" echo $_SERVER['PHP_SELF'];" method="post">

while($i<count($dfield))
{
<input type="checkbox" name="dfields[]" value="echo $dfield[$i] /> <br/>
$i=$i+1;
}
<input type="submit name="submit" Value="Submit!" />
?>

</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: NEED HELP in forms! Please help!

#2 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 172
  • View blog
  • Posts: 615
  • Joined: 12-October 09

Re: NEED HELP in forms! Please help!

Posted 14 June 2011 - 02:47 PM

Hi, that code is quite wrong. You can't put HTML tags inside the PHP tags like that, you either have to do them outside of the <?php ?> tags, or echo/print them out, eg.

<?php
echo "<form><input type='text' /></form>";
?>



Anywho, i'm not exactly sure what you're wanting, it's difficult to understand. But if I understand correctly, you want to print out a checkbox for every name entered?

E.g. If I enter: "name1", "name2", "name3".

Then you want three checkboxes written out? Yes? No?

If that is what you mean, try this:

<html>
<body>
<?php

$fields = $_POST['fields'];
$dfield = explode(",", $fields);

echo "<form action=\"\" method=\"post\">";

for($i = 0; $i < count($dfield); $i++)
{
    echo "<input type=\"checkbox\" name=\"dfields[]\" value=\"".$dfield[$i]."\" /><br />";
}

echo "</form>";

?>

</body>
</html>



Was This Post Helpful? 0
  • +
  • -

#3 ithril  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-June 11

Re: NEED HELP in forms! Please help!

Posted 14 June 2011 - 02:54 PM

Well,i'm new to php. Just started like 2 days back. Could you please explain me the code from 8-15,if you don't mind.
Why are forward slashes used here- echo "<form action=\"\" method=\"post\">";
Was This Post Helpful? 0
  • +
  • -

#4 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 172
  • View blog
  • Posts: 615
  • Joined: 12-October 09

Re: NEED HELP in forms! Please help!

Posted 14 June 2011 - 03:03 PM

// This line is echoing something out to the page.
// In this case you are echoing out the string: <form action="" method="post">
// But because we are using the double quote in the echo - echo "something here"; We have to escape the double quotes in the string to display them, otherwise it would look like this: echo "<form action="" method="post">";
// And it will give you an error
echo "<form action=\"\" method=\"post\">";

// This is a for loop
// It's saying that $i = 0 and for as long as $i is less than count($dfield) [which is the number of elements in the array $dfields] it should do whatever code we put in between the {}
// And after it has finishing executing that code once, it should increment $i by 1, before checking if it is still less than count($dfield) and continuing the loop
for($i = 0; $i < count($dfield); $i++)
{
    // The reason for the \"\" in this line are the same as the previous one
    // We are also concatenating a variable onto our string: value=\"".$dfield[$i]."\"
    // Concatenating is where we add a variable (or another string, etc...) onto another string
    // E.g. $str = "my"; $str2 = "string"; echo "this is" . $str . " " . $str2;
    // That would print out: this is my string
    echo "<input type=\"checkbox\" name=\"dfields[]\" value=\"".$dfield[$i]."\" /><br />";
}

echo "</form>";


Was This Post Helpful? 0
  • +
  • -

#5 ithril  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-June 11

Re: NEED HELP in forms! Please help!

Posted 14 June 2011 - 03:14 PM

Okay,got it. Thanks a lot :D

And if you don't mind,could you please tell me,how do i recieve the checked checkboxes? I mean how do i recieve the information of which boxes are checked and which are not?
Was This Post Helpful? 0
  • +
  • -

#6 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 172
  • View blog
  • Posts: 615
  • Joined: 12-October 09

Re: NEED HELP in forms! Please help!

Posted 14 June 2011 - 03:32 PM

Have a play around with this example and see how it works:

<?php
if(isset($_POST['submit_fruit']) && !empty($_POST['fruit']))
{

    $fruit = $_POST['fruit'];

    $numberSelected = count($fruit);

    for($i = 0; $i < $numberSelected; $i++)
    {
        echo "Fruit Selected: " . $fruit[$i] . "<br />";
    }

}
?>
<html>
<body>

<form action="" method="post">
    <label>Select some Fruit:</label><br />
    <input type="checkbox" name="fruit[]" value="Apple" /> Apple<br />
    <input type="checkbox" name="fruit[]" value="Banana" /> Banana<br />
    <input type="checkbox" name="fruit[]" value="Pear" /> Pear<br />
    <input type="checkbox" name="fruit[]" value="Orange" /> Orange<br />
    <input type="submit" name="submit_fruit" />
</form>
</body>
</html>




Was This Post Helpful? 0
  • +
  • -

#7 ithril  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-June 11

Re: NEED HELP in forms! Please help!

Posted 14 June 2011 - 04:01 PM

Is it possible to initialise the values of each checkbox to something unique like this? Because my program requires user based output.
echo '<input type="checkbox" name="field[]" value=$dfield[$i] />'.' '.$dfield[$i].'<br/>'

<html>
<body>
<?php

$fields=$_POST['fields'];
$dfield=split(",",$fields);

$i=0;
echo " <i> Please check the fields that are going to behave as keys </i> <br/><br/>";

echo '<form action="mt.php" method="post">';

while($i<count($dfield))
{
echo '<input type="checkbox" name="field[]" value=$dfield[$i] />'.' '.$dfield[$i].'<br/>' ; ---IS THIS LINE CORRECT?
$i=$i+1;
}
echo '<input type="submit" name="submit" value="Submit!" />';
?>

</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#8 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 172
  • View blog
  • Posts: 615
  • Joined: 12-October 09

Re: NEED HELP in forms! Please help!

Posted 14 June 2011 - 07:06 PM

It would be:

echo '<input type="checkbox" name="field[]" value="'.$dfield[$i].'" /> ' . $dfield[$i] . '<br />';
$i++;


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1