Jump to content

noob here need some help


craigafer

Recommended Posts

well, here's my problem, im new to php, i currently working on this mini online shop the picture will explain it

phpproblem.jpg

well, i've managed to create the receipt per shop :qft: but i cant do the receipt of the overall shopping here is my code:

<html>
<head>
<title>My Shop 4</title>
<script type="text/javascript">
function gohome()
{
window.open("receipt.php","_self");
}
function changeshop()
{
window.open("home.html","_self");
}
</script>
</head>
<body>
<form action="" method="POST">
<input type="checkbox" name="prod1" id="prod1"\ value=10>Product 1		10.00php<br>
<input type="checkbox" name="prod2" id="prod2"\ value=20>Product 2		20.00php<br>
<input type="checkbox" name="prod3" id="prod3"\ value=30>Product 3		30.00php<br>
<input type="checkbox" name="prod4" id="prod4"\ value=40>Product 4		40.00php<br>
<input type="checkbox" name="prod5" id="prod5"\ value=50>Product 5		50.00php<br>
<input type="submit" name="submit" id="submit" value="Buy!!"><br>
<?php
$det = 4;
require("../Labphp/lab.php");
shop($det);
?>

</form>

</body>
</html>

 

and here's the php part

<?php
$total = 0;
$total3 = 0;
$total4 = 0;
$recent ="HAHAHA";
global $total,$total3,$total4,$recent;

?>
<?php

function shop($num)
{	
 $value1 = $_POST['prod1'];
 $value2 = $_POST['prod2'];
 $value3 = $_POST['prod3'];
 $value4 = $_POST['prod4'];
 $value5 = $_POST['prod5'];
 $sum=0;

$sum = $value1 + $value2 + $value3 + $value4 + $value5;

if($sum!=0)
{
echo "Selected Products<br>";
echo "Product Name &nbsp&nbsp&nbsp&nbsp&nbsp Price<br>";
if($value1==true)
{
echo "Product 1&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 10.php<br>";
}
if($value2==true)
{
echo "Product 2&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 20.php<br>";
}
if($value3==true)
{
echo "Product 3&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 30.php<br>";
}
if($value4==true)
{
echo "Product 4&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 40.php<br>";
}
if($value5==true)
{
echo "Product 5&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 50.php<br>";
}
echo "Total&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp $sum php<br>";

if($num==4)
	{
		$total4=$sum;
		$recent.="Shop 4 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp $total4 php<br>";
	}
else if($num==3)
	{
		$total3=$sum;
		$recent.="Shop 3 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp $total3 php<br>";
	}
$total = $total4 + $total3;
echo "$recent &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp $total";
echo "<input type='button' name='go' id='go' value='Choose Another Shop' onclick='javascript:changeshop();'>";
echo "<input type='button' name='end' id='end' value='End Shopping' onclick='javascript:gohome();'>";
}
}


function getshopping()
{
	return $recent;
}

?>

 

the receipt code

<?php
require("../Labphp/lab.php");
?>
<html>
<head>
<title>Official Receipt</title>
</head>
<body
<?php
echo $_REQUEST["'$recent'"];
?>
</body>
</html>

 

 

Link to comment
Share on other sites

Hi there,

<?php
//change this to without quotes & change from $_REQUEST to POST or GET
if(isset($_POST[$recent])){
echo $_POST[$recent];
}
else{
echo "No receipt has been set to print!";
}
?>

 

This is why it's not printing anything! Change the request to get or post, whichever you are using as there are security issues surrounding $_REQUEST at present, also you need to check to see if it's set before it's requested to screen!

 

And secondly, the function getshopping() doesn't have anything passed into it, so returning $recent; from it doesn't actually do anything so far as I can tell, you need to pass something as a parameter into the function to be able to return something from it, unless the function (by design) does all it's functionality without parameters being passed to it eg:-

 

function Bark(){
return $myDog = "My dog wants feeding!";
}

echo Bark();

//will output:-
My dog wants feeding!

 

Cheers,

Rw

Link to comment
Share on other sites

