How to get MySQL query to carry over with PHP Pagination? - php

I am trying to keep my pagination based on the query I am using, but my problem is, it only works on the first page of pagination, and after that the query reverts to the standard one without filters (page one shows my filter, but page two show all results). I am wondering if there is an effective method that will carry over my filtered query when I click my pages, I am just at a loss right now as to how to accomplish this. Here is my code currently:
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<style type="text/css">
#win-range, #gaa-range, #sv-range{
width: 160px;
font-size: 10px;
margin: 0 auto;
}
#win-range a, #gaa-range a, #sv-range a{
margin-top: 0px !important;
padding: 0 !important;
}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(function(){
$("#win-range").slider({
range: true,
min: 1,
max: 1000,
values: [1, 1000],
slide: function(event, ui) {
// in order to pass the user selected values to your app, we will use jQuery to prepopulate certain hidden form elements, then grab those values from the $_POST
$("#minwins").val(ui.values[0]);
$("#maxwins").val(ui.values[1]);
$("#winamount").val(ui.values[0] + " - " + ui.values[1]);
}
});
$("#winamount").val($("#win-range").slider("values", 0) + " - " + $("#win-range").slider("values", 1));
});
$(function(){
$("#gaa-range").slider({
range: true,
min: 0,
max: 10,
values: [0, 10],
slide: function(event, ui) {
// in order to pass the user selected values to your app, we will use jQuery to prepopulate certain hidden form elements, then grab those values from the $_POST
$("#mingaa").val(ui.values[0]);
$("#maxgaa").val(ui.values[1]);
$("#gaaamount").val(ui.values[0] + " - " + ui.values[1]);
}
});
$("#gaaamount").val($("#gaa-range").slider("values", 0) + " - " + $("#gaa-range").slider("values", 1));
});
$(function(){
$("#sv-range").slider({
range: true,
min: 750,
max: 1000,
values: [750, 1000],
slide: function(event, ui) {
// in order to pass the user selected values to your app, we will use jQuery to prepopulate certain hidden form elements, then grab those values from the $_POST
$("#minsv").val(ui.values[0]);
$("#maxsv").val(ui.values[1]);
$("#svamount").val(ui.values[0] + " - " + ui.values[1]);
}
});
$("#svamount").val($("#sv-range").slider("values", 0) + " - " + $("#sv-range").slider("values", 1));
});
</script>
<?php
include("includes/header.php");
include("includes/mysqli_connect.php");
$minwins = $_POST['minwins'];
$maxwins = $_POST['maxwins'];
$mingaa = $_POST['mingaa'];
$maxgaa = $_POST['maxgaa'];
$minsv = $_POST['minsv'];
$maxsv = $_POST['maxsv'];
$querySelection = $_POST['q'];
// FILTERING YOUR DB
$sortstats = $_GET['sortstats'];
$sortstatslow = $_GET['sortstatslow'];
// pagination
$getcount = mysqli_query ($con,"SELECT COUNT(*) FROM Player");
$postnum = mysqli_result($getcount,0);// this needs a fix for MySQLi upgrade; see custom function below
$limit = 6; //how many blog posts per page you will see.
if($postnum > $limit){
$tagend = round($postnum % $limit,0);
$splits = round(($postnum - $tagend)/$limit,0);
if($tagend == 0){
$num_pages = $splits;
}else{
$num_pages = $splits + 1;
}
if(isset($_GET['pg'])){
$pg = $_GET['pg'];
}else{
$pg = 1;
}
$startpos = ($pg*$limit)-$limit;
$limstring = "LIMIT $startpos,$limit";
}else{
$limstring = "LIMIT 0,$limit";
}
// MySQLi upgrade: we need this for mysql_result() equivalent
function mysqli_result($res, $row, $field=0) {
$res->data_seek($row);
$datarow = $res->fetch_array();
return $datarow[$field];
}
?>
<div class="listingcontainer">
<div class="sidebar">
<h3>Sort By:</h3>
Most Wins
Best Goals Against
Best Save %
<hr/>
<h3>Custom Filter</h3>
<br/>
<div class="custom-filter">
<form name="filters" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="filters">
<label for="winamount">Win Range:</label>
<input type="text" id="winamount" />
<div style="clear:both;"></div>
<input type="hidden" id="minwins" name="minwins" value="0" />
<input type="hidden" id="maxwins" name="maxwins" value="1000" />
<div id="win-range"></div>
<br/>
<label for="gaaamount">GAA:</label>
<input type="text" id="gaaamount" /><br />
<div style="clear:both;"></div>
<input type="hidden" id="mingaa" name="mingaa" value="0" />
<input type="hidden" id="maxgaa" name="maxgaa" value="10" />
<div id="gaa-range"></div>
<br/>
<label for="svamount">SV %:</label>
<input type="text" id="svamount" /><br />
<div style="clear:both;"></div>
<input type="hidden" id="minsv" name="minsv" value="750" />
<input type="hidden" id="maxsv" name="maxsv" value="1000" />
<div id="sv-range"></div>
<input type="submit" name="submit" id="submit"/>
</form>
</div>
</div>
<div class="main-listings">
<h1>Current NHL Goaltenders</h1>
<?php
$result = mysqli_query($con, "SELECT * FROM Player ORDER BY PlayerID ASC $limstring");
if(isset($sortstats)) {
$result = mysqli_query($con,"SELECT * FROM Player ORDER BY $sortstats DESC $limstring ") or die (mysql_error());
}
if(isset($sortstatslow)) {
$result = mysqli_query($con,"SELECT * FROM Player ORDER BY $sortstatslow ASC $limstring ") or die (mysql_error());
}
if(isset($_POST['submit']))
{
$result = mysqli_query($con, "SELECT * FROM Player WHERE Wins BETWEEN '$minwins' AND '$maxwins' AND
GAA BETWEEN '$mingaa' AND '$maxgaa' AND SavePerc BETWEEN '$minsv' AND '$maxsv'
ORDER BY PlayerID ASC $limstring") or die (mysql_error());
}
while($row = mysqli_fetch_array($result)){
$name = $row['LastName'] . ", " . $row['FirstName'];
$wins = $row['Wins'];
$pid = $row['PlayerID'];
$image = $row['Picture'];
$gaa = $row['GAA'];
$sv = $row['SavePerc'];
echo "<div class=\"player-listing\">";
echo "<div class=\"image-holder\">";
echo "<span class=\"helper\"></span>";
echo "<img src=\"admin/thumbs/$image\" alt=\"$name\">";
echo "</div>";
echo "<div style=\"clear:both;\"></div>";
echo "$name";
echo "<table align=\"center\">";
echo "<tr>";
echo "<td style=\"border-bottom: 1px solid #212121;\">Wins</td>";
echo "<td style=\"border-bottom: 1px solid #212121;\">GAA</td>";
echo "<td style=\"border-bottom: 1px solid #212121;\">SV%</td>";
echo "</tr>";
echo "<tr>";
echo "<td>$wins</td>";
echo "<td>$gaa</td>";
echo "<td>.$sv</td>";
echo "</tr>";
echo "</table>";
echo "</div>";
}
// paging links:
echo "<div class=\"paging\">";
if($postnum > $limit){
echo "<span class=\"page-numbers\"><strong>Pages:</strong> </span>";
$n = $pg + 1;
$p = $pg - 1;
$thisroot = $_SERVER['PHP_SELF'];
if($pg > 1){
echo "<< prev ";
}
for($i=1; $i<=$num_pages; $i++){
if($i!= $pg){
echo "$i ";
}else{
echo "$i ";
}
}
if($pg < $num_pages){
// INSERT QUERY STRING VARIBLE TO CARRY OVER DB QUERY
echo "next >>";
}
echo " ";
}
// end paging
echo "</div>";
?>
</div>
<div style="clear:both;"></div>

Save you search filters in session and always check the session value is set for any search or not and pass that data into you query.until and unless nobody reset that search criteria and when your user reset that search criteria to null then put the value blank in the session for that session search variable.

You need to pass all filter conditions to the paginating link, for example
$filter = "minwins={$minwins}&maxwins={$maxwins}&mingaa={$mingaa}&minsv={$minsv}&maxsv={$maxsv}&q={$querySelection}";
if($pg > 1){
echo "<< prev ";
}
for($i=1; $i<=$num_pages; $i++){
if($i!= $pg){
echo "$i ";
}else{
echo "$i ";
}
}
if($pg < $num_pages){
// INSERT QUERY STRING VARIBLE TO CARRY OVER DB QUERY
echo "next >>";
}
In the code for getting filter conditions from $_POST you are using, change it to $_REQUEST because it should get from URL ($_GET).
Ex
$minwins = $_REQUEST['minwins'];

You could always use the below to create a list of URL keys (filters).
foreach($_GET as $key => $value){
$url_keys .= $key . "=" . $value . "&";
}
And then add that into your URL for next/prev page.
echo "next >>";
You will then be passing your filters through the URL and have access to them on the next page.

Related

Why a different result of a PHP for loop when loaded with Ajax?

I load data onto a page using Ajax.
Here is the div (saleContentData) into which it is loaded:
<div class="resultsPage salesList">
<h2>Sales</h2>
<div class="salesFilters">
<form action="" method="POST">
<label for="date1">Start Date</label>
<input id="startDate" type="date" name="date1" value="Start Date" placeholder="Start Date" value="" min="1997-01-01" max="2030-12-31" />
<label for="date2">End Date</label>
<input id="endDate" type="date" name="date2" value="End Date" placeholder="End Date" value="" min="01-01-2010" max="01-01-2030" />
<button id="filterSales">Filter Sales</button>
<button id="resetFilter">Reset</button>
</form>
</div>
<div class=" salesContent">
<div class="saleContentData">
<div id="loading">
<img id="loading-image" src="assets/img/loader.gif" alt="Loading..." />
</div>
</div>
</div>
</div>
Here is the the script to display the data.
if (isset($_POST['start_date']) && $_POST['end_date']) {
$startDate = $_POST['start_date'];
$endDate = $_POST['end_date'];
$page_size = $_POST['page_size'];
$page_num = $_POST['page_num'];
showFiltered($startDate, $endDate, $page_size, $page_num);
} else if (isset($_POST['page_size']) && $_POST['page_num']) {
$page_size = $_POST['page_size'];
$page_num = $_POST['page_num'];
showNormal($page_size, $page_num);
} else {
showNormal();
}
function showNormal($page_number = 1, $page_size = 100)
{
//THE DATA
$data = getData("https://api.someapi.com/v2/sales?page_size=" . $page_size . "&page_number=" . $page_number);
//SHOW THE DATA
showRecords($data);
}
function showRecords($data)
{
//STATISTICS ABOUT THE DATA
$total_sales = ($data['page_summary']['total']);
$current_page = $data['page_summary']['page_number'];
echo $total_sales . " sales<br/>";
$num_pages = (ceil($total_sales / 100) * 100) / 100;
echo "<ul>";
// TO DISPLAY THE NUMBER OF PAGES
for ($page = 0; $page < $num_pages; $page++) {
if ($page == $current_page - 1) {
echo "<li class='page$page active'>" . $page + 1 . "</li>";
} else {
echo "<li class='page$page'>" . $page + 1 . "</li>";
}
}
echo "</ul>";
// TABLE WITH ALL SALES RECORDS
$sales = $data['sales'];
$counter = 1;
echo "<table id='salesTable'>
<thead><tr class='headingRow'><th class='image'></th><th class='productHD'>Product</th><th>Status</th><th>Revenue</th><th>Quantity</th><th class='dcCol'>DC</th><th>Order ID</th><th>Customer Name</th><th>Date</th></tr></thead>";
foreach ($sales as $key => $row) {
$col1 = $row['selling_price'];
$col2 = $row['quantity'];
$revenue = ($col1 * $col2);
echo "<tr><td class='num centerCol'><img src='placeholder.png'/></td><td class='productTitle'>" . $row['product_title'] . "</td><td class='centerCol'>" . $row['sale_status'] . "</td><td class='centerCol'>" . "R" . $revenue . "</td><td class='centerCol'>" . $row['quantity'] . "</td><td class='centerCol'>" . $row['dc'] . "</td><td class='centerCol'>" . $row['offer_id'] . "</td><td>" . $row['customer'] . "</td><td>" . $row['order_date'] . "</td></tr>";
}
echo "</table>";
}
Here is the Javscript that loads the page into the div:
$(".goSale").click(function () {
$.ajax({
method: "GET",
url: "getsales.php",
success: function (data) {
$(".saleContentData").html(data);
},
error: function (request, status, error) {
$(".saleContentData").html(error);
},
complete: function () {},
});
});
if I run the file by itself the list with the page numbers is displayed correctly and I see 1 TO 7. If I load the file using Ajax I get a different result and the page numbers are displayed as seven 1s.
Why would this be please?
So after looking at the HTML I see the list is not being generated and the numbers are loaded in the tag and so there is not list items.
Why would that be?
Does Ajax remove something?
Loading the page by itself
Loading the page using Ajax
Solved it:
echo "<ul id='pagesList'>";
for ($page = 0; $page < $num_pages; $page++) {
echo "<li class='page$page'>";
echo $page + 1;
echo "</li>";
}
echo "</ul>
The problem might be that you did not send any data to getsales.php page.
Check something like this.
How to send multiple data fields via Ajax?
If script you showed for displaying data is getsales.php, then you need to send there some data as below via ajax.
if (isset($_POST['start_date']) && $_POST['end_date']) {
$startDate = $_POST['start_date'];
$endDate = $_POST['end_date'];
$page_size = $_POST['page_size'];
$page_num = $_POST['page_num'];
}
showFiltered($startDate, $endDate, $page_size, $page_num);

How to fix an array of inputs and display it correctly

This Question is a bit long and I have edited too much to reduce my code but I hope anyone can help me out please!
I'm working on a Math test in times table project with php which will ask the user to enter his name and then ask him 10 questions like "2 x 2 = ?" giving four buttons to choose an answer and then after answering 10 questions the user will be redirected to result.php page which will show the result and here is what I wrote so far in homePage.php:
<?php
session_start();
if (isset($_POST['submit']) ) {
$_SESSION['QuestionNumber'] = 1;
$_SESSION['CorrectAnswers'] = 0;
$_SESSION['QuestionsAsnwered'] = 0;
$_SESSION['QuestionsAnswered'] = 0;
header("location: MathTest.php");
}
?>
<!DOCTYPE html>
<html>
<body>
<h1 align="center">Math Test</h1>
<form method="POST" action="">
<div style="text-align: center;">
<input type="submit" name="submit" value="Begin Test"">
</div>
</form>
</body>
</html>
and in MathTest.php which contains the problem:
<?php
session_start();
//just controlling user answer no problem here
if (isset($_POST[$_SESSION['UserAnswer']])) {
if ($_SESSION['QuestionNumber'] == 10) {
header("location: result.php");
}
if ($_SESSION['UserAnswer'] == $_SESSION['CorrectAnswer']) {
$_SESSION['QuestionNumber'] += 1;
$_SESSION['CorrectAnswers'] += 1;
$_SESSION['QuestionsAnswered'] += 1;
QuestionGenerator();
} else {
$_SESSION['QuestionNumber'] += 1;
$_SESSION['QuestionsAnswered'] += 1;
QuestionGenerator();
}
}
//I think that my problem(written down) is here
function QuestionGenerator(){
$rnd1 = rand(1, 12);
$rnd2 = rand(2, 12);
$CorrectAnswer = $rnd1 * $rnd2;
$WrongAnswer1 = $CorrectAnswer - 2;
$WrongAnswer2 = $CorrectAnswer + 2;
$WrongAnswer3 = $CorrectAnswer + 4;
$_SESSION['rnd1'] = $rnd1;
$_SESSION['rnd2'] = $rnd2;
$_SESSION['CorrectAnswer'] = $CorrectAnswer;
$_SESSION['WrongAnswer1'] = $WrongAnswer1;
$_SESSION['WrongAnswer2'] = $WrongAnswer2;
$_SESSION['WrongAnswer3'] = $WrongAnswer3;
$_SESSION['AnswersArray'] = array($_SESSION['CorrectAnswer'], $_SESSION['WrongAnswer1'], $_SESSION['WrongAnswer2'], $_SESSION['WrongAnswer3']);
}
function EchoAnswers($array) {
shuffle($array);
echo "<form method='POST' action='' ";
foreach ($array as $_SESSION['UserAnswer']) {
echo "<button><input style='width: 200px; height: 100px; font-size: 75px;' type='submit' name='" . $_SESSION['UserAnswer'] . "' value='" . $_SESSION['UserAnswer'] . "' ></button";
}
echo "</form>";
}
?>
<!DOCTYPE html>
<html>
<body>
<?php echo "<h1 align='center' style='font-size: 75px;'>Question " . $_SESSION['QuestionNumber'] . " of 10</h1>"; ?>
<h2 align="center" style="font-size: 60px"><?php echo $_SESSION['rnd1'] . " X " . $_SESSION['rnd2'] . " = ?" ?></h2>
<div align="center">
<?php EchoAnswers($_SESSION['AnswersArray']) ?>
</div>
</body>
</html>
the problem is when I click on any button it will only shuffle the answers but if I press the last button it will work full functionally
The problem I had was that there is 4 submit buttons each with different name tag so the session will only store the last input button and will make it functional but for the others it will only refresh the page with no different because they are not being controlled by php code so to solve this problem I only changed the EchoAnswers function like this:
function EchoAnswers($array) {
shuffle($array);
echo "<form method='POST' action='' ";
foreach ($array as $_SESSION['UserAnswer']) {
echo "<button><input style='margin: 10px; border: 3px solid black; background-color: lightblue; width: 200px; height: 100px; font-size: 75px;' type='submit' name='UserAnswer' value='" . $_SESSION['UserAnswer'] . "' ></button";
}
echo "</form>";
}
the name tag was also set to $_SESSION['UserAnswer'] so I changed this part to a normal name and I also changed the php code like this:
if (isset($_POST['UserAnswer'])) {
if ($_SESSION['QuestionNumber'] == 10) {
header("location: result.php");
}
if ($_SESSION['UserAnswer'] == $_SESSION['CorrectAnswer']) {
$_SESSION['QuestionNumber'] += 1;
$_SESSION['CorrectAnswers'] += 1;
$_SESSION['QuestionsAnswered'] += 1;
QuestionGenerator();
} else {
$_SESSION['QuestionNumber'] += 1;
$_SESSION['QuestionsAnswered'] += 1;
QuestionGenerator();
}
}
the part that I have changed in php code is isset($_POST['UserAnswer']) because UserAnswer was set to $_SESSION['UserAnswer'] instead of normal name in the name tag.

Keep Page Data on Refresh With $_POST

<!DOCTYPE html>
<html lang="en">
<head>
<h1>Table Generator</h1>
</head>
<body>
<center>Refresh</center>
<?php
$rows = (isset($_POST['rows']) ? $_POST['rows'] : null);
$cols = (isset($_POST['cols']) ? $_POST['cols'] : null);
$highlight = (isset($_POST['highlight']) ? $_POST['highlight'] : null);
if ($rows == "")
{
$rows = 10;
}
if ($cols == "")
{
$cols = 10;
}
if ($highlight == "")
{
$highlight = 5;
}
?>
<form method="post">
ROWS <input type="text" name="rows" value = "<?php echo $rows;?>" />
COLUMNS <input type="text" name="cols" value = "<?php echo $cols;?>" />
HIGHLIGHT <input type = "text" name = "highlight" value = "<?php echo $highlight;?>" /><br>
<input type="submit" value="Generate">
</form>
<?php
if(isset($_POST['rows']))
{
$randnumber = rand(0,100);
$rows = $_POST['rows'];
$cols = $_POST['cols'];
$highlight = $_POST['highlight'];
echo '<table border="1" align = "center">';
if (is_numeric($rows) and is_numeric($cols) and is_numeric($highlight))
{
if ($randnumber % 2 == 0)
{
echo '<center>The first number is <div class = "red">even</div></center>';
}
else
{
echo '<center>The first number is <div class = "green">odd</div></center>';
}
for($row = 1; $row <= $rows; $row++)
{
echo '<tr style = "background-color:green">';
for($col = 1; $col <= $cols; $col++)
{
if ($randnumber % $highlight == 0)
{
echo '<td style = "background-color: red">';
echo $randnumber;
$randnumber++;
echo '</td>';
}
else
{
echo '<td>';
echo $randnumber;
$randnumber++;
echo '</td>';
}
}
echo '</tr>';
}
echo '</table>';
}
else
{
echo "<center>Rows / Columns / Highlight must ALL be INTEGER values. Re-enter correct value(s).</center>";
}
echo '<pre><center>';
print_r($_POST);
echo '</center></pre>';
}
?>
<style type ="text/css">
h1 {
color: grey;
text-align:center;
}
form {
text-align: center;
padding-bottom: 20px;
}
a:link {
text-decoration: none;
}
.red {
color: red;
}
.green {
color: green;
}
</style>
</body>
</html>
So. I have this PHP code to generate a table based off the user's input and I recently ran into a problem I cant figure out how to fix.
It was working perfectly fine but now whenever I use the Refresh link it resets the entire page to default (i.e. default textbox values instead of keeping the current ones, removing the table).
So, I have 2 questions. How would I keep the data on refresh (with $_POST being used) and how to display the table with the default values when the page first loads.
Refresh
Clicking it will trigger browser's reload mechanism and you'll be asked to resubmit the form action, it will allow you to keep POST data.
You need to re-create the post if you want to keep the parameters. Can be done pretty eaily by looping thru the array.
<form method='POST' id='refresh' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
<?php foreach($_POST as $k=>$v): ?>
<input type='hidden' name='<?php echo $k; ?>' value='<?php echo $v; ?>' />
<?php endforeach; ?>
<a href='#' onclick='document.getElementById("refresh").submit(); return false;'>refresh</a>
</form>
Note: This is a little longer than the other answer, but will not prompt to resend post data.

PHP Value of Variable is undefined

I am getting " Notice: Undefined index:k" in the PHP code. K is the name of the text field and I am using $_GET[] method to get the value of k. In the below mentioned example I am trying to keep the value available even after the form is submitted. This Code runs fine for the first time but the second time it gives the above error.
<form name="keywordquery" method="get" action="keywordsearch.php">
<fieldset class="fieldsetclass"><legend class="legendclass">Search by
Keywords</legend>
<div id="searchbox">
<input type="text" name="k" value="<?php if(isset($_GET['k'])){echo
htmlentities($_GET['k']);} ?>" style="border: 1px, thin; width:92%; "/> <input
type="image" style="margin-bottom: 0; margin-top: 2px;" src="search.png"
value="submit" />
</div>
</fieldset>
</form>
</div>
<table id="dataTable" cellpadding="0" cellspacing="0" border="1" style="border-
radius:20px; box-shadow: 9px 5px 8px #7E9044;">
<tbody>
<?php
// PAGINATION Code. check if the starting row variable was passed in the
URL or not
if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
//we give the value of the starting row to 0 because nothing was found in URL
$startrow = 0;
//otherwise we take the value from the URL
} else {
$startrow = (int)$_GET['startrow'];
}
$k1 = $_GET['k'];
$term = explode(" ", $k1);
$query = "SELECT * FROM data ";
foreach ($term as $each) {
$i++;
if($i==1)
{
$query .= "WHERE keywords LIKE '%$each%' ";
}
else {
$query .= " OR WHERE keywords LIKE '%$each%' ";
}
}
$query .= "LIMIT $startrow, 1";
$connection = mysql_connect("xxxx", "xxxxx","");
if(!$connection)
echo "No database connected";
$dbase = mysql_select_db("xxxxxxxx", $connection);
if(!$dbase)
echo "No datatable connected";
$ourquery1 = mysql_query ($query);
if(!$ourquery1)
echo "No query found";
$row1 = mysql_num_rows ($ourquery1);
if ($row1 > 0)
{
while($result = mysql_fetch_assoc($ourquery1))
{
echo "<tr>";
$title = $result['title'];
$link = $result['link'];
$region = $result['region'];
$sector = $result['sector'];
$theme = $result['theme'];
echo "<td> <a href=$link><h3>$title<h3></a>";
echo "<h4>Sector: $sector
Theme: $theme
<br> Region: $region </td>";
}
}
echo "</tbody>";
echo 'Next';
echo 'Prev';
Replace the line:
$k1 = $_GET['k'];
with something like:
$k1 = isset($_GET['k'])? $_GET['k'] : $default_k_value;
You don't show the complete form so it is tough to tell what is wrong but here is a hint. Swap $_GET with $_REQUEST.
Example:
<?php if(isset($_REQUEST['k'])){echo
htmlentities($_REQUEST['k']);} ?>
If the form is using a POST method, the value would be in $_POST. If the form uses a GET method, the value would be in $_GET. However $_REQUEST will contain the form fields whether the form used POST or GET.

