3

CoffeeNoticeError

OK I tried to look through possible duplicates but didn't help

Now , I have a coffee website mainly built in HTML 4 and CSS3, JavaScript and a bit of php. What I did is on a page named coffee.html I created a picture. When I click on it a cookie is created.

I have 8 such images on one page like coffee.html and I have 4 such pages.

That means I can create 8 x 4 = 32 cookies.

On orders page where I can see what are the items I ordered. I used

<?php 
    if($_COOKIE['cup1'])
        {echo "<img src='img//coffee69.png' width='10%' onClick='erasec1()'>";} 

    if($_COOKIE['cup2'])
        {echo "<img src='img//coffee131.png' width='10%'>";} 
    if($_COOKIE['cup3'])
        {echo "<img src='img//hot drink64.png' width='10%'>";} 
    if($_COOKIE['cup4'])
        {echo "<img src='img//caffeine1.png' width='10%'>";}

    if($_COOKIE['cup5'])
        {echo "<img src='img//tea24.png' width='10%' onClick='erasec1()'>";} 
    if($_COOKIE['cup6'])
        {echo "<img src='img//coffee20.png' width='10%'>";} 
    if($_COOKIE['cup7'])
        {echo "<img src='img//cup31.png' width='10%'>";} 
    if($_COOKIE['cup8'])
        {echo "<img src='img//coffee34.png' width='10%'>";}

 ?>

This is only for first 8 cookies. like that I will have 24 more cookies. So my Question is

From the start I don't have something set for those cookies and that's why it shows error messages ugly error messages

Notice: Undefined index: cup1 in /opt/lampp/htdocs/cafe/orders.php on line 56`

and that sucks.

I will leave a few screen shots to go through. and a download link for my code.

UPDATE

Links to webpage

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
Xuki Team
  • 33
  • 4

4 Answers4

2

How about storing one single cookie instead? You're essentially storing a series of booleans, which can easily be compressed into a single number.

<?php

$images = [
    'coffee69.png', // 0
    'coffee70.png', // 1
    'coffee80.png', // 2
    'tea96.png',    // 3
    'milk34.png',   // 4
    'cola53.png',   // 5
    'etc.png',      // 6
    'more.png'      // 7
];

$number = get(3) | get(5) | get(6); //get me some tea, cola, and etc.

echo $number . "\n\n"; //echos 104

rtn($images, $number);

/* Functions */

function get($num) {
    return pow(2, $num);
}

function rtn($images, $num) {
    $size = sizeof($images);
    for ($i = 0; $i < $size; $i++)
        if ($num & get($i))
            echo '<img src="img/' . $images[$i] . '" width="10%">' . "\n";
}

Output:

104

<img src="img/tea96.png" width="10%">
<img src="img/cola53.png" width="10%">
<img src="img/etc.png" width="10%">

So when you're deciding what cookie to set on the JavaScript side, you can simply use this function to generate your number:

function get(num) {
  return Math.pow(2, num);
}

alert(get(3) | get(5) | get(6)); //echos 104

So now you can simply set your cookie to 104 (or whatever output it gives you) with:

document.cookie.coffee = 104;

Now within PHP, you only need to check one cookie:

if (isset($_COOKIE['coffee'])) {
    $coffee = $_COOKIE['coffee'];

    //and coffee now is some number that can be process with:

    rtn($images, $coffee); //check the above code for reference

}
Dave Chen
  • 10,887
  • 8
  • 39
  • 67
2

Use isset() function to check cookie set or not.

<?php 
    if(isset($_COOKIE['cup1']))
        {echo "<img src='img//coffee69.png' width='10%' onClick='erasec1()'>";} 

    if(isset($_COOKIE['cup2']))
        {echo "<img src='img//coffee131.png' width='10%'>";} 
    if(isset($_COOKIE['cup3']))
        {echo "<img src='img//hot drink64.png' width='10%'>";} 
    if(isset($_COOKIE['cup4']))
        {echo "<img src='img//caffeine1.png' width='10%'>";}

    if(isset($_COOKIE['cup5']))
        {echo "<img src='img//tea24.png' width='10%' onClick='erasec1()'>";} 
    if(isset($_COOKIE['cup6']))
        {echo "<img src='img//coffee20.png' width='10%'>";} 
    if(isset($_COOKIE['cup7']))
        {echo "<img src='img//cup31.png' width='10%'>";} 
    if(isset($_COOKIE['cup8']))
        {echo "<img src='img//coffee34.png' width='10%'>";}

 ?>
Smit Patel
  • 167
  • 1
  • 8
1

use isset statement. It might help. <?php if (isset($_COOKIE['cup1'])){ echo '<img'; } if (!(isset($_COOKIE['cup1']))){ echo '<img src=\'noimage.jpg\'>'; } ?>

Jimmy Canadezo
  • 161
  • 1
  • 10
1

From your question:

From the start I don't have something set for those cookies

Yes you need to use isset() for checking either COOKIE set or not.

Example:

if(isset($_COOKIE['cup1'])) {
     echo "<img src='img//coffee69.png' width='10%' onClick='erasec1()'>";
} 

if(isset($_COOKIE['cup2'])) {
........
devpro
  • 16,184
  • 3
  • 27
  • 38