473,837 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

My $_POST variable works only for the first page.

3 New Member
I have written a form in with radio buttons the name is set to orderby and the value is set to KundeVorName and the next value is KundeNachName and it goes so on. I wanna modify my query according this values. It works well for the first page but at the second it looses its value and gives me an error message. When i hard code it like "Select * from Kunde ORDER BY KundeVorName" it works for all pages when i use my variable $orderby it works just for the first page. here is my code:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <style type="text/css">
  4. A:link {text-decoration: none}
  5. A:visited {text-decoration: none}
  6. A:active {text-decoration: none}
  7. A:hover {text-decoration: underline overline; color: red;}
  8. table.second { margin-top:20px; margin-bottom:0px;}
  9. </style>
  10. <title>Implementing Paging with next and prev</title>
  11. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  12. </head>
  13.  
  14. <body bgcolor="#99CC99">
  15.  
  16. <table width="60%" align="center" bgcolor="#009966"><tr>
  17. <td><a href="insert.php">Veri Gir</a></td>
  18. <td><a href="query.php">Veri Görüntüle</a></td>
  19. <td><a href="search.php"> Veri Ara </a></td>
  20. <td><a href="update.php"> Veri Güncelle </a></td>
  21. <td><a href="logout.php"> Ç?k?? </a></td>
  22. </tr></table>
  23. <?php
  24. include 'connect.php';
  25. $orderby = $_POST['orderby'];
  26. $type = $_POST['type'];
  27.  
  28.     if (isset($orderby)) {
  29.         echo "orderby is set\n";
  30.     } else {
  31.         echo "orderby is not set\n";
  32.     }
  33.     if (isset($type)) {
  34.         echo "type is set\n";
  35.     } else {
  36.         echo "type is not set\n";
  37.     }
  38.  
  39. // how many rows to show per page
  40. $rowsPerPage = 5;
  41.  
  42. // by default we show first page
  43. $pageNum = 1;
  44.  
  45. // if $_GET['page'] defined, use it as page number
  46. if(isset($_GET['page']))
  47. {
  48.     $pageNum = $_GET['page'];
  49. }
  50.  
  51. // counting the offset
  52. $offset = ($pageNum - 1) * $rowsPerPage;
  53.  
  54.  
  55. $query = " SELECT * FROM Kunde ORDER BY $orderby" .
  56.          " LIMIT $offset, $rowsPerPage";
  57. $result = mysql_query($query) or die('Error, query failed');
  58.  
  59. // start of the query *******************************************************************************
  60. ?>
  61. <div><table class='second' border='1'><tr><td>ID</td><td>?sim Soyad</td><td>Firma</td><td>S?n?f</td><td>Ürünler</td><td>Adres</td><td>?ehir</td><td>Ülke</td><td>Türü</td><td>?? Telefonu</td><td>Fax</td><td>Cep</td><td>Mail</td><td>Web</td><td>Tarih</td></b></tr>
  62. <?php
  63. while($row = mysql_fetch_array($result))
  64. {
  65. ?>
  66. <tr><td><?php echo $row['KundeID']; ?> </td>
  67. <td> <?php echo $row['KundeVorName']."&nbsp;".$row['KundeNachName']; ?> </td>
  68. <td> <?php echo $row['KundeFirma']; ?> </td>
  69. <td> <?php echo $row['KundeKategorie']; ?> </td>
  70. <td> <?php echo $row['KundeProdukte']; ?> </td>
  71. <td> <?php echo $row['KundeAdresse']; ?> </td>
  72. <td> <?php echo $row['KundeOrt']; ?> </td>
  73. <td> <?php echo $row['KundeLand']; ?> </td>
  74. <td> <?php echo $row['KundeArt']; ?> </td>
  75. <td> <?php echo $row['KundeFestnetz']."&nbsp;"; ?> </td>
  76. <td> <?php echo $row['KundeFax']."&nbsp;"; ?> </td>
  77. <td> <?php echo $row['KundeMobile']."&nbsp;"; ?> </td>
  78. <td> <?php $mail = $row['KundeMail']; ?><a href="mailto:<?php echo "$mail"; ?>"><?php echo $row['KundeMail']."&nbsp;"; ?> </a></td>
  79. <td> <?php $web = $row['KundeWeb']; ?> <a href="http://<?php echo "$web"; ?>" Target="blank"> <?php echo $row['KundeWeb']."&nbsp;"; ?></a> </td>
  80. <td> <?php echo $row['KundeRegDatum']."&nbsp;"; ?> </td></tr>
  81. <?php } ?>
  82. </table>
  83. <?php
  84. // end of the query *******************************************************************************
  85. // how many rows we have in database
  86. $query   = "SELECT COUNT(KundeID) AS numrows FROM Kunde ";
  87. $result  = mysql_query($query) or die('Error, query failed');
  88. $row     = mysql_fetch_array($result, MYSQL_ASSOC);
  89. $numrows = $row['numrows'];
  90.  
  91. // how many pages we have when using paging?
  92. $maxPage = ceil($numrows/$rowsPerPage);
  93.  
  94. // print the link to access each page
  95. $self = $_SERVER['PHP_SELF'];
  96. $nav = '';
  97. for($page = 1; $page <= $maxPage; $page++)
  98. {
  99.     if ($page == $pageNum)
  100.     {
  101.         $nav .= " $page ";   // no need to create a link to current page
  102.     }
  103.     else
  104.     {
  105.         $nav .= " <a href=\"$self?page=$page\">$page</a> ";
  106.     }        
  107. }
  108.  
  109. // creating previous and next link
  110. // plus the link to go straight to
  111. // the first and last page
  112.  
  113. if ($pageNum > 1)
  114. {
  115.     $page = $pageNum - 1;
  116.     $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
  117.  
  118.     $first = " <a href=\"$self?page=1\">[First Page]</a> ";
  119. else
  120. {
  121.     $prev  = '&nbsp;'; // we're on page one, don't print previous link
  122.     $first = '&nbsp;'; // nor the first page link
  123. }
  124.  
  125. if ($pageNum < $maxPage)
  126. {
  127.     $page = $pageNum + 1;
  128.     $next = " <a href=\"$self?page=$page\">[Next]</a> ";
  129.  
  130.     $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
  131. else
  132. {
  133.     $next = '&nbsp;'; // we're on the last page, don't print next link
  134.     $last = '&nbsp;'; // nor the last page link
  135. }
  136.  
  137. // print the navigation link
  138. echo $first . $prev . $nav . $next . $last;
  139.  
  140. // and close the database connection
  141.  
  142. ?>
  143. </body>
  144. </html>
  145.  
[Please use CODE tags when posting source code. Thanks! --pbmods]
I get orderby is not set ,type is not set and query error messages.
How can I fix my problem with the variables?
May 30 '07 #1
3 2347
pbmods
5,821 Recognized Expert Expert
Heya, Mesut. Welcome to TSDN!

Um... that's a lot of code you just dumped there. And most of it really isn't relevant to your problem at all.

You might want to save your POST variables in the session. Take a look at this thread.
May 30 '07 #2
Mesut
3 New Member
Heya, Mesut. Welcome to TSDN!

Um... that's a lot of code you just dumped there. And most of it really isn't relevant to your problem at all.

You might want to save your POST variables in the session. Take a look at this thread.

I am really a newbie, i did not get the answer i have modified most of the scripts now i a m stucked up, i dont know how to modify
Expand|Select|Wrap|Line Numbers
  1. foreach($_POST as $k => $v)
  2. {
  3. $_SESSION[$k] = $v;
  4. }
  5.  
[Please use CODE tags when posting source code. Thanks! --pbmods]
variable...??
Anyway thats an amazing fast answer, you are great!!
May 30 '07 #3
pbmods
5,821 Recognized Expert Expert
I am really a newbie, i did not get the answer
We've all been there. No problem ^_^

First off, you need to make one change:
Expand|Select|Wrap|Line Numbers
  1. //  Add this line, or else nothing will work properly:
  2. session_start();
  3.  
  4. foreach($_POST as $k => $v)
  5. {
  6. $_SESSION[$k] = $v;
  7. }
  8.  
Alrightey. What this code does is run through $_POST and copy the values in $_POST to $_SESSION.

To be more verbose, for each element in $_POST, we assign the key (e.g., 'orderby') to $k and the value (e.g., 'KundeVorName') to $v. Then we set $_SESION[$k] to $v ($_SESSION['orderby'] = 'KundeVorName') .

The end result is that anything that was in $_POST is now in $_SESSION, and $_SESSION (unlike $_POST) is persistent between page loads.

Put that script by where you assign the value of $orderby and then change your statement from
Expand|Select|Wrap|Line Numbers
  1. $orderby = $_POST['orderby'];
  2.  
to

Expand|Select|Wrap|Line Numbers
  1. $orderby = $_SESSION['orderby'];
  2.  
May 30 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
11882
by: Dan | last post by:
I was trying to troubleshoot a login page that doesn't work - it keeps saying the login/password is missing - when my tracing discovered this peculiar behavior. register_globals is off, so at the top of my script I assign a few variables to incoming GET and POST values. $login = clean($_POST, 30); $passwd = clean($_POST, 30);
15
7156
by: zorro | last post by:
greetings... I'm wondering what more advanced coders would think ot this: $_POST = clean($_POST); and now I can use POST directly: $sql= "select * from T1 where myvar='$_POST' " ;
8
2170
by: Simon Rigby | last post by:
Hello folks, Ive seen lots of recommendations to not try to do what Im trying to do but let me put it this way and hopefully someone can tell if this is a good idea or not (or an alternative). What Im trying to do is this. I have a series of links that when clicked will navigate to a common page passing in a value encoded in the link. For reasons not worth going into I cant use a querystring.
12
2672
by: Todd Michels | last post by:
Hi all, I am trying to send data from a form and insert it into a MSSQL DB. When I submit the data I get: Warning: mssql_query() : message: The name "Todd" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted. (severity 15) in "Myfile"
9
1976
by: shreedhan | last post by:
Hi I'm recenlty studying PHP myself and I have got a problem in passing variables to one php script from another i have following code as index.php <?php $name=HREEDN ?> <html> <head>
2
3148
by: keeps21 | last post by:
I have a script that recieves an id number via the address bar when a link is clicked. ie . index.php?id=1 if the link was for the story whose ID is 1. My script checks if a user is logged in, if not they are redirected to the login page. If logged in they may edit the story. I assign $_GET to $id.
4
45665
by: mtuller | last post by:
I have a page that submits data to a database, and I want to make it so that if the page is refreshed, it doesn't submit the information again. I am trying to use unset the variables so that if the page is refreshed, it will not submit again. I saw a post about setting a time delay of something like 30 seconds, but would prefer just to unset the variable. I can't seem to get it working. Here is a snippet of what I have: echo 'First...
10
3019
by: Mason Barge | last post by:
I have a standard POST form consisting of two types of input: text input and textarea. The form downloads current settings from a mysql database. The user can update the information by modifying the text and clicking a standard "submit" button. MAIN PROBLEM: My problem is that the information transmitted to the formhandler apparently has some sort of whitespace added to it. If I simply use trim() on the POST variable, it fails the...
0
1285
by: Gilles Ganault | last post by:
Hello I use a POST variable named "status" to show different screens while keeping everything in the same script. The problem is that in the Print screen, I need to empty this variable so the user doesn't get FireFox's familiar ""The page you are trying to view contains POSTDATA" if he hits F5/Refresh. Neither unset() nor setting the variable to nothing works:
0
9846
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10883
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10579
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10633
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10276
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9412
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5674
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5852
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4050
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.