<?php
require("../Labphp/lab.php");
?>
<html>
<head>
<title>Official Receipt</title>
</head>
<body
<?php
//change this to without quotes & change from $_REQUEST to POST or GET
if(isset($_POST[$recent])){
echo $_POST[$recent];
}
else{
echo "No receipt has been set to print!";
}
?>
</body>
</html>

]

ive tried this but nothing prints too  :shrug:

the two shops and receipts are different file though

 

i got another question

if(isset($_POST[$recent])) <--- whats this for? i always see this but idk whats this for  :-[

Link to comment
Share on other sites

Hi there,

i got another question

if(isset($_POST[$recent])) <--- whats this for? i always see this but idk whats this for

 

Simply means:-

if(isset($_POST[$recent]))

 

If it is set (the variable/global/array that you are referencing) then do something with it else do something else with it. See this from the manual

 

I have no idea then why it's not working, but it seems to me like you need to see how you have your functions interacting and find out where $recent should be being populated...

 

Cheers,

Rw

Link to comment
Share on other sites

well it worked with sessions :D

codes for shop4 (codes for shop3 is the same i just edited the $det to 3)

<?php session_start(); ?>
<html>
<head>
<title>My Shop 4</title>
<script type="text/javascript">
function gohome()
{
window.open("receipt.php","_self");
}
function changeshop()
{
window.open("home.html","_self");
}
</script>
</head>
<body>
<form action="" method="POST">
<input type="checkbox" name="prod1" id="prod1"\ value=10>Product 1		10.00php<br>
<input type="checkbox" name="prod2" id="prod2"\ value=20>Product 2		20.00php<br>
<input type="checkbox" name="prod3" id="prod3"\ value=30>Product 3		30.00php<br>
<input type="checkbox" name="prod4" id="prod4"\ value=40>Product 4		40.00php<br>
<input type="checkbox" name="prod5" id="prod5"\ value=50>Product 5		50.00php<br>
<input type="submit" name="submit" id="submit" value="Buy!!"><br>
<?php
$det = 4;
require("../Labphp/lab.php");
shop($det);
?>

</form>

</body>
</html>

 

php codes

<?php session_start(); ?>
<?php
$total = 0;
$total3 = 0;
$total4 = 0;
$recent ="";
global $total,$total3,$total4,$recent;

?>
<?php

function shop($num)
{	
 $value1 = $_POST['prod1'];
 $value2 = $_POST['prod2'];
 $value3 = $_POST['prod3'];
 $value4 = $_POST['prod4'];
 $value5 = $_POST['prod5'];
 $sum=0;

$sum = $value1 + $value2 + $value3 + $value4 + $value5;

if($sum!=0)
{
echo "Selected Products<br>";
echo "Product Name &nbsp&nbsp&nbsp&nbsp&nbsp Price<br>";
if($value1==true)
{
echo "Product 1&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 10.php<br>";
}
if($value2==true)
{
echo "Product 2&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 20.php<br>";
}
if($value3==true)
{
echo "Product 3&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 30.php<br>";
}
if($value4==true)
{
echo "Product 4&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 40.php<br>";
}
if($value5==true)
{
echo "Product 5&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 50.php<br>";
}
echo "Total&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp $sum php<br>";

if($num==4)
	{
		$total4=$sum;
		$recent.="Shop 4 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp $total4 php<br>";
	}
else if($num==3)
	{
		$total3=$sum;
		$recent .="Shop 3 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp $total3 php<br>";
	}
$total = $total4 + $total3;
$_SESSION['overall'] += $total;	
$_SESSION['history'] .= $recent;
echo "<input type='button' name='go' id='go' value='Choose Another Shop' onclick='javascript:changeshop();'>";
echo "<input type='button' name='end' id='end' value='End Shopping' onclick='javascript:gohome();'>";
}
}

?>

 

codes for receipt

<?php session_start(); ?>
<?php
require("../Labphp/lab.php");
echo "<br>".$_SESSION['history'];
echo "Total ".$_SESSION['overall'];
?>

<html>
<head>
<title>Official Receipt</title>
</head>
<body>
</body>
</html>

 

its working nao!! thnx btw :D

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.