insert to database with checkbox

I'm trying to find a way to upload a value to SQL using a checkbox but no luck
this is my code:
/////////////////////////// FOR RXTRA ////////////////////////////////////////////////////////////
$sql = "SELECT ext_id,ext_price,ext_name,ext_description FROM tbl_extra ORDER by ext_id ASC";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
$number = mysql_num_rows($result);
$i = 0;
while ($number > $i) {
$ID = mysql_result($result,$i,"ext_id");
$NA= mysql_result($result,$i,"ext_name");
$PR= mysql_result($result,$i,"ext_price");
$DES= mysql_result($result,$i,"ext_description");
//this part chack if the value is "0" and show with "tooltip" the value\/
if ( $PR == 0 ) {
print ''.$NA.'<span>' .' free '.'</span>!';
} else {
print ''. $NA .'<span>' .' add '.' '. $PR .' $ '. '</span>!';
}
print "<input style='width: 30px; height: 15px;' type='checkbox' name='extra[]' value='$NA'></td>\n";
//this java calculate the value add to extra ant outpot the total extra that pass to sql table
print "<input type='hidden' name='item_name'/>";
print "<input type='hidden' name='amount'/>";
print "<input style='width: 30px; height: 15px;' type='checkbox' onClick='ReadForm (this.form, false);' value='+$PR'></td>\n";
$i++;
}
?>
</div></div>
<?
} else {
}
?>
<!----->
<div class="item_add_cart">
<span class="title">total extra $</span>
<div class="content">
<?
print "<input style='color:#000;font-size:13px;' size='7' name='tot' type='text'/>";
?>
</div></div>
<!----->
my problem is that I have two table's one is the total price and one is the names I try to insert all value with one checkbox and it's not working
If I create 2 checkboxes and click on them then the value uploads ok
but I need only one checkbox that sends value from $NA to Table ext_name and total price from name='tot' to ext_price table
Please note that the above code doesn't work, #Majid Fouladpour suggests the following code instead.
/////////////////////////// FOR RXTRA ////////////////////////////////////////////////////////////
$sql = "SELECT ext_id,ext_price,ext_name,ext_description FROM tbl_extra ORDER by ext_id ASC";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
$number = mysql_num_rows($result);
$i = 0;
while ($number > > $i) {
$ID = mysql_result($result,$i,"ext_id");
$NA= mysql_result($result,$i,"ext_name");
$PR= mysql_result($result,$i,"ext_price");
$DES= mysql_result($result,$i,"ext_description");
//this part chack if the value is "0" and show with "tooltip" the value\/
if ( $PR == 0 ) {
print ''.$NA.'' <a href="#" class="tooltip2">'.$NA.'<span>' .' free '.'!';
.'</span></a>!';
} else {
print ''. <a href="#" class="tooltip2">'. $NA .'' '<span>' .' add '.' '. $PR .' $ '. '!';
</span></a>!';
}
print "\n";<input style='width: 30px; height: 15px;' type='checkbox' name='extra[]' value='$NA'></td>\n";
//this java calculate the value add to extra ant outpot the total extra that pass to sql table
print "";
<input type='hidden' name='item_name'/>";
print "";<input type='hidden' name='amount'/>";
print "\n";
<input style='width: 30px; height: 15px;' type='checkbox' onClick='ReadForm (this.form, false);' value='+$PR'></td>\n";
$i++;
}
?>
total >
</div></div>
<?
} else {
}
?>
<!----->
<div class="item_add_cart">
<span class="title">total extra $</span>
<div class="content";
>
<?
print "<input style='color:#000;font-size:13px;' size='7' name='tot' type='text'/>";
?>
>
</div></div>
<!----->
(I've made this answer community wiki so the rep doesn't go to me)

Categories