I have this html code:
<tr>
<td><label><input type="text" name="id" class="DEPENDS ON info BEING student" id="example">ID</label></td>
</tr>
<tr>
<td>
<label> <input type="checkbox" name="yr" class="DEPENDS ON info BEING student"> Year</label>
</td>
</tr>
But I don't have any idea on how do I check this checkboxes if they are checked using php, and then output the corresponding data based on the values that are checked.
Please help, I'm thinking of something like this. But of course it won't work, because I don't know how to equate checkboxes in php if they are checked:
<?php
$con = mysql_connect("localhost","root","nitoryolai123$%^");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("school", $con);
$id = mysql_real_escape_string($_POST['idnum']);
if($_POST['id'] == checked & $_POST['yr'] ==checked ){
$result2 = mysql_query("SELECT * FROM student WHERE IDNO='$id'");
echo "<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['IDNO'] . "</td>";
echo "<td>" . $row['YEAR'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
mysql_close($con);
?>
You must give your checkboxes a value. This value gets send to the server, in case the checkbox is checked.
if ( $_POST['checkboxname'] == 'checkboxvalue' ) {
}
Since I see no form:
To send the data to the server, you need a form around your input elements:
<form method="POST" action="myphpscript.php">
YOUR CONTENT HERE
</form>
try the following:
if (isset($_POST['yr'])) { ... }
$_POST['yr'] == checked
should be:
$_POST['yr'] == 'on'
The default for firefox is 'on', maybe different in other browsers. (Thanks to David)
If you include a hidden field, with the same name and the failure value that you want to show up in the post data, then when the checkbox does not return a value (it is unchecked), the hidden control on the form will.
echo '<form method="post"><input type="hidden" name="checkdata" value="0">\
<input type="checkbox" name="checkdata" value="1">\
<input name="submitbutton" type="submit"></form>\
</body></html>';
if ($_POST['submitbutton']) {
echo "Value:|".$_POST['checkdata']."|";
}
Related
by using this code i can now fetch pro_price table but i need multiple column saerch. this is my working code for an solo column, but i need to fetch two column more from that table. How i can i do that. please help
Database name: auction
Table name : addproduct
Column names : pro_price, pro_code, hsn_code
This is my code,
if(isset($_REQUEST['search'])){
$pro_price = $_REQUEST['pro_price'];
foreach ($_REQUEST['pro_price'] as $pro_price) {
$statearray[] = mysql_real_escape_string($pro_price);
}
$states = implode ("','", $statearray);
$sql = "SELECT * FROM addproduct WHERE pro_price IN ('$states'))";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) == 0)
{
echo "Sorry, but we can not find an entry to match your query...<br><br>";
}
else
{
echo "<table border='1' width='900' class='srchrslt'>
<tr class='head'>
<td>pro_name</td>
<td>pro_brand</td>
<td>hsn_code</td>
<td>pro_tax2</td>
<td>pro_tax3</td>
</tr>";
while($row = mysql_fetch_assoc( $result ))
{
echo "<tr>";
echo "<td>" . $row['pro_name'] . " </td>";
echo "<td>" . $row['pro_brand'] . " </td>";
echo "<td>" . $row['hsn_code'] . " </td>";
echo "<td>" . $row['pro_tax2'] . " </td>";
echo "<td>" . $row['pro_tax3'] . " </td>";
echo "</tr>";
}
echo "</table>";
}
}
please help...and thank you
You do a SELECT *, which returns all columns in a table. In your case, it seems there's only a single column, so either you need to add additional columns to the table, or alternatively, you need to figure out whether perhaps you have not been granted permissions to see all columns in the table.
UPDATE:
It seems what you really want to do is add additional search criteria. This would mean your query would become something like the following:
SELECT * FROM addproduct WHERE pro_price IN ('$states') OR pro_code IN ('$codes')
For that to work, you would have to do what you already with the selected prices (i.e. store them i a variable called $states). In the SQL line above, I assume you store the selected values of the codes in a similar variable called $codes.
The full code with this addition would be something like tis:
if(isset($_REQUEST['search'])){
$pro_price = $_REQUEST['pro_price'];
$pro_code = $_REQUEST['pro_code'];
foreach ($_REQUEST['pro_price'] as $pro_price) {
$statearray[] = mysql_real_escape_string($pro_price);
}
foreach ($_REQUEST['pro_code'] as $pro_code) {
$codesarray[] = mysql_real_escape_string($pro_code);
}
$states = implode ("','", $statearray);
$codes = implode ("','", $codesarray);
$sql = "SELECT * FROM addproduct WHERE pro_price IN ('$states') OR pro_code IN ('$codes')";
Note that I'm not a PHP coder, so it is entirely possible the code can be optimized. I just wanted to show you what I think would work.
At last I got the solutions to my question. Thanks to #SchmitzIT who had supported me. Iam posting the code for further candidates.
index.php
<form name="search" method="post" action="searchplant">
<table width="900" border="1" class="srch">
<tr class="head"><td>Pro Price</td></tr>
<tr>
<td>
<input type="checkbox" value="250" name="pro_price[]">250<br />
<input type="checkbox" value="80" name="pro_price[]">80<br />
<input type="checkbox" value="50" name="pro_price[]">50<br />
<input type="checkbox" value="40" name="pro_price[]">40<br />
<input type="checkbox" value="299" name="pro_price[]">299<br />
<td>
</tr>
<tr class="head"><td>hsncode</td></tr>
<tr>
<td>
<input type="checkbox" value="101101" name="hsn_code[]">101101<br />
<input type="checkbox" value="101102" name="hsn_code[]">101102<br />
<input type="checkbox" value="101103" name="hsn_code[]">101103<br />
<input type="checkbox" value="101104" name="hsn_code[]">101104<br />
<input type="checkbox" value="101105" name="hsn_code[]">101105<br />
<td>
</tr>
<tr class="head"><td>procode</td></tr>
<tr>
<td>
<input type="checkbox" value="101" name="pro_code[]">101<br />
<input type="checkbox" value="102" name="pro_code[]">102<br />
<input type="checkbox" value="103" name="pro_code[]">103<br />
<input type="checkbox" value="104" name="pro_code[]">104<br />
<input type="checkbox" value="105" name="pro_code[]">105<br />
<td>
</tr>
<tr><td colspan="3" align="Right">
<input type="submit" name="search" value="Search" /></td></tr>
</table>
</form>
</div><!-- end service-->
<div id="media" class="group">
search.php
if(isset($_REQUEST['search']))
{
$pro_price = $_REQUEST['pro_price'];
$pro_code = $_REQUEST['pro_code'];
$hsn_code = $_REQUEST['hsn_code'];
foreach ($_REQUEST['pro_price'] as $pro_price) {
$statearray[] = mysql_real_escape_string($pro_price);
}
foreach ($_REQUEST['pro_code'] as $pro_code) {
$codesarray[] = mysql_real_escape_string($pro_code);
}
foreach ($_REQUEST['hsn_code'] as $hsn_code) {
$hsnarray[] = mysql_real_escape_string($hsn_code);
}
$states = implode ("','", $statearray);
$codes = implode ("','", $codesarray);
$hsn = implode ("','", $hsnarray);
$sql = "SELECT * FROM addproduct WHERE pro_price IN ('$states') OR pro_code IN ('$codes') OR hsn_code IN ('$hsn')";
//Now we search for our search term, in the field the user specified
$result = mysql_query($sql) or die(mysql_error());
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
if (mysql_num_rows($result) == 0)
{
echo "Sorry, but we can not find an entry to match your query...<br><br>";
}
else
{
echo "<table border='1' width='900' class='srchrslt'>
<tr class='head'>
<td>pro_name</td>
<td>pro_brand</td>
<td>hsn_code</td>
<td>pro_price</td>
<td>pro_code</td>
</tr>";
//And we display the results
while($row = mysql_fetch_assoc( $result ))
{
echo "<tr>";
echo "<td>" . $row['pro_name'] . " </td>";
echo "<td>" . $row['pro_brand'] . " </td>";
echo "<td>" . $row['hsn_code'] . " </td>";
echo "<td>" . $row['pro_price'] . " </td>";
echo "<td>" . $row['pro_code'] . " </td>";
echo "</tr>";
}
echo "</table>";
}
}
Just change the database name, table name, column name and values according to your data.
Hope you find this useful
I'm new in PHP. some one please help to stop inserting blank data when refreshing the browser. I'm trying to insert data in SQL database from a HTML form & showing them in the same page in a table.
here is my from & PHP code
Thanks in advance....
</head>
<body>
<?php
$sub = mysql_connect ("localhost", "root","");
if (!$sub)
{
die('Could Not Connect:'.mysql_erro());
}
mysql_select_db("test", $sub);
$sql="INSERT INTO te(Dat,Sadi,Jam,Washi) VALUES('$_POST[Dat]','$_POST[Sadi]','$_POST[Jam]','$_POST[Washi]')";
if (!mysql_query($sql, $sub))
{
die('Error:'.mysql_error());
}
echo 'One Record added';
$result=mysql_query("SELECT * FROM te");
echo "<table border='1'>;
<tr>
<th>Date</th>
<th>Sadi</th>
<th>Jam</th>
<th>Washi</th>
</tr>";
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" .$row['Dat']."</td>";
echo "<td>" .$row['Sadi']."</td>";
echo "<td>" .$row['Jam']."</td>";
echo "<td>" .$row['Washi']."</td>";
echo "</tr>";
}
echo "</table>";
Mysql_close($sub);
?>
</body>
<form action="index.php" method="post">
<input type="text" name="Dat" placeholder="Date, DD-MM-YY">
<input type="text" name="Sadi" placeholder="Sadi">
<input type="text" name="Jam" placeholder="Jam">
<input type="text" name="Washi" placeholder="Washi">
<input type="submit" name="sub">
</form>
Just verify if the $_POST is not empty
if(!empty($_POST))
{
$sql="INSERT INTO te(Dat,Sadi,Jam,Washi) VALUES('$_POST[Dat]','$_POST[Sadi]','$_POST[Jam]','$_POST[Washi]')";
if (!mysql_query($sql, $sub))
{
die('Error:'.mysql_error());
}
}
I have a table with pizza name, pizza type and price. I used a loop to print all the items in a table.
<form method="Cart.php" method="post">
<table border="1" cellpadding="10">
<tr>
<th>Pizza name</th>
<th>Pizza type</th>
<th>Price</th>
</tr>
<?php
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['subcat_name'] . "</td>";
echo "<td>" . $row['cat_name'] . "</td>";
echo "<td>" . $row['price']. "</td>";
echo "<td><input type=\"checkbox\" name=\"checkbox\" value=\"\" id=\"checkbox\"></td>";
echo "</tr>";
}
echo "</table>";
?>
<input type="submit" name="addToCart" id="addToCart"/>
</form>
Now I want to access the pizza names and prices as I click the submit button.
You can access them by first checking if it exists...
if(isset($_POST['checkbox']){
$checkbox_value = $_POST['checkbox'];
}else{
$checkbox_value = ""; // set a default value here
}
I should add that checkboxes that are NOT "checked" will not pass along in a POST, so you need to explicitly check if it has been "checked" by calling
if(isset($_POST['checkbox'])){}
Which at that point you can decide to set a value yourself or use the value you set in the form.
Here is another way, Hope this helps.
if(isset($_POST['addToCart'])){
$check_list = $_POST['checkbox'];
foreach ($check_list as $checked=>$value) {
//Here you got all checked values in "$checked"
//Eg: to move checked values to array
array_push($pizzaOrdered, $value);
}
}
Try this just use the name of the check box $_POST['checkbox']
This is my code for creating an html form that reads from a database and will allow the user to check and uncheck boxes for each of the 640 items. This is the form.php:
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<html><body> <table cellpadding=10 border=1>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['stickerID']."</td>";
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="<?php echo $row['stickerID'] ?>" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
}
echo "</table></body></html>";
echo " " ?>
<input type="submit" name="editWish" value="Edit">
</form>
<?php " ";
} else {
// no
// print status message
echo "No rows found!";
}
The user must then be able to click on submit and have those values updated in the mysql database.
Right now when I click the submit button, it posts to edit form.php which has this:
<?php
//echo results
foreach($_POST['stickerID'] as $k=>$v ){
echo $k;
echo $v;
}
?>
But I don't get anything echoed. I was thinking the problem could be that Im actually creating a form for every row instead of 1 form with many rows/checkboxes. But when I move the form code after the and the tag to the line where line, I can't even load the form.php, it just loads blank.
Where is my problem? :) Thx
Name your checkbox like this:
<input type="checkbox" name="stickerID[]" value=" <?php echo $row['stickerStatus']; ?> ">
And as Amal already said update your code to PDO or MySQLi
you can do this with a tag :-
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="checkbox[]" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
on your php code you get :-
$all_checkes_checkbox = $_POST['checkbox'];
here is your all checked checkbox:-
and this array also bale key and value
I'm trying to let the user check off which item to be deleted. When the user check off one or many items and click the Delete button, those data will be erased from the database. I've also added a search box to search for the dvd. The search box works, but the deleting doesn't. This is what it looks like in the browser.
My PHP looks like this (I took out the searching code):
<form action="" method="post">
<p><input type="text" name="search"> <input type="submit" value="Search"></p>
<p><input type="submit" name="deleting" value="Delete"></p>
</form>
<?php
$link = mysqli_connect( $host, $user, $password, $dbname);
if (!$link) {
die('Could not connect: ' . mysqli_connect_error());
}
echo 'Connected successfully<br/>';
//searching code goes here
if (isset ($_POST['deleting']) && isset ($_POST['deleteThese']) )
{
$deleteThese = implode(",", $_POST['deleteThese']);
$queryTwo = "DELETE FROM `$dbname`.`dvds` WHERE `dvds`.`DvdID` IN ($deleteThese)";
$resultTwo = mysqli_query($link, $queryTwo);
}
echo "<table border=\"1\"><tr><th>DvdTitle</th><th>RunningTime</th><th>Delete</th></tr>";
if (mysqli_num_rows($result) == 0)
echo "<tr><td colspan='2'>No records found.</td></tr>";
else {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>" . $row['DvdTitle'] . "</td>";
echo "<td>" . $row['RunningTime'] . "</td>";
echo "<td>" . "<form>" . "<input type='checkbox' name='deleteThese[]' value='" . $row['DvdID'] . "' >" . "</form>" . "</td></tr>\n";
}
}
echo "</table>";
mysqli_free_result($result);
mysqli_close($link);
?>
Each DvdTitle has an unique Dvd ID, hence the value of each row is the dvd's ID $row['DvdID'].
Adding the parentheses will allow for those ID's to be selected for deletion.
IN($deleteThese)
EDIT
Do not close the form after the submit button. Put that at the end of the code. This will allow the form to include the checkbox values.
<form action="" method="post">
<p><input type="text" name="search"> <input type="submit" value="Search"></p>
<!-- YOUR PHP CODE -->
<p><input type="submit" name="deleting" value="Delete"></p>
</form>
2nd Edit [requested to improve code]
Move the isset on top of the form.
<?php
if (isset ($_POST['deleting']) && isset ($_POST['deleteThese']) )
{
$deleteThese = implode(",", $_POST['deleteThese']);
$queryTwo = "DELETE FROM `$dbname`.`dvds` WHERE `dvds`.`DvdID` IN ($deleteThese)";
$resultTwo = mysqli_query($link, $queryTwo);
}
?>
<form>....
$deletethese might need to have quotes around it.