tags:

views:

781

answers:

4

I have a form with checkboxes, each one has a value. When the registered user select any checkbox the value is incremented (the summation) and then then registred user save his

selection of checkbox if he satisfied with the result of summation into database all this work fine ...i want to enable the registred user to view his selection history by retriving and displaying the checkboxes he selected in a page with thier values ... How I can do that?

I'm just able to save the selected checkboxes as choice 1, choice 2, for example ..

I want to view the selected checkboxes that is saved in database as the appear in the page when the user first select them: for example if the registred user selects these 3 options

  • LEAD DEEP KEEL (1825)
  • FULLY BATTENED MAINSAIL (558)
  • TEAK SIDE DECKS (2889)

They will be saved as for example (choice1, choice2, choice3).

But if he want to view selected checkboxes the appear exactly as first he selects them:

  • LEAD DEEP KEEL (1825)
  • FULLY BATTENED MAINSAIL (558)
  • TEAK SIDE DECKS (2889)

This is my user table:

$query="CREATE TABLE User( 
user_id      varchar(20), 
password     varchar(40),
user_type    varchar(20),
firstname    varchar(30),
lastname     varchar(30),
street   varchar(50),
city         varchar(50),
county   varchar(50),
post_code    varchar(10),
country      varchar(50),
gender   varchar(6),
dob      varchar(15),
tel_no   varchar(50),
vals         varchar(50),
email        varchar(50))";

and the code to inser the options selected to database

<?php

include("databaseconnection.php");

$str = '';

foreach($_POST as $key => $val)

if (strpos($key,'choice') !== false)
 $str .= $key.',';


