Jump to content


Photo

Losing my POST values


  • Please log in to reply
18 replies to this topic

#1 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 26 September 2006 - 06:39 PM

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

<?

        $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 = '';
?>       


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

#2 mewhocorrupts

mewhocorrupts
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts
  • LocationParker, CO

Posted 26 September 2006 - 06:54 PM

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

<?

        $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 = '';
?>       


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


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?
-mewhocorrupts

#3 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 26 September 2006 - 07:14 PM

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?

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> ";
}

Thanks

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 26 September 2006 - 07:35 PM

The code you have in your first post is for processing a form, which is normally involving a single record.

You say you are paging which is for process selections involving many records, screenful by screenful.

I'm having difficulty reconciling what's going on that requires both techniques.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 26 September 2006 - 08:00 PM

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


#6 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 26 September 2006 - 08:16 PM

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

<?
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> ";
}

?>




#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 26 September 2006 - 08:40 PM

When you go to the next page you are using the query strings to pass the data to the page so you need to include the groups to be selected in the link HREF data
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 26 September 2006 - 09:33 PM

Many thanks, can you enlighten me a bit more on that as I haven't a clue from here on. Do you mean the select query needs to be passed on, or product groups. !! Thanks

#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 26 September 2006 - 09:43 PM

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> ";
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 26 September 2006 - 10:10 PM

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

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> ";
}


Modified all href lines. Thanks again

#11 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 26 September 2006 - 10:14 PM

Are you picking up the "where" value from the querystring and using it in the query?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#12 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 26 September 2006 - 10:33 PM

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 $where 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.

#13 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 27 September 2006 - 07:06 AM

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

#14 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 27 September 2006 - 07:18 PM

I can't see where you pick up the value fron the querystring

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

?>

to
<?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')";
    }
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#15 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 27 September 2006 - 08:24 PM

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

#16 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 27 September 2006 - 08:27 PM

Can you echo  the rejected sql statement so we can see the code
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#17 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 27 September 2006 - 08:42 PM

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


#18 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 27 September 2006 - 08:51 PM

Do you want to attach a dump of your orders table and I'll load it up at my end so I can test the script?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#19 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 27 September 2006 - 09:20 PM

If its of help then yes that could be done. I exported it to a file called orders.sql (I dont know if that contains the information you want or do you also want the structure export. How do I get it over to you. Many thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users