1

I have a form that takes the following inputs:
Name: IBM
Surface(in m^2): 9
Floor: (Checkbox1)
Phone: (Checkbox2)
Network: (Checkbox3)
Button to send to a next php page.

All those values above are represented in a table when i press the submit button.
The first two (name and surname) are properly displayed in the table.
The problem is with the checkboxes. If i select the first checkbox the value in the table should be presented with 1. If its not selected the value in the table should be empty.

 echo "<td>$Name</td>"; // works properly
 echo "<td>$Surface</td>"; // works properly
 echo "<td>....no idea for the checkboxes</td>;

Some part of my php code with the variables:

<?php
if (!empty($_POST))
{
$name= $_POST["name"];
$surface= $_POST["surface"];
$floor= $_POST["floor"]; 
$phone= $_POST["telefoon"]; 
$network= $_POST["netwerk"]; 


if (is_numeric($surface)) 
{
    $_SESSION["name"]=$name;
    $_SESSION["surface"]=$surface;
    header("Location:ExpoOverzicht.php"); 
}
else 
{
    echo "<h1>Wrong input, Pleasee fill in again</h1>";
}  

if(!empty($floor) && ($phone) && ($network))
{
    $_SESSION["floor"]=$floor;
    $_SESSION["phone"]=$phone;
    $_SESSION["network"]=$network;
    header("Location:ExpoOverzicht.php"); 
}    
}

?>

Second page with table:

<?php

$name= $_SESSION["name"];
$surface= $_SESSION["surface"];
$floor= $_SESSION["floor"];
$phone= $_SESSION["phone"];
$network= $_SESSION["network"];

echo "<table class=\"tableExpo\">";

echo "<th>name</th>";
echo "<th>surface</th>";
echo "<th>floor</th>";
echo "<th>phone</th>";
echo "<th>network</th>";
echo "<th>total price</th>";

for($i=0; $i <= $_SESSION["name"]; $i++)
{
    echo "<tr>";

        echo "<td>$name</td>"; // gives right output
        echo "<td>$surface</td>"; // gives right output
        echo "<td>...</td>"; //wrong output (ment for checkbox 1)
        echo "<td>...</td>"; //wrong output (ment for checkbox 2)
        echo "<td>...</td>"; //wrong output (ment for checkbox 3)
        echo "<td>....</td>";

    echo "</tr>;";
}
echo "</table>";



<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="form1">
<h1>Vul de gegevens in</h1>
<table>
    <tr>
        <td>Name:</td>
        <td><input type="text" name="name" size="18"/></td>
    </tr>
    <tr>
        <td>Surface(in m^2):</td>
        <td><input type="text" name="surface" size="6"/></td>
    </tr>
    <tr>
        <td>Floor:</td>
        <td><input type="checkbox" name="floor" value="floor"/></td>
    </tr>
    <tr>
        <td>Phone:</td>
        <td><input type="checkbox" name="phone" value="phone"/></td>
    </tr>
    <tr>
        <td>Network:</td>
        <td><input type="checkbox" name="network" value="network"/></td>
    </tr>
    <tr>
        <td><input type="submit" name="verzenden" value="Verzenden"/></td>
    </tr>
</table>

There might be a few spelling mistakes since i had to translate it. Best regards.

2
  • And your html code looks like ??? Commented Apr 16, 2010 at 9:15
  • added the form html code. Everything is in 2 php files. Commented Apr 16, 2010 at 9:21

3 Answers 3

2

Instead of directly assigning your checkbox variables, see if they have been checked or not first.

$verdieping = isset($_POST["floor"]) ? $_POST["floor"] : 0; 
$telefoon = isset($_POST["telefoon"]) ? $_POST["telefoon"] : 0; 
$netwerk = isset($_POST["netwerk"]) ? $_POST["netwerk"] : 0; 

This way, if the user hasn't ticked a checkbox, you have a value of '0' assigned to it instead of an undefined variable.

Sign up to request clarification or add additional context in comments.

Comments

1

If you declare a checkbox with:

<input type="checkbox" name="mycheckbox" value="1">

you can check the value after submitting the form by:

if(!empty($_POST["mycheckbox"])) {
    // checkbox was checked
}
else {
    // checkbox was not checked
}

1 Comment

Hello, have that in my first php file. But what exactly do i put in my table td element to call upon that value?
1

In this php page you can write like this, it may be solution of your question

if (!empty($_POST))
{
$standnaam = $_POST["name"];
$oppervlakte = $_POST["surface"];
$verdieping = $_POST["floor"]; 
$telefoon = $_POST["telefoon"]; 
$netwerk = $_POST["netwerk"]; 


if (is_numeric($oppervlakte)) 
{
    $_SESSION["name"]=$standnaam;
    $_SESSION["surface"]=$oppervlakte;
    header("Location:ExpoOverzicht.php"); 
}
else 
{
    echo "<h1>Wrong input, Pleasee fill in again</h1>";
}  

if(!empty($verdieping) && ($telefoon) && ($netwerk))
{
    $_SESSION["floor"]=$verdieping;
    $_SESSION["phone"]=$telefoon;
    $_SESSION["network"]=$netwerk;
    header("Location:ExpoOverzicht.php"); 
}    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.