PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Sep 28, 2020 8:54 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 15 posts ] 
Author Message
 Post subject: PHP counter display
PostPosted: Mon Sep 01, 2014 7:50 am 
Offline
Forum Commoner

Joined: Tue Jul 08, 2014 2:18 pm
Posts: 53
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";
?>


Top
 Profile  
 
 Post subject: Re: PHP counter display
PostPosted: Mon Sep 01, 2014 8:31 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
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.

_________________


Top
 Profile  
 
 Post subject: Re: PHP counter display
PostPosted: Mon Sep 01, 2014 9:40 am 
Offline
Forum Commoner

Joined: Tue Jul 08, 2014 2:18 pm
Posts: 53
what is "foo" ?

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


Top
 Profile  
 
 Post subject: Re: PHP counter display
PostPosted: Mon Sep 01, 2014 9:45 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
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?

_________________


Top
 Profile  
 
 Post subject: Re: PHP counter display
PostPosted: Mon Sep 01, 2014 10:25 am 
Offline
Forum Commoner

Joined: Tue Jul 08, 2014 2:18 pm
Posts: 53
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'])) {


Top
 Profile  
 
 Post subject: Re: PHP counter display
PostPosted: Mon Sep 01, 2014 10:28 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
I don't see anything wrong with that line. Please also post the lines above it.

_________________


Top
 Profile  
 
 Post subject: Re: PHP counter display
PostPosted: Mon Sep 01, 2014 10:34 am 
Offline
Forum Commoner

Joined: Tue Jul 08, 2014 2:18 pm
Posts: 53
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";
?>


Top
 Profile  
 
 Post subject: Re: PHP counter display
PostPosted: Mon Sep 01, 2014 10:37 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
That's probably because there's currently no mechanism for submitting your form, so $_POST is going to be an empty array.

_________________


Top
 Profile  
 
 Post subject: Re: PHP counter display
PostPosted: Mon Sep 01, 2014 12:48 pm 
Offline
Forum Commoner

Joined: Tue Jul 08, 2014 2:18 pm
Posts: 53
when I put value=3, the counter is always "0"

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


Top
 Profile  
 
 Post subject: Re: PHP counter display
PostPosted: Mon Sep 01, 2014 2:47 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
Because you've created the form but never actually submitted it.

_________________


Top
 Profile  
 
 Post subject: Re: PHP counter display
PostPosted: Tue Sep 02, 2014 1:40 am 
Offline
Forum Commoner

Joined: Tue Jul 08, 2014 2:18 pm
Posts: 53
do you know any method that I can send the variable $qty to header.php without using submitted form ?


Top
 Profile  
 
 Post subject: Re: PHP counter display
PostPosted: Tue Sep 02, 2014 6:48 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
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.

_________________


Top
 Profile  
 
 Post subject: Re: PHP counter display
PostPosted: Tue Sep 02, 2014 7:40 am 
Offline
Forum Commoner

Joined: Tue Jul 08, 2014 2:18 pm
Posts: 53
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 ?


Top
 Profile  
 
 Post subject: Re: PHP counter display
PostPosted: Tue Sep 02, 2014 8:13 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
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?

_________________


Top
 Profile  
 
 Post subject: Re: PHP counter display
PostPosted: Tue Sep 02, 2014 1:41 pm 
Offline
Forum Commoner

Joined: Tue Jul 08, 2014 2:18 pm
Posts: 53
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 !


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 25 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group