user3314254
user3314254

Reputation: 49

Getting database driven checkboxs value in php

I have two checkboxes in my page. The data for both checkboxes are driven from database. No i am trying to get the value of each checkbox but it displays on. here is my script

<?php include('conn.php'); ?>
<!doctype html>
<html><head>
<meta charset="utf-8">
<title>New Page</title>
<style type="text/css">
#div{
    height:500px; width:150px;  overflow:auto;
    border:thin;
    float:left;
}
#div2{
    height:auto;
    width:150px;
    overflow:auto;
    border:thin;
}
</style>
</head>

<body>
<form name="" action="2nd.php" method="post"> 
<div style="border:1; border-color:#006;">
<?php 
$sql=$connection->query("SELECT * FROM provinces");
echo "<div id=\"div\">"; 
while($result=$sql->fetch_assoc()){
    //echo "<input type=\"checkbox\" name=\""; echo $result['name'];  echo" \" "; echo $result['name']; echo" />"; 
    echo "
<input type=\"checkbox\" name=\"check[]\" value\""; echo $result['name']; echo" \" />"; 
echo $result['name']; 
echo "<br/>";
}
echo "</div>";
?>
<?php 
$sql=$connection->query("SELECT * FROM commodities");
echo "<div id=\"div2\">"; 
while($result=$sql->fetch_assoc()){
    //echo "<input type=\"checkbox\" name=\""; echo $result['name'];  echo" \" "; echo $result['name']; echo" />"; 
    echo "
<input type=\"checkbox\" name=\"check[]\" value\""; echo $result['name']; echo" \" />"; 
echo $result['name']; 
echo "<br/>";
}
echo "</div>";

?>
<input type="submit" name="save"/>
</div>
</form>
</body></html>

This code handles what comes from 1st page but it display on on.

<?php
$check=$_REQUEST['check'];
 foreach($check as $instrument)
{
    echo $instrument.'<br>';
}
?>

Upvotes: 0

Views: 61

Answers (3)

user3314254
user3314254

Reputation: 49

Thank you all for your help. Here is my complete working code.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>New Page</title>
<style type="text/css">
#div{
    height:500px;
    width:150px;
    overflow:auto;
    border:thin;
    float:left;
}
#div2{
    height:auto;
    width:150px;
    overflow:auto;
    border:thin;
}
</style>
</head>
<body>
<form name="" action="2nd.php" method="post"> 
<div style="border:1; border-color:#006;">
<?php 
$sql=$connection->query("SELECT * FROM provinces");
echo "<div id=\"div\">"; 
while($result=$sql->fetch_assoc()){
    //echo "<input type=\"checkbox\" name=\""; echo $result['name'];  echo" \" "; echo $result['name']; echo" />"; 
    echo "
<input type=\"checkbox\" name=\"check[]\" value=\""; echo $result['name']; echo" \" />"; 
echo $result['name']; 
echo "<br/>";
}
echo "</div>";
?>
<?php 
$sql=$connection->query("SELECT * FROM commodities");
echo "<div id=\"div2\">"; 
while($result=$sql->fetch_assoc()){
echo "<input type=\"checkbox\" name=\"check[]\" value=\""; echo $result['name']; echo" \" />"; 

echo $result['name']; 
echo "<br/>";
}
echo "</div>";

?>
<input type="submit" name="save"/>

</div>
</form>
</body>
</html>

2nd Page code which handle the form.

<?php
$check=$_REQUEST['check'];
 foreach($check as $instrument)
{
    echo $instrument.'<br>';
}
?>

Upvotes: 0

user2078937
user2078937

Reputation:

Note It that if you have not provided value="" then in this case checkbox it will show 'on'

so correct your syntax value\"" to value=\"\"

Upvotes: 0

Quentin
Quentin

Reputation: 943214

You need an = sign between the name of the value attribute and the "data".

A validator is a useful tool.

Upvotes: 1

Related Questions