Jump to content

Losing my POST values


cyprus

Recommended Posts

I have a series of checkboxes on a form/page. I use the code below which maintains there settings on page re submission.

[code]<?

        $products = array (
        1 => 'Digital Betacam',
        2 => 'Betacam SP',
        3 => 'DVCPro',
        4 => 'HDCAM',
        5 => 'Mini DV'
    );

      foreach ($products as $id =>$prod) {
        if ($_POST['product']) {
            // was value of id in those posted?
            $chk = in_array($id, $_POST['product']) ? 'checked' : '';
        }
        else $chk = '';
?>        [/code]


However, now I am using page pagination, the checkbox's lose there checked/unchecked status when paging. Any idea how I prevent this happening. Many thanks
Link to comment
Share on other sites

[quote author=cyprus link=topic=109587.msg441907#msg441907 date=1159295970]
I have a series of checkboxes on a form/page. I use the code below which maintains there settings on page re submission.

[code]<?

        $products = array (
        1 => 'Digital Betacam',
        2 => 'Betacam SP',
        3 => 'DVCPro',
        4 => 'HDCAM',
        5 => 'Mini DV'
    );

       foreach ($products as $id =>$prod) {
        if ($_POST['product']) {
            // was value of id in those posted?
            $chk = in_array($id, $_POST['product']) ? 'checked' : '';
        }
        else $chk = '';
?>        [/code]


However, now I am using page pagination, the checkbox's lose there checked/unchecked status when paging. Any idea how I prevent this happening. Many thanks
[/quote]

I'm fairly certain that your issue might lie in how your referencing your $_POST items.  $_POST['product'] should be $_POST[$prod], shouldn't it?
Link to comment
Share on other sites

Thanks. However I tried it out and I lose my checkbox settings if I submit the page back to itself. Its all been working fine until I introduced page pagination, and its when I change the pages I lose my checkbox settings.

Don't know if something in here would be the reason?

[code]if ($pagenumber == 1) {
  echo " First Previous ";
} else {
  echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=1'>First</a> ";
  $prevpage = $pagenumber-1;
  echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$prevpage'>Previous</a> ";
}

echo " ( Page $pagenumber of $lastpage ) ";

if ($pagenumber == $lastpage) {
  echo " Next Last ";
} else {
  $nextpage = $pagenumber+1;
  echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$nextpage'>Next</a> ";
  echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$lastpage'>Last</a> ";
}[/code]

Thanks
Link to comment
Share on other sites

Thanks, I will try and explain. The page/form has six checkboxes on representing 6 product types contained within a table containing archived orders. An HTML table displays the contents of the order table. However a user may wish to show only one or selected product types in the table, so they leave checkboxes checked or unchecked, and press submit which resubmits the same form back, the contents of the checkboxes perform a creation of the WHERE statement used in the database query. The status of checkboxes did remain in the conditions they were set, however selecting further pages in pagnitation cause the checkboxes to drop out and the query reverts to all records when page 2 of the table is shown.

Hope you are with me so far.

So when it was just the same page returned there were no problems, but in adding extension pages for long HTML tables I have this problem.

Regards
Link to comment
Share on other sites

Thought I would post the complete code in case it helps. Regards

[code]<?
session_start();
$_SESSION['Dayte']=date("F j, Y, g:i a");
?>

<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Date</title>
</head>
<form action="" method="POST">

<BR>         
<input type="submit" value="Submit Data" name="X1" >
<input type="submit" value="Clear Data" name="X2" >
<input type="submit" value="Exit" name="X3" >
<BR> 

// Offer product selection checkboxes. If page returned from submission then hold values


<div align="left">
<table border="1" width="90%" id="table1" bgcolor="#FFFFCC" bordercolor="#000000" cellspacing="0">
<?

  $products = array (
        1 => 'Digital Betacam',
        2 => 'Betacam SP',
        3 => 'DVCPro',
        4 => 'HDCAM',
        5 => 'Mini DV'
    );

      foreach ($products as $id =>$prod) {
        if ($_POST['product']) {
            // was value of id in those posted?
            $chk = in_array($id, $_POST['product']) ? 'checked' : '';
        }
        else $chk = '';

?>       
<TD>
<?   
echo "<input type='checkbox' name='product[]' value='$id' $chk>$prod<br>";
     
        }
?>
</TD>
</TABLE>
<BR> 


// If page being resubmitted to itself, use posted values to create select WHERE statement

<?

  if (isset($_POST['X1'])) {
    if (count($_POST['product'])) {
    $prodlist = join("','", $_POST['product']);
    $where = "WHERE GGroup IN ('$prodlist')";
    }
    }

if (isset($_GET['pagenumber'])) {
  $pagenumber = $_GET['pagenumber'];
} else {
  $pagenumber = 1;
}

// Connect to database, run query, setup multiple pages for viewing HTML table

$username="root";
// $password="password";
$database ="xxx";

//connect to MySQL
mysql_connect(localhost,$username);
mysql_select_db($database) or die( "Unable to select database"); 

//$query = "SELECT count(*) FROM Orders";
$query = "select Orderdate as Ordered,product as Description,item as Code,Duration,Qty as Quantity,unitprice as Unit,runningtotal as SubTotal from orders $where order by Orderdate,Pindex";

$result = mysql_query($query) or die('Problem with query: ' . $query . '
    '. mysql_error());

$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

$rows_per_page = 10;
$lastpage      = ceil($numrows/$rows_per_page);

$pagenumber = (int)$pagenumber;
if ($pagenumber < 1) {
  $pagenumber = 1;
} elseif ($pagenumber > $lastpage) {
  $pagenumber = $lastpage;
}

$limit = 'LIMIT ' .($pagenumber - 1) * $rows_per_page .',' .$rows_per_page;

//$query = "SELECT * FROM Orders $where $limit";
$query = "select Orderdate as Ordered,product as Description,item as Code,Duration,Qty as Quantity,unitprice as Unit,runningtotal as SubTotal from orders $where order by Orderdate, PIndex $limit";
$result = mysql_query($query) or die('Problem with query: ' . $query . '
    '. mysql_error());


if (($result)||(mysql_errno == 0))

{

echo "<table border='1' bordercolor='#000000' bgcolor='#FFFFCC' cellspacing='0' width='90%'><tr>";

    if (mysql_num_rows($result)>0)
    {
    //loop thru the field names to print the correct headers
          $i = 0;
          while ($i < mysql_num_fields($result))
    {
     
    echo "<td align='center'><b><font face='Arial' size='1'>".    mysql_field_name($result, $i) . "</font></b></td>";
     
    $i++;
    }
    echo "</tr>";
   
    //display the data
   
    while ($rows = mysql_fetch_assoc($result))
   
      {
      echo "<tr>";


foreach ($rows as $k => $data) {
switch ($k) {
case 'Unit':
case 'SubTotal':
$data = '&pound; '.$data;
break;
case 'Quantity':
$data = $data. 'off ';
                                  break;
case 'OrderDate':
$data = date("j-M-Y", strtotime($data));
}
echo "<td align='center'><b><font face='Arial' size='1'>". $data . "</font></b></td>";
    }

    }
  }else{
    echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";

}
echo "</table>";
}else{
  echo "Error in running query :". mysql_error();
}

// PAGINATION BITS

if ($pagenumber == 1) {
  echo " First Previous ";
} else {
  echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=1'>First</a> ";
  $prevpage = $pagenumber-1;
  echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$prevpage'>Previous</a> ";
}

echo " ( Page $pagenumber of $lastpage ) ";

if ($pagenumber == $lastpage) {
  echo " Next Last ";
} else {
  $nextpage = $pagenumber+1;
  echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$nextpage'>Next</a> ";
  echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$lastpage'>Last</a> ";
}

?>
[/code]


Link to comment
Share on other sites

When you first process the check boxes you end with something like "1,3,5" as the selected GGroups.

So where you currently have

echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$nextpage'>Next</a> ";

Add the ggroup data to that

echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$nextpage&where=$where'>Next</a> ";
Link to comment
Share on other sites

I tried the following, but with same results, could very well have misinterpreted suggestion:

[code]if ($pagenumber == 1) {
  echo " First Previous ";
} else {
  echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=1&where=$where'>First</a> ";
  $prevpage = $pagenumber-1;
  echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$prevpage&where=$where'>Previous</a> ";
}

echo " ( Page $pagenumber of $lastpage ) ";

if ($pagenumber == $lastpage) {
  echo " Next Last ";
} else {
  $nextpage = $pagenumber+1;
  echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$nextpage&where=$where'>Next</a> ";
  echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$lastpage&where=$where'>Last</a> ";
}[/code]


Modified all href lines. Thanks again
Link to comment
Share on other sites

Thanks. Yes the WHERE string is being passed to the SELECT query,

This part works okay after first pass after checkboxes set, and on repetitive submissions.

if (isset($_POST['X1'])) {
    if (count($_POST['product'])) {
    $prodlist = join("','", $_POST['product']);
    $where = "WHERE GGroup IN ('$prodlist')";
    }
    }

The WHERE statement gets added in as:

.....from orders [b]$where [/b] order by Orderdate, PIndex $limit";

If I didn't have to extend my page to accomodate longer HTML tables being displayed, then all the code worked fine. I was about to add in To/From order dates to complete the page before I then realised my HTML tables could end up quite long.
Link to comment
Share on other sites

Sitting here, tearing the last strands of hair from my head, I just wonder why I am going down the page pagination route to show a long broken up HTML table when I am submitting the same page back to me. Is there a way to just refil the table each time with ascending/descending query limts, values held in session variables, which simply get counted up/down? Thanks
Link to comment
Share on other sites

I can't see where you pick up the value fron the [b]querystring[/b]

Change
[code]<?php
if (isset($_POST['X1'])) {
    if (count($_POST['product'])) {
        $prodlist = join("','", $_POST['product']);
        $where = "WHERE GGroup IN ('$prodlist')";
    }
}

?>[/code]

to
[code]<?php
if (isset($_GET['where'])) {
    $where = $_GET['where'];
}
elseif (isset($_POST['X1'])) {
    if (count($_POST['product'])) {
        $prodlist = join("','", $_POST['product']);
        $where = "WHERE GGroup IN ('$prodlist')";
    }
}
?> [/code]
Link to comment
Share on other sites

Many thanks for still being in there. I tried the code but it rejected the SQL select statement when trying to goto the next page, it was rejecting the where statement. I know the statement is being built as I have echoed it. Why is life so easy? Thanks again
Link to comment
Share on other sites

I can paste the where statement for the first submission:

WHERE GGroup IN ('1','2')

however on selecting the next page, I cannot get anything other than:

WHERE GGroup IN (Problem with query: select Orderdate
as Ordered,product as Description,item as Code,Duration
,Qty as Quantity,unitprice as Unit,runningtotal as SubTotal
from orders WHERE GGroup IN ( You have an error in your
SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near '' at line 1

Also in moving to page 2 the checkbox drops out of being checked, so maybe that causes the error. If I just keep hitting submit, in other words just keep returning the form/page while on page 1, there are no errors. Thanks
Link to comment
Share on other sites

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.