0

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>';
}
?>

3 Answers 3

1

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

A validator is a useful tool.

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

Comments

0

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=\"\"

1 Comment

The \ is needed, it appears in the middle of a PHP string delimited with " characters.
0

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>';
}
?>

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.