Page 1 of 1

PHP counter display

Posted: Mon Sep 01, 2014 7:50 am
by terrenuit
Hi,
I want to create a quantity counter, but it doesn't work,
the error is in header.php line 3 ($res=$_POST['qty'];): Undefined index: qty in C:\wampserver32\www\sale\head.php on line 3
anyone can help me ?
index.php

Code: Select all

<?php      
 <?php require_once("header.php")  ?>
       $qty=0;
       $totalprice=0;
       foreach($listproduit as $produit) {  
	   $subqty=$produit['quantity'];
       $subtotal=$produit['quantity']*$produit['price'];
       $totalprice+=$subtotal;
	   $qty+=$subqty;	   
       ?> 
       <form action="header.php" method="post">  
        <input type="hidden" name="qty" value="$qty"/>  
        </form>
header.php

Code: Select all

<?php
session_start();	
$res=$_POST['qty'];
    echo "counter:$res"; 
?>

Re: PHP counter display

Posted: Mon Sep 01, 2014 8:31 am
by Celauran
You're assuming a form has been posted. You want to check instead.

Code: Select all

if (isset($_POST['qty'])) {
	$res = $_POST['qty'];
	echo "counter:$res";
}
I also noticed the form on index.php has no submit button.

Finally, index.php seems to have the first two lines in reversed order. It should read

Code: Select all

<?php require_once 'foo'; ?>
<?php
Alternately, you can simply leave the tag open after the require line and remove the next opening tag entirely.

Re: PHP counter display

Posted: Mon Sep 01, 2014 9:40 am
by terrenuit
what is "foo" ?

if I use your script:
it is no good
if (isset($_POST['qty'])) {
$res = $_POST['qty'];
echo "counter:$res";
}

Re: PHP counter display

Posted: Mon Sep 01, 2014 9:45 am
by Celauran
I couldn't remember the name of the file you were requiring, so I just used some dummy text. How do you mean my script is no good? I posted a number of potential errors in the code you had originally posted. Have they all been addressed? What's happening with your script now? What errors are you encountering?

Re: PHP counter display

Posted: Mon Sep 01, 2014 10:25 am
by terrenuit
with your method, the error is:
Parse error: syntax error, unexpected '{' in C:\wampserver32\www\sale\header.php on line 3
which mean's : (isset($_POST['qty'])) {

Re: PHP counter display

Posted: Mon Sep 01, 2014 10:28 am
by Celauran
I don't see anything wrong with that line. Please also post the lines above it.

Re: PHP counter display

Posted: Mon Sep 01, 2014 10:34 am
by terrenuit
I changed my header.php script:
it is better, now, I have the counter display, but it is always in "0",
before, there was no counter output

<?php
session_start();
if ($_POST) {
$res=$_POST['qty'];
}
else { $res=0; }
echo "counter:$res";
?>

Re: PHP counter display

Posted: Mon Sep 01, 2014 10:37 am
by Celauran
That's probably because there's currently no mechanism for submitting your form, so $_POST is going to be an empty array.

Re: PHP counter display

Posted: Mon Sep 01, 2014 12:48 pm
by terrenuit
when I put value=3, the counter is always "0"

<form action="header.php" method="post">
<input type="hidden" name="qty" value="3"/>
</form>

Re: PHP counter display

Posted: Mon Sep 01, 2014 2:47 pm
by Celauran
Because you've created the form but never actually submitted it.

Re: PHP counter display

Posted: Tue Sep 02, 2014 1:40 am
by terrenuit
do you know any method that I can send the variable $qty to header.php without using submitted form ?

Re: PHP counter display

Posted: Tue Sep 02, 2014 6:48 am
by Celauran
Where is the value coming from and what is the goal here? If it's stored in a database, for example, you could just query it on load.

Re: PHP counter display

Posted: Tue Sep 02, 2014 7:40 am
by terrenuit
the variable $qty is the client who want to buy the quantity( for each item),
i want to display this quantity,
now, I know I have to use the "public function count()"
I am Learning it,
do you have any idea ?

Re: PHP counter display

Posted: Tue Sep 02, 2014 8:13 am
by Celauran
I'm not sure you'd need count(). That will return the number of elements in an array. You seem to already have handled that when you iterate over $listproduit. Looking at the code, $qty probably contains the correct value and the issue is solely one of display. Does that sound correct?

Re: PHP counter display

Posted: Tue Sep 02, 2014 1:41 pm
by terrenuit
using submitted form is impossible to realise my program,
I saw a tutorial in "public function count()",
I am Learning it I wish I could arrive
thank you !