Jump to content

Recommended Posts

Hey guys,

I have checkboxes that are generated with elements from the db and I'd like it when the user clicks on submit it stores whatever he checked into a session so it can be used on another page.

here's what I got in my form
[code]
<?php

$pf_time = strtotime("-21 days");
$pf_date = date("Y-m-d", $pf_time);

$result=mysql_query("SELECT * FROM news WHERE date >= " . $pf_date . " ORDER by id") or die(query_error());
$row=mysql_fetch_array($result);
$num_rows = mysql_num_rows($result);

$result2=mysql_query("SELECT * FROM events") or die(query_error());
$row2=mysql_fetch_array($result2);
$num_rows2 = mysql_num_rows($result2);

?>

<div id="article">
<form action="admin.php?a=newnewsletter" method="post" enctype="multipart/form-data" name="form" id="form">
<input type="hidden" name="process" value="yes" />
<input type="hidden" name="size_limit" value="500" />
    <input name="size_limit" type="hidden" id="size_limit" value="500" />
    <table width="603" border="0">
      <tr>
        <td colspan="2">&nbsp;</td>
      </tr>
      <tr>
        <td width="151">Select News :</td>
        <td width="442"><? if ($num_rows < 1) {
echo 'There are no new News';
} else {
for ($i=0;$i<$num_rows;$i++) {
$ID = mysql_result($result,$i,"headline");
$NA= mysql_result($result,$i,"headline");
echo '<input type ="checkbox" name="NA[]" value="'.$ID.'">
'.$ID.'';
}
}?></td>
      </tr>
      <tr>
        <td>Select Events:</td>
        <td><? if ($num_rows < 1) {
echo 'There are no new News';
} else {
for ($i=0;$i<$num_rows2;$i++) {
$ID2 = mysql_result($result2,$i,"name");
$DA= mysql_result($result2,$i,"name");
echo '<input type ="checkbox" name="DA[]" value="'.$ID2.'">
'.$ID2.'';
}
}?></td>
      </tr>
     
      <tr>
        <td>Type:</td>
        <td><select name="type" id="type">
          <option>Choose one</option>
          <option value="house">House</option>
          <option value="hip hop">Hip Hop</option>
          <option value="both">Both</option>
        </select>
        </td>
      </tr>
     
    </table>
<input name="send" type="submit" id="send" value="Submit"/>
</form>
</div>
[/code]

I want all that info to be stored in a session,

here's what I got so far in admin.php

[code]
$process = $_POST['process'];
      if ($process == "yes") {
          $qtmp = array();
          $wtmp = array();
          foreach($_POST['NA'] as $k => $v) {
          $qtmp[] = $v;
          }
           foreach($_POST['DA'] as $c => $b) {
           $wtmp[] = $b;
}
//...
[/code]

that grabs everything that's been checked but how can I reuse that on another page, let's say someone checks news 1, news 2, news 6 I'd like to take that info, query the news table and get all the info for each of those headlines...

how can I do this?
Link to comment
https://forums.phpfreaks.com/topic/14108-checkboxes-and-db-solved/
Share on other sites

no one?

ok I'll simplify things, I need to query a table depending on what a user checks

so let's say I have this in my table

+----------------------------------------+
| id | headline | content | date                |
+----------------------------------------+
| 1 | test1 | blablab | 2006-07-09            |
+----------------------------------------+
| 2 | test2 | blablab2 | 2006-07-09          |
+----------------------------------------+
| 3 | test3 | blablab3 | 2006-07-09          |
+----------------------------------------+

and in the form the user checks test1 and test3

when the form is processed everything that got checked goes in an array like so

[code]
$qtmp = array();
foreach($_POST['NA'] as $k => $v) {
    $qtmp[] = $v;
}
[/code]

now I want to query the databse so that it displays the data of everything in test1 and test3,  Itried doing this but it just gives me an error

[code]
$result=mysql_query("SELECT * FROM ".$prefix."news WHERE headline IN('. implode(',', $qtmp). '") or die(query_error());
while ($row=mysql_fetch_array($result)) {
echo $row['content'];
}
[/code]

what am I doing wrong??
thanks Barand that worked perfectly.

Do you know how I can retrieve the info for each row seperatly? right now if I echo $row['content'] I get blablabblablab3  (test1 and test3 combined together) but I want to be able to echo the headline and content of each news seperatly
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.