How send some info (values) in a link (<a>) to another page? - php

I have a page which generates a list of games and button for each game with value "BUY GAME".
The objective is to execute multiple SQL queries when the button is clicked for a particular game.
What is an appropriate way of accessing the 'name','consolename',etc. for EACH game from the ARRAY and to execute queries on them in another page - purchase.php?
NOTE: I did consider using sessions but when I pass the variable $index as a SESSION variable, I only get the final value at all times.
buygame.php
<?php
session_start();
//connect to db
dbConnect("root", "") ;
dbSelect("webdesign");
//SEARCH FOR GAMES
print "<h3>Games to Buy: </h3>";
$query = "SELECT gamecode,name, consolename,price, points, genre from game";
$result = runQuery($query);
while($row = mysql_fetch_array($result))
{
$array[] = $row;
}
$index =0 ;
//DISPLAYING A LIST OF GAMES
while($index<sizeof($array))
{
echo $array[$index]['name'];
echo $array[$index]['consolename'];
echo $array[$index]['genre'];
echo ("Price is: ".$array[$index]['price']." USD");
echo ("Loyalty Points: ".$array[$index]['points']);
//THE BUTTON "BUY GAME"
echo("<a href='purchase.php'><input type='submit' name='submit[$index]' value='Buy Game'/></a>");
$index = $index + 1;
}
?>
purchase.php
<?php
session_start();
//connect to database
dbConnect("root", "") ;
dbSelect("webdesign");
$index = 0;
echo "Button clicked ";
//I am trying this but I'm sure that this is not right
if (isset($_POST['submit[$index'])){
print "$index was clicked";
}
?>

In buygame.php change the code to:
//THE BUTTON "BUY GAME"
echo("<a href='purchase.php?gamecode={$array[$index]['gamecode']}'><input type='submit' name='submit[$index]' value='Buy Game'/></a>");
In purchase.php
<?php
if(isset($_GET['gamecode'])) {
echo $_GET['gamecode'];
//and make ......
}

//THE BUTTON "BUY GAME"
$gamecode = $array[$index]['gamecode'];
echo("<a href='purchase.php?gamecode=$gamecode'><input type='submit' name='submit[$index]' value='Buy Game'/></a>");
$index = $index + 1;
Then in purchase.php You know exactly, what game has been bought.

Related

How run code when clicking on a looped button

I need to show a button for each value and do some function,
The following code is working for displaying the button only
but it doesn't show the result of the function after the button
$ids = [1,2,3,4,5,6];
foreach( $ids as $id ) {
$post_url = get_the_permalink($id);
echo $post_url."<input type='submit' name='doit' value='Change'><br>";
$doit = $_POST['doit'];
if(isset($_POST['doit'])){
echo get_post_field( 'post_name', $id)."<br>";
}
}
Can you try this solution?
I added an ID to the name field so when you click the button it runs only the if statement of this button.
Also make sure that the buttons are wrapped inside a form tag with a method attribute.
echo "<form method='post'>";
$ids = [1,2,3,4,5,6];
foreach( $ids as $id ) {
$post_url = "test.com";
echo "$post_url <input type='submit' name='doit_$id' value='Change'><br>";
if(isset($_POST['doit_'. $id])){
echo $id . "_RUNNING <br>";
}
}
echo "</form>";
Also $doit = $_POST['doit']; prints a NOTICE in php when it is not defined, it is better to check if $_POST['doit'] isset before assigning it to a variable.

PHP-how to send data to another php-sql page and recive an chart image on the same page

PHP-how to send data to another php-sql page and recive an chart image on the same page
hi guys. i have an problem and i dont know how i could solve that. i have an mainpage, where i want to put some data in an input field, which would send
the data to an other page to create the chart. ok. thats not the problem. it works fine, if i open the secound page. the chart img is created. but if i want to stay on the mainpage klicking the button and recive the img from the secound page, the img is empty and i dont know how to realize that.
here is an short code of the mainpage.php
echo "<form action='#' method='post' type='text'>";
echo "Abzufragendes Jahr [JJJJ]:";
echo '<input type="text" name="eingabe_1" value="">';
echo "<input type='submit' name='eingabe_2' value='DB Abfrage'/>";
echo "<input type='submit' name='ausgabe_statistik' value='Charts erstellen'
formaction='#' formmethod='post'/></br></br></br></br>";
echo "</form>";
//Versuch das Bild zu erzeugen
if(isset($_POST['eingabe_1']) && isset($_POST['ausgabe_statistik'])) {
echo "<img src='charts/charts_jahr.php'> </br>";
}
and the secound page consists
include("../../PHP/pChart2.1.4/class/pData.class.php");
include("../../PHP/pChart2.1.4/class/pDraw.class.php");
include("../../PHP/pChart2.1.4/class/pImage.class.php");
include("../../SQL/log-in.php");
mysql_select_db(MYSQL_DATENBANK) or die("Auswahl der Datenbank fehlgeschlagen");
for ($a = 1 ; $a <= 365 ; $a++) {
$kalenderwoche[] = $a;
}
$var = $_POST['eingabe_11'];
$var_jahr = "j" . $var;
$Result = mysql_query("SELECT sum(gewicht) as gewicht_1 FROM $var_jahr group by datum",$db_link);
while($row = mysql_fetch_array($Result))
{
$gewicht[] = $row["gewicht_1"];
$fett[] = $row["fett"];
}
//chartpart
$myData = new pData();
$myData->addPoints($gewicht,"Serie1");
..
.
i thought over another solution to save the temp var in he sql and get the value for $var_jahr out of them. but i think thats not the solution right?
br and many thx for an solution!
My solution:
if ( isset($_POST['statistik_gewicht']) ) {
$var22 = $_POST['eingabe_1'];
mysql_query("update variablen set var1 = $var22 where id = 1");
mysql_query("update variablen set var_gewicht = 1 where id = 1");
echo "<div align='center'><img src='charts/charts_jahr.php'></div> </br>";
}
Not well, but it works! Write an tmp var over
echo '<td colspan="3">Abzufragendes Jahr [JJJJ]'; echo '<input type="text" name="eingabe_1" value="">';

