How get post value in pagination - php

I am working on search functionality with pagination in php.Below is my code but whenever I click on next link the search query is not taking variable which is passed through POST.
Can you please help me..
searchstr=$_POST['q'];
session_start();
$_SESSION['searchchar']=$searchstr;
$rec_limit=3;
$connection=connection();
$searchstr=$_REQUEST['q'];
$sql="Select count(*) from FIMTRN_Memorial where FirstName like '%".$_SESSION['searchchar']."%'";
echo$sql;
$result1=mysql_query($sql);
if(! $result1)
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($result1,MYSQL_NUM );
$rec_count = $row[0];
echo$rec_count;
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql="Select * from FIMTRN_Memorial where FirstName like '%".$_SESSION['searchchar']."%' limit $offset,$rec_limit";
echo$sql;
$result=mysql_query($sql);
$rec_num=mysql_num_rows($result);
if($rec_num>0)
{
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
$fname=$row['FirstName'];
$image=$row['ProfileImagePath'];
$about=$row['About'];
$url=$row['CustomURL'];
$DOB=$row['DOB'];
$DOD=$row['DOD'];
?>
<div class="search">
<p class="content"><img src="<?php echo $image;?>" style="border:1px solid red;width:100px;height:100px;" /></p>
<div class="content"><p>
<?php
echo "$fname";
?>
<p>(<?php echo date("Y",strtotime($DOB));?>-<?php echo date("Y", strtotime($DOD));?>)</p>
</p><p><?php echo$about?></p>
</div>
</div>
<?php
}
if( $page > 0 & $left_rec > $rec_limit)
{
$last = $page - 2;
echo "Previous Records |";
echo "Next Records";
}
else if( $page == 0 & $rec_count>$rec_limit )
{
echo "Next Records";
}
else if( $left_rec < $rec_limit & $rec_count>$rec_limit )
{
$last = $page - 2;
echo "Previous Records";
}
}
else
{
echo"No memorial found.Please try with other name.";
}
enter code here
?>

There are a couple problems in your code.
First:
You are using the $_GET variable incorrectly. You have $_GET{'page'}, but you should be using it the same way as the$_POST variable, like this: $_GET['page']
Second:
You are interpreting the idea of POSTing and clicking links incorrectly. What you need to make with your next and previous buttons are a hidden form that has all the other variables you need. For example, the simplest way that won't involve changing a lot of your other code:
if( $page > 0 & $left_rec > $rec_limit)
{
$last = $page - 2;
// Previous button form
echo "<form action='?page=$last' method='POST'>";
echo "<input type='hidden' name='q' value='" . $_POST['q'] . "' />"; // This will be a field that saves information but won't appear on the page (though a user could see it if they view source)
echo "<input type='submit' value='Previous Records' />";
echo "</form>";
echo " | "; // The delimiting character you were using, you'll need to style the input type='submit's to look more like a link if that's what you are going for
// Next button form
echo "<form action='?page=$page' method='POST'>";
echo "<input type='hidden' name='q' value='" . $_POST['q'] . "' />"; // This will be a field that saves information but won't appear on the page (though a user could see it if they view source)
echo "<input type='submit' value='Next Records' />";
echo "</form>";
}
This method will keep your variables in the request. Using a link essentially resets the $_POST variable. There are a ton of other possible solutions, but this is one will require the least amount of work to modify your example.

if you are creating sorting and searching functionality than I will suggest you to use $_SESSION variable rather than $_POST or $_GET it will help you manage your grid/data very well.

Related

Multiple php isset in one page

