PHP Developers Network
http://forums.devnetwork.net/

PHP counter display
http://forums.devnetwork.net/viewtopic.php?f=1&t=140157
Page 1 of 1

Author:  terrenuit [ Mon Sep 01, 2014 7:50 am ]
Post subject:  PHP counter display

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
Syntax: [ Download ] [ Hide ]
<?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
Syntax: [ Download ] [ Hide ]
<?php
session_start();       
$res=$_POST['qty'];
    echo "counter:$res";
?>

Author:  Celauran [ Mon Sep 01, 2014 8:31 am ]
Post subject:  Re: PHP counter display

You're assuming a form has been posted. You want to check instead.

Syntax: [ Download ] [ Hide ]
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
Syntax: [ Download ] [ Hide ]
<?php require_once 'foo'; ?>
<?php


Alternately, you can simply leave the tag open after the require line and remove the next opening tag entirely.

Author:  terrenuit [ Mon Sep 01, 2014 9:40 am ]
Post subject:  Re: PHP counter display

what is "foo" ?

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

Author:  Celauran [ Mon Sep 01, 2014 9:45 am ]
Post subject:  Re: PHP counter display

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?

Author:  terrenuit [ Mon Sep 01, 2014 10:25 am ]
Post subject:  Re: PHP counter display

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'])) {

Author:  Celauran [ Mon Sep 01, 2014 10:28 am ]
Post subject:  Re: PHP counter display

I don't see anything wrong with that line. Please also post the lines above it.

Author:  terrenuit [ Mon Sep 01, 2014 10:34 am ]
Post subject:  Re: PHP counter display

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";
?>

Author:  Celauran [ Mon Sep 01, 2014 10:37 am ]
Post subject:  Re: PHP counter display

That's probably because there's currently no mechanism for submitting your form, so $_POST is going to be an empty array.

Author:  terrenuit [ Mon Sep 01, 2014 12:48 pm ]
Post subject:  Re: PHP counter display

when I put value=3, the counter is always "0"

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

Author:  Celauran [ Mon Sep 01, 2014 2:47 pm ]
Post subject:  Re: PHP counter display

Because you've created the form but never actually submitted it.

Author:  terrenuit [ Tue Sep 02, 2014 1:40 am ]
Post subject:  Re: PHP counter display

do you know any method that I can send the variable $qty to header.php without using submitted form ?

Author:  Celauran [ Tue Sep 02, 2014 6:48 am ]
Post subject:  Re: PHP counter display

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.

Author:  terrenuit [ Tue Sep 02, 2014 7:40 am ]
Post subject:  Re: PHP counter display

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 ?

Author:  Celauran [ Tue Sep 02, 2014 8:13 am ]
Post subject:  Re: PHP counter display

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?

Author:  terrenuit [ Tue Sep 02, 2014 1:41 pm ]
Post subject:  Re: PHP counter display

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 !

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/