Creating Multiple options for checked values.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply

Is ePHP woth upgrading to window 2000?

yes
2
100%
no
0
No votes
 
Total votes: 2

User avatar
brianw84
Forum Newbie
Posts: 18
Joined: Fri Nov 15, 2002 9:01 pm
Location: Jax|Fla

Creating Multiple options for checked values.

Post by brianw84 »

Hello there PHP buddies and Gurus. I have set up a form that gives me results from a table and lists them. I have allso set it up so that it places a check box next to each search result. I have then placed two buttons below the search results "Delete" and "Veiw Details". I am curious as to how to make the buttons interact with each search result that has a checked checkbox next to it. So that when the user checks 1 or 2 or 3 of the results and then clicks on the "delete" button for example, the checked results are deleted from the database. Help on the subject would be greatly appreciated.

Code: Select all

<?php
$linkID = mysql_connect("localhost","","");
mysql_select_db("ymca", $linkID);
if ($search == "") 
{$search = '%';} 
$result = mysql_query ("SELECT * FROM members 
                         WHERE lastname LIKE '%$search%' 
                         ");
  print "<table width=500 valign=top align=center border=1 cellpadding=0 cellspacing=0 bordercolor=#000000><tr><td width=500 height=0 valign=top><form name=search method=post action=search.php>
      <table valign=top width=500 height=157 border=1 cellpadding=0 cellspacing=10 bordercolor=#FFFFFF>
        <tr bordercolor=#FFFFFF><td nowrap bgcolor=#EBEBEB><div align=left><font size=2>Search Members<font face=Times New Roman, Times, serif></font></font></div></td><td height=12 rowspan=4 nowrap bordercolor=#000000> <div align=center><img src=images/mysql.jpg width=167 height=87></div></td></tr><tr bordercolor=#FFFFFF><td width=204 height=53 nowrap><font size=2><font face=Times New Roman, Times, serif><input name=search type=text id=search2 size=29><input name=membersearch type=submit id=membersearch2 value=Search></font></td></tr><tr bordercolor=#FFFFFF>
          <td height=18 align=left valign=top nowrap bgcolor=#EBEBEB><font size=2 face=Verdana, Arial, Helvetica, sans-serif><a href=advsearch.html>Advanced 
            Search</a></font></td>
        </tr><tr bordercolor=#FFFFFF><td height=17 nowrap bgcolor=#EBEBEB><font size=2><a href=mailto:sdiver1500@aol.com>Contact Administrator</a> </font></td></tr></table></form><form name=form1 method=post action=options.php><table width=500 height=0 border=0 cellpadding=0 cellspacing=10><tr bgcolor=#EBEBEB><td width=13 bgcolor=white><font size=2> </font></td><td width=0 nowrap><font size=2>Last Name</font></td><td width=0 nowrap><font size=2>Card Number</font></td><td width=0 nowrap><font size=2>Home Phone Number</font></td></tr>";

if ($row = mysql_fetch_array($result)) { 

do {        print "<tr>";
            print "<td><font size=2>";
            print "<input type=checkbox name=check id=check value=1>";
            print "</font></td>";
            print "<td nowrap><font size=2>";
			print $rowї"lastname"];
			print "</font></td>";
            print "<td nowrap><font size=2>";
			print $rowї"cardnum"];
			print "</font></td>";
            print "<td nowrap><font size=2>";
			print $rowї"homenum"];
			print "</font></td>";

} while($row = mysql_fetch_array($result));
} else {print "Sorry, no records were found!";}
print "<tr> 
            <td colspan=4><div align=right> 
                <input name=add type=submit id=add value=Add>
                <input name=delete type=submit id=delete value=Delete>
                <input name=veiw type=submit id=veiw value=Veiw Details>
              </div></td>
          </tr>
        </table>
      </form> </td></tr>";
print "</table></td>";
?>
sorry its so messy
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I've tried to explain value passing here: viewtopic.php?t=4396 maybe it's helpful
You can assign names to submit-buttons as well. Only the value of the button pressed will be transmitted.
e.g. try

Code: Select all

<html><body>
	<pre><?php print_r($_POST); ?></pre>
	<form method="POST">
		<input type="submit" name="mode" value="delete" />
		<input type="submit" name="mode" value="edit" />
		<input type="submit" name="mode" value="add" />
	</form>
</body></html>
print "<tr>
<td colspan=4><div align=right>
<input name=add type=submit id=add value=Add>
<input name=delete type=submit id=delete value=Delete>
<input name=veiw type=submit id=veiw value=Veiw Details>
</div></td>
</tr>
</table>
</form> </td></tr>";
  • there are three tips I have for you ;)
  • you took care of closing each tag you've opened, excellent :D
    But then close <input> as well. A tag without any content can be closed by the short syntax <tag [properties] />
  • properties should always have quoted values like <input type="submit" .... which leads to the third tip
  • you use double quoted strings for output. Read http://www.php.net/manual/en/language.t ... ng.parsing to see what they're used for. If using single quoted strings you can use " within them freely, e.g.

    Code: Select all

    print 'you may use " here without escaping';
    since you're not using the return value of print you might find echo quit handy as you may pass multiple parameters for output, e.g.

    Code: Select all

    echo '&lt;td width="', $width, '" /&gt;';
    only one function call and no string-concatenation needed.
    When outputting a larger amount of static text/html you may use heredoc-syntax or leave the php-block for a moment. e.g.

    Code: Select all

    &lt;?php
    ...
    if ($row = mysql_fetch_array($result)) { 
    do { ?&gt;
    &lt;tr&gt;
    	&lt;td&gt;&lt;font size="2"&gt;
    		&lt;input type="checkbox" name="check" id="check" value="1"&gt;";
    	&lt;/font&gt;&lt;/td&gt;
    	&lt;td nowrap="true"&gt;&lt;font size="2"&gt;
    	         &lt;?php echo $row&#1111;'lastname']; ?&gt;
    	&lt;/font&gt;&lt;/td&gt;
    	&lt;td nowrap="true"&gt;&lt;font size="2"&gt;
    	        &lt;?php echo $row&#1111;'cardnum']; ?&gt;
    	&lt;/font&gt;&lt;/td&gt;
    	&lt;td nowrap="true"&gt;&lt;font size="2"&gt;
    	         &lt;?php echo $row&#1111;"homenum"]; ?&gt;
    	&lt;/font&gt;&lt;/td&gt;
    &lt;? php
    } while($row = mysql_fetch_array($result));
    ?&gt;
p.s. the board eats my closing php-tags from time to time :cry:
so when the text in a php-block turns to black again there was a ? and > right before and if you quote this post you can see them ;)
Post Reply