I'm trying to achieve multiple pagination in one page.
The pagination for all the results, is working.
But except from the main results, i want to include different options.
For example, in the image below i have three more options,
Newest, Reputation, Oldest
So, i want multiple pagination for all these options.
In the page that i want to display the results, i have multiple isset(), in order to achieve that.
<div id='paginationUsers'>
<?php
//for all the results that displaying first.
if(isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 1;
}
echo "<input type='hidden' id='pageAll' value='".$page."'>";
// for the Newest results
if(isset($_GET['pageNewest'])) {
$pageNewest = $_GET['pageNewest'];
} else {
$pageNewest = 1;
}
echo "<input type='hidden' id='newPage' value='".$pageNewest."'>";
//for the Oldest results
if(isset($_GET['pageOldest'])) {
$pageOldest = $_GET['pageOldest'];
} else {
$pageOldest = 1;
}
echo "<input type='hidden' id='oldPage' value='".$pageOldest."'>";
//for the Reputation results
if(isset($_GET['pageReputation'])) {
$pageReputation = $_GET['pageReputation'];
} else {
$pageReputation = 1;
}
echo "<input type='hidden' id='reputationPage' value='".$pageReputation."'>";
?>
</div>
Ofcourse, in that way cannot work as you can see in the code.
So, is it possible to include multiple isset in one page?
Try this code:
<?php
function getGET($name) {
if (isset($_GET[$name]) {
return $_GET[$name];
} else {
return 1;
}
}
function genInput($id, $name) {
return "<input type='hidden' id='". $id ."' value='". getGET($name) ."'>";
}
echo "<input type='hidden' id='pageAll' value='". getGet('page')."'>";
echo genInput('newPag', 'pageNewest');
echo genInput('oldPage', 'pageOldest');
echo genInput('', 'pageReputation');

php cart out of stock mysql

The following PHP validation code works however if i go back to products page to add more of the same product it can overrides saying out' on cart page when quantity is not available
The following shows an if statement that is linked to $cart session data along with $film quantity which is stored in a table.
For some reason $film['quantity'] has a value of 1 so if quantity >1 added to cart 'out' shows which is wrong?
<?php $itemcount = $_SESSION['itemcount'];
if($cart[QUANTITY][$i] > $table['quantity'] ) { ?>
<span style="font-family:'arial';">Out </span>
<?php } ?>
With the code shown it is hard to say what the issue is. However there is a good suspect here:
if($cart[QUANTITY][$i] > $film['quantity'] ) {
I suspect it should be:
if($cart[$i]['QUANTITY'] > $film['quantity'] ) {
Other things that might be amiss:
Have you session_start()ed?
Is the key QUANTITY not quantity (Case is important)
Do you happen to have defined a constant QUANTITY somewhere? (Quotes are important)
I think it will help you:
<?php
echo $product_image;
echo $product_count;
echo $product_price;
echo "<form method='post' action=''>";
echo "<input type='text' name='product_shopping_count' />";
echo "<input type='hidden' name='product_id' value='".$product_id."' />";
echo "<input type='submit' name='addCart' value='AddCart' />";
echo "</form>";
if(isset($_POST['addCart']){
if(isset($_SESSION['cart']){
$cart = $_SESSION['cart'];
if($cart[$_POST['product_id']]['quantity']>$product_count){
echo "<span style='font-family:arial;font-size:16px;'>Out </span>";
}else if($_POST['product_shopping_count']>$product_count){
echo "<span style='font-family:arial;font-size:16px;'>Out </span>";
}else {
$cart['product_id']= $_POST['product_id'];
$cart['product_id']['quantity'] = $_POST['product_shopping_count'];
$cart['product_id']['price'] = $product_price;
$_SESSION['cart'] = $cart;
}
}
}
?>

I have a simple php script for throwing dices, but all dices have the same number

I tried to make a dice script in php that should look like this one:
http://u11626.hageveld2.nl/po/opdracht1b/index.php
this is the link to mine:
http://u10511.hageveld2.nl/po/opdracht1b/index.php
Sorry if the answer is really obvious but i just can't figure out why the script doesn't work.
btw: "knop" means "button",and the pictures that I use as dices are called dobbelsteen1, dobbelsteen2 ..... dobbelsteen 6
<?php
session_start();
if (!isset($_SESSION["d1"]) || !isset($_SESSION["d2"]) || !isset($_SESSION["d3"])) {
$_SESSION["d1"]=0;
$_SESSION["d2"]=0;
$_SESSION["d3"]=0;
}
elseif (isset($POST["knop1"])) {
$_SESSION["d1"]=0;
}
elseif (isset($POST["knop2"])) {
$_SESSION["d2"]=0;
}
elseif (isset($POST["knop3"])) {
$_SESSION["d3"]=0;
}
elseif (isset($POST["knop4"])) {
$_SESSION["d1"]=0;
$_SESSION["d2"]=0;
$_SESSION["d3"]=0;
}
echo "d1 =" . $_SESSION["d1"];
echo "d2 =" . $_SESSION["d2"];
echo "d3 =" . $_SESSION["d3"];
if ($_SESSION["d1"]==0) {
$f = rand(1,6);
}
if ($_SESSION["d2"]==0) {
$g=rand(1,6);
}
if ($_SESSION["d3"]==0) {
$h=rand(1,6);
}
echo $f;
for ($r=1; $r<4; $r++) {
if (!$f==0) {
$f = 0;
echo "
<div align='center'>
<img src='dobbelsteen" . $f . ".gif'>
<form method='post'>
<input type='submit' name='knop1' value='Dobbelsteen gooien'>
</form>
";
}
elseif (!$g==0) {
$g = 0;
echo "
<div align='center'>
<img src='dobbelsteen" . $g . ".gif'>
<form method='post'>
<input type='submit' name='knop2' value='Dobbelsteen gooien'>
</form>
";
}
elseif (!$h==0) {
$h = 0;
echo "
<div align='center'>
<img src='dobbelsteen" . $h . ".gif'>
<form method='post'>
<input type='submit' name='knop3' value='Dobbelsteen gooien'>
</form>
";
}
}
?>
i have written what i consider an optimal script for this dice throwing exercise you are doing. I am giving you all the answers here but hopefully you will research my approach and learn from it.
<?php
//Start the php session
session_start();
//Initialise local dice array
//Check the session variable for already set values, if not set a random value
//Use TERNARY OPERATORS here to avoid multipl if else
$dice = array(
0 => (!empty($_SESSION['dice'][0])) ? $_SESSION['dice'][0] : rand(1, 6),
1 => (!empty($_SESSION['dice'][1])) ? $_SESSION['dice'][1] : rand(1, 6),
2 => (!empty($_SESSION['dice'][2])) ? $_SESSION['dice'][2] : rand(1, 6)
);
//If form has been submitted, and our expected post var is present, check the dice we want to role exists, then role it
//$_POST['roll_dice'] holds the index of the local dice value array element we need to update
if(!empty($_POST['roll_dice']) && !empty($dice[intval($_POST['roll_dice'])])){
$dice[intval($_POST['roll_dice'])] = rand(1, 6);
}
//Save the updated values to the session
$_SESSION['dice'] = $dice;
//Loop over the dice and output them
foreach($dice as $dice_index => $dice_val){
echo "<div class='dice' style='height:100px;width:100px;background-color:red;text-align:center;margin-bottom:50px;padding-top:10px;'>";
echo "<p style='style='margin-bottom:20px;'>".$dice_val."</p>";
echo "<form method='post'>";
echo "<input type='hidden' name='roll_dice' value='".$dice_index."' />";
echo "<input type='submit' value='Roll Dice' />";
echo "</form>";
echo "</div>";
}
?>
To add a new dice simply increase the size of the $dice array. For example the next one would be:
3 => (!empty($_SESSION['dice'][3])) ? $_SESSION['dice'][3] : rand(1, 6)
and then 4, and so on.
I hope this helps.
There are couple of issues with your script, as #Marc B has said "you never save the random numbers back into the session" also you are accessing the post data using $POST where as it should be $_POST, hope that can help you fixing your script.

Using a generic if statement for all values of an array in PHP

I have this php statement that collects data from a database table. It loops over all rows and separates them into tables of 2 (home team and away team for that game). I also have radio buttons so the user can pick which team they would like to win for each game. There are 9 games.
$games = array();
for($num = 1; $num <= 9; $num++)
{
$games[$num] = "<table border='1'><tr><b><h4>Game ".$num."</h4></b><th>Home</th><th>Draw</th><th>Away</th></tr>";
}
while ($row = mysql_fetch_array($rs))
{
for($num = 1; $num <= 9; $num++)
{
$games[$num] .=
" <tr>
<td><input type='radio' id='home".$num."' name='game".$num."' value='".$row['home'.$num]."' > ".$row['home'.$num]."</td>
<td>Draw <br /><input type='radio' id='draw".$num."' name='game".$num."' value='0'></td>
<td>".$row['away'.$num]."<input type='radio' id='away".$num."' name='game".$num."' value='".$row['away'.$num]."'></td>
</tr>";
}
}
On submit, i want to be able to do something for each game via SELFSUBMIT.
So if a user selects home team for game n, i want to display a message.
if($_SERVER["REQUEST_METHOD"] == "POST")
{
if($_POST['game".$num."'] = ('home'.$num))
{
echo ('home'.$num);
}
}
Thats my wrong php code. Please tell me if i can workaround this.
You might want to use jquery
$(document).ready( function(){
$("input[id^=home]").on("click", function(){
$("#spanDisplay").html( $(this).attr("id") );
//you can also display $(this).attr("name") or $(this).val() to print the value
});
});
In your html, you can use span or div to display the value of id for example.
So in your span
<span id="spanDisplay"></span>
UPDATE: using PHP only
$games[$num] .= "<form name="aForm" method="POST" action="afterSubmit.php">";
for($num = 1; $num <= 9; $num++){
$games[$num] .= "<tr><td><input type='radio' id='home".$num."' name='game".$num."' value='".$row['home'.$num]."'> ".$row['home'.$num]."</td>
<td>Draw <br /><input type='radio' id='draw".$num."' name='game".$num."' value='0'></td>
<td>".$row['away'.$num]."<input type='radio' id='away".$num."' name='game".$num."' value='".$row['away'.$num]."'></td></tr>";
}
$games[$num] .= "</form>";
You will need to surround your input with the form tag, submit and post it to the next page for instance afterSubmit.php. So in afterSubmit.php page, you can put something like this:
for($num = 1; $num <= 9; $num++){
if( isset( $_POST['game".$num."'] ) ){
echo $_POST['game".$num."']; //echo the value
}
}

how to submit a form to another page in wordpress plugin

I am developing a wordpress plugin , which submits a form to another page. But when I try to submit the form to another page , then that page returns some php error. My form code is below
echo "<form action='".plugins_url()."/wp_voting_poll/frontend_poll_process.php' method='post'>";
echo "<input type='hidden' name='hide' value='$ques' />";
$total_vote_count = $wpdb->get_var( "SELECT COUNT(*) FROM $table_result WHERE question_uid='$ques'" );
if($ques!=""){
echo "<table>";
foreach($ans_data as $ans_res){
// $ans=$ans_res->answer;
$answer_id=$ans_res->id;
$type=$ans_res->answer_type;
$vote_count = $wpdb->get_var( "SELECT COUNT(*) FROM $table_result WHERE answer_id='$answer_id'" );
if($vote_count==0){
error_reporting(0);
}
$vote_percent=($vote_count*100)/$total_vote_count;
echo "<tr> <td>";
echo "<div class='answer_div'>";
if($type==1){
echo "<div class='input'><input type='radio' name='ans_name[]' value='$answer_id'/>".$ans_res->answer."<br/></div>";
}
elseif($type==0){
echo "<div class='input'><input type='checkbox' name='ans_name[]' value='$answer_id'/>".$ans_res->answer."<br/></div>";
}
if($backend==0){
echo "</td> <td>";
echo "<h4> total vote counted $vote_percent% </h4>";
// echo "<img src='$url' width='$width_img'/>";
$bar=$vote_percent*5.9;
echo "<img src='$url' height='10' width='$bar' />";
echo "</td></tr>";
}
}
echo "</table>";
echo "<input type='submit' value='Submit vote' />";
echo "</form>";
And this is my code of another page , which should process the form . But unfortunately it returns php error.
<?php
require_once("function_ip.php");
$vote_result=$_POST['ans_name'];
$uid=uniqid();
global $wpdb;
$table_vote=$wpdb->prefix."poll_answer_result";
$count=count($vote_result);
$hidden=$_POST['hide'];
$ans_data=$wpdb->get_results("SELECT * FROM $table_vote WHERE question_id='$hidden'" );
if($count>0){
foreach($vote_result as $vote_arr){
$wpdb->insert($table_vote,
array('answer_id' => $vote_arr,
'ip' =>get_client_ip(),
'question_uid' => $hidden
));
}
}
?>
Wordpress has a generic handler to deal with all forms - admin-post.php.
If you include a hidden field in your form called action, you can then hook in to a function of your choice with all the goodness of wordpress included.
echo "<form action='".get_admin_url()."admin-post.php' method='post'>";
echo "<input type='hidden' name='action' value='submit-form' />";
echo "<input type='hidden' name='hide' value='$ques' />";
{ Enter the rest of your first block of code from above here }
echo "</form>";
And then in your functions.php file (or any other php file that you have included via functions.php), you can use this method.
add_action('admin_post_submit-form', '_handle_form_action'); // If the user is logged in
add_action('admin_post_nopriv_submit-form', '_handle_form_action'); // If the user in not logged in
function _handle_form_action(){
{ Enter your second block of code from above here }
}
I'm not sure if you require a redirect once you reach your desired destination, but that can be easily accounted for if you do.
And one final question - is this form on the front end, or in the admin area? Not that it should make a difference that this answer, I'm just curious...
Your frontend_poll_process.php page is getting called out of the WordPress environment, therefore returning an error on $wpdb->get_results().
You can add your code to a plugin or functions.php using hooks:
<?php
add_action( 'after_setup_theme', 'so_19997913' );
function so_19997913() {
require_once("function_ip.php");
$vote_result = $_POST['ans_name'];
$uid = uniqid();
global $wpdb;
$table_vote = $wpdb->prefix . "poll_answer_result";
$count = count( $vote_result );
$hidden = $_POST['hide'];
$ans_data = $wpdb->get_results( "SELECT * FROM $table_vote WHERE question_id='$hidden'" );
if ( $count > 0 ) {
foreach ( $vote_result as $vote_arr ) {
$wpdb->insert( $table_vote, array('answer_id' => $vote_arr,
'ip' => get_client_ip(),
'question_uid' => $hidden
) );
}
}
}

Categories