$query = "INSERT INTO User (vals) VALUES('$str')";
$result=mysql_query($query,$conn);
if ($result)
{
     (mysql_error();
}
else
{  
echo " done";      
}

?>

And this is my form:

function checkTotal() { document.listForm.total.value = ''; var sum = 0; for (i=0;i

<form name="listForm" method="post" action="insert_options.php" >
<TABLE cellPadding=3 width=600 border=0>
        <TBODY>

        <TR>
          <TH align=left width="87%" bgColor=#b0b3b4><SPAN 
            class=whiteText>Item</SPAN></TH>
          <TH align=right width="13%" bgColor=#b0b3b4><SPAN 
            class=whiteText>Select</SPAN></TH></TR>
        <TR>
          <TD  bgcolor="#9da8af"colSpan=2><SPAN class=normalText><B>General</B></SPAN></TD></TR>
        <TR>
          <TD  bgcolor="#c4c8ca"><SPAN class=normalText >TEAK SIDE DECKS (2889)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="2889" type="checkbox" onchange="checkTotal()" /></TD></TR>
        <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>LEAD DEEP KEEL (1825)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="1825" type="checkbox" onchange="checkTotal()"></TD></TR>

        <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>FULLY BATTENED MAINSAIL (558)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="558"  type="checkbox"  onchange="checkTotal()"></TD></TR>

        <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>HIGH TECH SAILS FOR CONVENTIONAL RIG (1979)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="1979" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>IN MAST REEFING WITH HIGH TECH SAILS (2539)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="2539" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>SPlNNAKER GEAR (POLE LINES DECK FITTINGS) (820)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="820" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>SPINNAKER POLE VERTICAL STOWAGE SYSTEM (214)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="214" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>GAS ROD KICKER (208)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="208" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>SIDE RAIL OPENINGS (BOTH SIDES) (392)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="392" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>SPRING CLEATS MIDSHIPS -ALUMIMIUM (148)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="148" type="checkbox" onchange="checkTotal()"></TD></TR>


          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>ELECTRIC ANCHOR WINDLASS (1189)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="1189" type="checkbox" onchange="checkTotal()"> </TD></TR>




          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>ANCHOR CHAIN GALVANISED (50m) (202)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="202" type="checkbox" onchange="checkTotal()"> </TD></TR>



          <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>ANCHOR CHAIN GALVANISED (50m) (1141)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="1141" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TR>
          <TD  bgcolor="#9da8af"colSpan=2><SPAN class=normalText><B>NAVIGATION & ELECTRONICS</B></SPAN></TD></TR>
        <TR>
          <TD  bgcolor="#c4c8ca"><SPAN class=normalText >WIND VANE (STAINLESS STEEL)(41)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="41" type="checkbox" onchange="checkTotal()" /></TD></TR>
        <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>RAYMARINE ST6O LOG & DEPTH (SEPARATE UNITS)(226)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="226" type="checkbox" onchange="checkTotal()"></TD></TR>

      <TR>
          <TD  bgcolor="#9da8af"colSpan=2><SPAN class=normalText><B>ENGINES & ELECTRICS</B></SPAN></TD></TR>
        <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>SHORE SUPPLY (220V) WITH 3 OUTLETS (EXCLUDJNG SHORE CABLE) (327)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="327"  type="checkbox"  onchange="checkTotal()"></TD></TR>

        <TR>
          <TD bgColor=#c4c8ca><SPAN class=normalText>3rd BATTERY(14OA/H)(196)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="196" type="checkbox" onchange="checkTotal()"></TD></TR>

         <TD bgColor=#c4c8ca><SPAN class=normalText>24 AMP BATTERY CHARGER (475)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="475" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TD bgColor=#c4c8ca><SPAN class=normalText>2 BLADED FOLDING PROPELLER (UPGRADE)(299)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="299" type="checkbox" onchange="checkTotal()"></TD></TR>


         <TR>
          <TD  bgcolor="#9da8af"colSpan=2><SPAN class=normalText><B>BELOW DECKS/DOMESTIC</B></SPAN></TD></TR>

          <TD bgColor=#c4c8ca><SPAN class=normalText>WARM WATER (FROM ENGINE & 220V)(749)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="749" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TD bgColor=#c4c8ca><SPAN class=normalText>SHOWER IN AFT HEADS WITH PUMPOUT(446)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="446" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TD bgColor=#c4c8ca><SPAN class=normalText>DECK SUCTION DISPOSAL FOR HOLDINGTANK(166)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="166" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TD bgColor=#c4c8ca><SPAN class=normalText>REFRIGERATED COOLBOX (12V)(666)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="666" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TD bgColor=#c4c8ca><SPAN class=normalText>LFS SAFETY PACKAGE (COCKPIT HARNESS POINTS STAINLESS STEEL JACKSTAYS)(208)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="208" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TD bgColor=#c4c8ca><SPAN class=normalText>UPHOLSTERY UPGRADE IN SALOON (SUEDETYPE)(701)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="701" type="checkbox" onchange="checkTotal()"></TD></TR>

          <TR>
          <TD  bgcolor="#9da8af"colSpan=2><SPAN class=normalText><B>NAVIGATION ELECTRONICS & ELECTRICS</B></SPAN></TD></TR>

          <TD bgColor=#c4c8ca><SPAN class=normalText>VHF RADIO AERIAL CABLED TO NAVIGATION AREA(178)</SPAN></TD>
          <TD align=right bgColor=#c4c8ca><input  name="choice" value="178" type="checkbox" onchange="checkTotal()"></TD></TR>
</table>
A: 

To save the settings into the database you have to use UPDATE, not INSERT. First you must know the user_id. Then you'll have to issue a query like:

$query = "update User set val = '" . mysql_real_escape_string($str)
       . "' where user_id = $user_id";

You can use PHP's explode function to get an array of values out of your string.

$values = explode(',', $val);
kmkaplan
+2  A: 

In the html form, instead of having

<input type="checkbox" name="choice" value="123">

Do:

<input type="checkbox" name="choice[]" value="123">

This will make the choices available as an array to you.

Then change your php code for inserting the choices into the databse to this:

<?php

$str='';
foreach ($_POST['checked'] as $val)
{
    $str.=$val.',';
}

$str=substr($str,0,-1);//Remove the final comma from the end

$sql="UPDATE users SET vals='$str' WHERE user_id='$user_id'";
mysql_query($sql) or die(mysql_error());

?>

Then when displaying the choices back to the user, do something like this:

Php:

<?php

$checked=array();
$sql="SELECT vals FROM users WHERE user_id='$user_id'";
$result=mysql_query($sql) or die(mysql_error());

$row=mysql_fetch_assoc($row);
$vals=explode(',',$row['vals']);


foreach ($vals as $val)
{
    if ($val !='' && is_numeric($val))
     $checked[$val]='checked';
}

function set_checked($value)
{
    global $checked;
    if (isset($checked[$value]))
     return 'checked';
    else
     return '';
}

?>

In the html:

<input type="checkbox" name="choice[]" value="123" <? echo set_checked('123');?>>

P.S if it helps, please don't forget to upvote / mark as correct :)

P.P.s make sure you replace $user_id with the user id of the user or another way to identify him in the queries.

Click Upvote
A: 

A few questions before I can really write the answer:

  1. Are those checkbox values unique. e.g LEAD DEEP KEEL is the only checkbox that will ever have the value 1825?

    2.Do you want to save the sum of the values? e.g. 701 + 1802 + 211 as well?

  2. Will you have associated tables. e.g. will you need to be able to independently select user rows based on their choice?

stuartloxton
A: 

hi stuartloxton

yes these values are unique,, i dont want to save the sum of values they are just for displaying to user,, i have a table contains user register info and conatains also a field that i save the selected checkboxes in

does my code help?
Click Upvote