close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Checkbox group insert and update values in one field

Thread began 8/09/2010 9:49 pm by Roland Rogers | Last modified 8/20/2010 7:17 am by Jason Byrnes | 11227 views | 19 replies |

Roland Rogers

Checkbox group insert and update values in one field

Hi,

This seems so basic but is not working. After I ran the DA wizard I updated a field to be a check box group of days of the week. I need to be able to select from one to seven of the boxes and have those values stored in that field. Dreamweaver sets up the group to have the days like this:

<label><input type="checkbox" name="Availability" value="Monday" id="Availability_0" />
Monday</label>
<br />
<label><input type="checkbox" name="Availability" value="Tuesday" id="Availability_1" />
Tuesday</label>...

and so on.

When I select multiple days the field only gets the last day selected.

What am I missing here? Is it a DW or DA tweak I need to make?

Thanks for any advice or guidance!

Sign in to reply to this post

Jason ByrnesWebAssist

when using a multi checkbox in php, the name of the checkbox must end in "[]", for example:

php:
<input type="checkbox" name="Availability[]" value="Monday" id="Availability_0" />




this will create an array of the multiple selections. when you add the binding in the insert or update record server behavior, code will look like:

php:
<?php echo((isset($_POST["Availability[]"]))?$_POST["Availability[]"]:""?>



you will need to edit this code to use the php implode function to convert the array to a coma separated list

php:
<?php echo ((isset($_POST["Availability"]))?implode(", "$_POST["Availability"]):""); ?>



On the update page, you will need to make a few modifications to preselect the correct checkboxes.

You will need to use the php explode and in_array functions:

php:
<input type="checkbox" name="Availability[]" value="Monday" id="Availability_0" <?php echo((in_array("Monday"explode(", "$row_RecordsetName['ColumnName'])))?"checked=checked":""); ?>/>
Sign in to reply to this post

Roland Rogers

Brilliant!! This worked a charm! I spent hours learning about arrays on the web but your four step process made it easy to understand and helped me by telling exactly where to put the bits of code.

Not only does it work, it all makes sense now! I am so glad you are back in the forums!! You and the other Web Assist staff make this such a great resource, in combination with your products!

One follow up question.

When going through the Data Assist wizard is there a step to go through to have the wizard set up a checkbox group in one field, like this? Or should it be done manually each time it is needed?

Thanks again!

Sign in to reply to this post

Jason ByrnesWebAssist

That's great, I'm glad it helped to make sense out arrays as well.

DataAssist will nor create the multi check box for you, this will need to be done manually after the page is created by the DataAssist wizard.

Sign in to reply to this post

Roland Rogers

Now I am always pushing things further! I have the Insert and Update pages all set.

To update the Search page...
- I added the checkbox group and array to the search page,
- I added the "addComparisonFromEdit" line on the Results page for my Availability field

but where would the implode action take place? The search form processes on the results page, is that where the implode action takes place?

I added a hidden field at the end of the check box group and called it S_Availability - but would need some sort of trigger to get the comma list from the array. $_POST won't cut it here. Is there another trick I can use to get the value of the hidden field to pass to the Rersults page for the search?

Sign in to reply to this post

Jason ByrnesWebAssist

you would need to set it up as a keyword search. and use the imploded array as the value:
<?php echo ((isset($_POST["Availability"]))?implode(", ", $_POST["Availability"]):""); ?>

Sign in to reply to this post

johnb

Sooo close!!! Please help

After 8 hours of trial and error, I saw this post and got soooo excited because it seemed to be exactly what I need but I cannot seem to get it to work. The difference in mine might be that I am loading the checkboxes dynamically form another table. I have gotten that part to work fine. I am wondering if you could shed some light on why this wont work for me based on my code?

Here is the dynamically loaded code which places the checkboxes on the page:

<?php require_once("Connections/Polk.php");

$sql = "SELECT * FROM agents";
$pResult=mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($pResult) == 0)
{
print "There are No Records";
}
else
{
while($tmp = mysql_fetch_assoc($pResult))
{
echo "<input type=\"checkbox\" name=\"agent_id[]\" value=\"".$tmp["ID"]."\" />"; echo $tmp['name'];
echo "<br />";
}
}
?>





Here is the insert code that was generated by Data Assist:

// WA Application Builder Insert
if (isset($_POST["Insert_x"])) // Trigger
{
$WA_connection = $Polk;
$WA_table = "test_table";
$WA_sessionName = "WADA_Insert_test_table";
$WA_redirectURL = "test_table_Detail.php";
$WA_keepQueryString = false;
$WA_indexField = "ID";
$WA_fieldNamesStr = "agent_id";
$WA_fieldValuesStr = "".((isset($_POST["agent_id"]))?$_POST["agent_id"]:"") ."";
$WA_columnTypesStr = "none,none,NULL";
$WA_fieldNames = explode("|", $WA_fieldNamesStr);
$WA_fieldValues = explode("|", $WA_fieldValuesStr);
$WA_columns = explode("|", $WA_columnTypesStr);
$WA_connectionDB = $database_Polk;
mysql_select_db($WA_connectionDB, $WA_connection);
if (!session_id()) session_start();
$insertParamsObj = WA_AB_generateInsertParams($WA_fieldNames, $WA_columns, $WA_fieldValues, -1);
$WA_Sql = "INSERT INTO `" . $WA_table . "` (" . $insertParamsObj->WA_tableValues . ") VALUES (" . $insertParamsObj->WA_dbValues . ")";
$MM_editCmd = mysql_query($WA_Sql, $WA_connection) or die(mysql_error());
$_SESSION[$WA_sessionName] = mysql_insert_id();
if ($WA_redirectURL != "") {
if ($WA_keepQueryString && $WA_redirectURL != "" && isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !== "" && sizeof($_POST) > 0) {
$WA_redirectURL .= ((strpos($WA_redirectURL, '?') === false)?"?":"&").$_SERVER["QUERY_STRING"];
}
header("Location: ".$WA_redirectURL);
}
}
?>




It seems like I would need to edit this line as you suggested with the implode string but when I do that it just sends a 0 to the database.


$WA_fieldValuesStr = "".((isset($_POST["agent_id"]))?$_POST["agent_id"]:"")  ."";
Sign in to reply to this post

Jason ByrnesWebAssist

yes, this line:

php:
$WA_fieldValuesStr = "".((isset($_POST["agent_id"]))?$_POST["agent_id"]:"")  ."";




should be edited to:

php:
$WA_fieldValuesStr = "".((isset($_POST["agent_id"]))?implode(", ",$_POST["agent_id"]):"")  ."";




if it is still not working, make sure that the values are being set on the checkboxes. Before submitting the page, view the page source and examine the source of the checkboxes to make sure they have a proper value.


can you send back the source of the page from the browser.

Sign in to reply to this post

johnb

I did think that's what I was supposed to do from your initial suggestion in this thread and had tried adding:

php:
$WA_fieldValuesStr = "".((isset($_POST["agent_id"]))?implode(", ",$_POST["agent_id"]):"")  ."";



at no avail.

Here is the source of the page which does seem to be showing the values set:

php:
<body>

<div class="WADAInsertContainer">
  <form  action="test_table_Insert.php" method="post" name="WADAInsertForm" id="WADAInsertForm">
    <div class="WADAHeader">Insert Record</div>
    <div class="WADAHorizLine"><img  src="WA_DataAssist/images/_tx_.gif" alt="" height="1" width="1" border="0" /></div>
    <table class="WADADataTable" cellpadding="0" cellspacing="0" border="0">
      <tr>
        <th class="WADADataTableHeader">ID:</th>
        <td class="WADADataTableCell"></td>
      </tr>
      <tr>
        <th class="WADADataTableHeader">agent_id:</th>
        <td class="WADADataTableCell">
<!--        <input type="checkbox" name="agent_id" id="agent_id" value="1" />           -->
<input type="checkbox" name="agent_id[]" value="1" />John Brandenburg<br /><input type="checkbox" name="agent_id[]" value="2" />Christine Marshall<br /><input type="checkbox" name="agent_id[]" value="3" />John Brandenburg<br /><input type="checkbox" name="agent_id[]" value="4" />christine Marshall<br /></td>
      </tr>
    </table>
    <div class="WADAHorizLine"><img  src="WA_DataAssist/images/_tx_.gif" alt="" height="1" width="1" border="0" /></div>
    <div class="WADAButtonRow">
      <table class="WADADataNavButtons" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td class="WADADataNavButtonCell" nowrap="nowrap"><input type="image" name="Insert" id="Insert" value="Insert" alt="Insert"  src="WA_DataAssist/images/Pacifica/Refined_insert.gif"  /></td>
          <td class="WADADataNavButtonCell" nowrap="nowrap"><a href="test_table_Results.php" title="Cancel"><img border="0" name="Cancel" id="Cancel" alt="Cancel"  src="WA_DataAssist/images/Pacifica/Refined_cancel.gif" /></a></td>
        </tr>
      </table>
<input name="WADAInsertRecordID" type="hidden" id="WADAInsertRecordID" value="" />
    </div>
  </form>
</div>
</body>



It feels so close! I really appreciate your response so quickly to my post.

Sign in to reply to this post

Jason ByrnesWebAssist

that looks correct, the agent_id form elements all have valid values.


Try changing:

php:
$MM_editCmd = mysql_query($WA_Sql, $WA_connection) or die(mysql_error());




to:

php:
echo("<pre>");

var_dump($_POST);
echo("</pre>");
die("sql: ".$WA_Sql);
$MM_editCmd = mysql_query($WA_Sql, $WA_connection) or die(mysql_error());




and post back the results, this will help me troubleshoot further.

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...