| | |
checkboxes problem
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
hi
try this example:
suppose you want following to be done:
[]record1
[]record2
here [] is check box
then do following to get above:
1.suppose you have two variable $rec1 and $rec2 which contains data to be printed along with check box,
$rec1="record1" ;
$rec2="record2";
2.prepare a $rspString like following:
$rspString="<form>"+$rec1+"<input type='checkbox' name='some_name' value="+$rec1+">
<br>"+$rec2+"<input type='checkbox' name='some_name2' value="+$rec2+">
</form>";
print this.
it will create check box with required variable value.
this is just a example. you can follow this example to generate required string for you.
try this example:
suppose you want following to be done:
[]record1
[]record2
here [] is check box
then do following to get above:
1.suppose you have two variable $rec1 and $rec2 which contains data to be printed along with check box,
$rec1="record1" ;
$rec2="record2";
2.prepare a $rspString like following:
$rspString="<form>"+$rec1+"<input type='checkbox' name='some_name' value="+$rec1+">
<br>"+$rec2+"<input type='checkbox' name='some_name2' value="+$rec2+">
</form>";
print this.
it will create check box with required variable value.
this is just a example. you can follow this example to generate required string for you.
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Indian Developer
http://falaque.wordpress.com/
Hi,
Even though it is a fairly advanced script you could try looking at this post:
http://www.daniweb.com/forums/thread111225.html
or you can do a search on checkboxes and find a few additional examples
Even though it is a fairly advanced script you could try looking at this post:
http://www.daniweb.com/forums/thread111225.html
or you can do a search on checkboxes and find a few additional examples
I keep hitting "escape", but I'm still here!!!!
:}
:}
just assign a checkbox with a different name to each one with id value as the checkbox value. then use the foreach() statement to loop through the post results and regex the key to make sure it belongs to that set of input elements. after that take the value from the foreach statement and use it in a query to delete the record.
i know that sounds complicated and you probably don't understand it. if you need some example code let me know.
i know that sounds complicated and you probably don't understand it. if you need some example code let me know.
Last edited by kkeith29; Mar 7th, 2008 at 8:38 pm.
•
•
Join Date: Feb 2008
Posts: 90
Reputation:
Solved Threads: 2
Btw this is how i do it right now. im just using a text link to delete the records.
PHP Syntax (Toggle Plain Text)
$action=$_REQUEST['action']; $id=$_REQUEST['id']; if ($action==1){ $link=mysql_connect($hostname,$username,$password); $dbname=mysql_select_db($database); echo "<table width=100% cellspacing=0>"; echo "<tr><td bgcolor=darkblue><center><font face=verdana size=4 color=white>GV Local Site using PHP and MySQL</font></tr>"; $query = "SELECT * FROM employee"; echo "<table width=100% cellspacing=0 border=1><tr><td><center><B>ID<td><center><B>First<td><center><B>Last Name<td><center><B>Address<td><center><B>Delete Entry</tr>"; $result = mysql_query($query) or die ("Query failed: " . mysql_error() . "Actual query: " . $query); //$result = mysql_query("select * from mytable"); //while ($row = mysql_fetch_object($result)) { while ($row = mysql_fetch_object($result)){ echo "<tr><td>".$row->id; echo "<td>".$row->first; echo "<td>".$row->last; echo "<td>".$row->address; echo "<td><a href='ayan.php?action=2&id=$row->id'>Delete Record</a></tr>"; } echo "</table><br><br><center><font face=arial size=4 color=white><b><a href='index.php'>Go back to Main</a></b></font>"; }
here is the code. i am not for sure if it will work properly with your setup because i don't know the rest of the code, but i have used a similar system in many of my admin centers i have created and it works nicely.
let me know if there are any problems. i will help you fix them.
PHP Syntax (Toggle Plain Text)
<?php $action = $_GET['action']; if (!empty($action)) { $con = mysql_connect($hostname,$username,$password); $db_con = mysql_select_db($database); switch($action) { case 1: $sql = "SELECT * FROM `employee`"; $query = mysql_query($sql,$con) or die('Error: ' . mysql_error()); $tr_data = ''; $i = 0; while ($row = mysql_fetch_assoc($query)) { $tr_data .= '<tr>'; $tr_data .= '<td>' . $row['id'] . '</td>'; $tr_data .= '<td>' . $row['first'] . '</td>'; $tr_data .= '<td>' . $row['last'] . '</td>'; $tr_data .= '<td>' . $row['address'] . '</td>'; $tr_data .= '<td><input type="checkbox" name="delete' . $i . '" value="' . $row['id'] . '" /></td>'; $tr_data .= '</tr>'; $i++; } $html =<<<HTML <table width="100%" cellspacing="0"> <tr> <td bgcolor="#0000FF" align="center"> <font face="verdana" size="4" color="#FFFFFF">GV Local Site using PHP and MySQL</font> </td> </tr> </table> <form action="ayan.php?action=2" method="post"> <table width="100%" cellspacing="0" cellpadding="3" border="1"> <tr> <th>ID</th> <th>First</th> <th>Last Name</th> <th>Address</th> <th>Delete</th> </tr> $tr_data </table> </form> <br /> <br /> <center> <font face="arial" size="4" color="#FFFFFF"><strong><a href="index.php">Go back to Main</a></strong></font> </center> HTML; break; case 2: foreach($_POST as $key => $value) { if (preg_match("/^delete+[0-9]$/",$key)) { $sql = "DELETE FROM `employee` WHERE `id` = '" . mysql_real_escape_string($value) . "'"; $query = mysql_query($sql,$con) or die('Error: ' . mysql_error()); } } $html = 'Employees Deleted Successfully'; break; } } echo $html; ?>
let me know if there are any problems. i will help you fix them.
Last edited by kkeith29; Mar 8th, 2008 at 12:00 am.
what i did with the $tr_data is store the html into a variable instead of echoing like you had. this makes it so i can use the header() function to redirect, sorry its a force of habit. i can change if you want. the foreach() block starts off by reading the post data sent from the form where you checked the checkboxes. "foreach" of the array values it checks if the key (name element in html) is valid. if it is then it deletes the employee otherwise it skips it.
clear enough?
clear enough?
![]() |
Similar Threads
- How to get value of selected dynamic checkboxes? (ASP.NET)
- Count number of checkboxes being checked in asp.net (ASP)
- problem in passing multiple checkbox values (ASP)
- javascript radio buttons and insanity (JavaScript / DHTML / AJAX)
- Help with PHP and checkboxes (PHP)
- somebody help me! (JSP)
- checkboxes on continuous forms (Visual Basic 4 / 5 / 6)
- LOW RESOURCES problem (Windows 95 / 98 / Me)
- Problem Unsetting Variables? (PHP)
Other Threads in the PHP Forum
- Previous Thread: Trouble with Array Delete in PHP , MySQL
- Next Thread: Variable refering to a Variable Name
| Thread Tools | Search this Thread |
.htaccess ajax apache api array beginner beneath binary broadband broken button cakephp checkbox class cms code countingeverycharactersfromastring crack cron curl database date decode display dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail match menu mlm mod_rewrite multiple mysql oop paypal pdf php problem protocol query radio random recursion regex remote script search server sessions sms smtp soap source space sql strip_tags survey syntax system table tutorial undefined update upload url validation validator variable video virus votedown web window.onbeforeunload=closeme; xml youtube