How do I continually rename a variable in php?

I apologize in advanced because I believe that my question may be kind of confusing.
I have three different PHP files. The first one asks you how many different products you want (1-20) in which the variable is called $quantity. Once a number is selected from a drop down box, you are taken to the next PHP page that automatically generates a table with $quantity number of rows and in each row there is a dropdown box that is being populated from a database. There is also another column with empty textboxes for the quantity.
Here is the code for that:
<?php
$quantity = "";
$i = 1;
if (isset($_POST['quantity'])) {
$quantity= $_POST['quantity'];
$var = "";
while($row = mysqli_fetch_array($result1)){
$var = $var . "<option>" . $row['product_name'] . "</option>";
}
echo "<left><table border='1' width='1%'><tr><td><center>Product</td><td>Quantity</center></td></tr>";
while ($i <= $quantity){
echo "<tr><td><select name='product[]' size='1'>";
echo $var;
echo "</select></td><td><input name='quant[]' size='5' /></td></tr>";
$i++;
}
echo "</table></left>";
}
?>
Once each product and its desired quantity is entered, the user clicks submit and they are taken to the final PHP page. This PHP page is supposed to be a confirmation page with all of the customer information and their selected products and quantities. HOWEVER, my code is only printing out the LAST product and quantity in the table from the second PHP page. For example if the table is:
Product Quantity
Bed 1
Chair 2
Couch 3
My confirmation page prints out a one row table with ONLY the information for Couch instead of multiple rows with ALL three of those products. Here is my code for the last PHP page:
<body>
<?php
$curTime= "";
$customerbox= "";
$region= "";
$products = $_POST['product']; //I changed this (edit 2)
$quants = $_POST['quant']; //I changed this (edit 2)
if (isset($_POST['curTime'])) $curTime= $_POST['curTime'];
if (isset($_POST['customerbox']))$customerbox= $_POST['customerbox'];
if (isset($_POST['region']))$region= $_POST['region'];
if (isset($_POST['product']))$product= $_POST['product'];
if (isset($_POST['quant']))$quant= $_POST['quant'];
$error= false;
$done=false;
if ((isset($curTime) && empty($curTime))) {
print "Please enter the date.<br/>";
$error = true;
}
if (!isset($_POST['customerbox'])) {
print "Please select your customer.<br/>";
$error = true;
}
if (!isset($_POST['region'])){
print "Please select your region.<br/>";
$error = true;
}
if (!isset($_POST['product'])){
print "Please select your product.<br/>";
$error = true;
}
if ((isset($quant) && empty($quant))){
print "Please enter the quantity.<br/>";
$error = true;
}
else{
$error = true;
$done = true;
}
for ($i =0; $i < count($products); $i++){
echo $products[$i]; //I changed this
echo $quants[$i]; //I changed this
}
?>
<br>
<table style= border-collapse:collapse width="1%"border="2" align="center" <?php if (!$done){ echo "hidden";}?>
<tr>
<th>Date</th>
<th>Customer</th>
<th>Region</th>
<th>Product</th>
<th>Quantity</th>
</tr>
<tr>
<td><center><?php print $curTime?></td></center>
<td><center><?php print $customerbox?></td></center>
<td><center><?php print $region?></td></center>
<td><center><?php print $product?></td></center>
<td><center><?php print $quant?></td><center>
</tr>
</table>
</body>
I believe this problem is occurring because when I am creating the table with $quantity rows, it's continuously naming each dropdown box $product so it's taking the very last value as $product and printing that.
Is there anyway to print out all of the products with their respective quantities?
Thank you in advance!
For your product and quant dropdown boxes you should use name="product[]" and name="quant[]".
This will send an array instead of one value as $_POST variable, and you can then loop over this array by using
$products = $_POST[product];
$quants = $_POST[quant];
for ($i =0; $i < count($products); $i++){
echo $products[$i]; //echo one product
echo $quants[$i]; //echo one quantity
//etc..
}

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 get post value in pagination

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.

Categories