Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I know that both php and javascript work with cookies and you can create a cookie in php and use that one in javascript.

now I have this code
// csv code werkt wel tot ik de set cookie gebruik om
      $csv_file = "filename.csv";

      // Open the file for reading
      $file_handle = fopen($csv_file, "r");

      // Read the contents of the file
      while (($row_data = fgetcsv($file_handle, 1000, ';')) !== FALSE) {
          if (empty($Toevoeging)){
            if ($row_data[0] == $Zipcode && $row_data[1] == $Housenumber ) {
              // User input found in the CSV
              // $_SESSION['input_found'] = "user input found";
              // echo $_SESSION['input_found'];
              $user_input = "User Input Found";
              setcookie("user_input", $user_input);
              break 1;
             }
          }else{

            if ($row_data[0] == $Zipcode && $row_data[1] == $Housenumber && $row_data[2] == $Toevoeging) {
              // User input found in the CSV
              // $user_input = "";
                $user_input = " Found";
                setcookie("user_input", $user_input);
                break 1;
              }else{
                $user_input = "User Input NOT Found";
                setcookie("user_input", $user_input);
                break 1;
              }
          }

      }
      // Close the file handle
      fclose($file_handle);


and I know that it works because I used it without cookies and just hard coded it in there for testing perposes (not good at reading csvs) however it's not working anymore. when I show the var in my table
javascript:
var obj = JSON.parse(msg);
            var match = document.cookie.match(new RegExp('user_input=([^;]+)'));

            var input = decodeURIComponent(match[1]);

             // var noSpecialChars = input.replace(/[^A-Za-z]+/g, '');
             var table = $('#displayTable');
             //reset de table zodat er nieuwe td in kan komen.
         $('#displayTable > td').remove();

             for (var i = 0; i < Object.keys(obj[0].AvailableSupplier.AvailableSpeeds.AvailableSpeed).length; i++) {
                 //tabel append is row toevoegen aan table
                 table.append("<tr>");
                 table.append("<td>" + input + "</td>");
                 table.append("<td>" + obj[0].AvailableSupplier.AvailableSpeeds.AvailableSpeed[i].Technology + "</td>");
                 table.append("<td>" + obj[0].AvailableSupplier.AvailableSpeeds.AvailableSpeed[i].Availability + "</td>");
                 table.append("<td>" + obj[0].AvailableSupplier.AvailableSpeeds.AvailableSpeed[i].Description + "</td>");
                 table.append("</tr>");

             }


it only shows me user input found.
but this is wrong.
the csv check is supposed to check if the entered zipcode and housenumber is in the csv. if they are the user input is found
if the zipcode housenumber and extra letter (don't know the english word sorry example would be: zipcode: 7492 SI Housenumber:34A).

and if they arent in there the user input is not found. this code used to work when I just said echo "user input found" and so on.
how do I fix this

What I have tried:

checkt the internet about cookies. but don't really know what to look up because this is supposed to work.
Posted
Updated 3-May-23 2:59am
v2

1 solution

Hello !


you can get the value 'user_input' of the cookie with

// JS :
var people = Document.cookie['user_input'] ;
// cookie works with 'key => value'.
// and like Array too, associative Arrays or indexed Array

var people = Document.cookie[0] ;


// Php : retrieve the cookie, and its value :

$_COOKIE ;
$_COOKIE["user_input"] ;
$_COOKIE[0] ;

if( !empty($_COOKIE) ){ .....;}

if( !empty(["user_input"]) ){ .....;}

// in Php $_COOKIE is a global var, reachable all along the php code. It's an Array.
 
Share this answer
 
Comments
Rebecca2002 4-May-23 4:09am    
I kinda need it the other way around. javascript is supposed to retrieve the cookie. because I'm doing a csv check in php and that response I want to show in javascript. for this I need set_cookie instead right? because the csv check isn't in a form.
[no name] 4-May-23 4:19am    
hello,

to retrieve cookie by JS after a PHP feedback,
you can write :
//from php side, you send a JS script, it will add in the DOM of your page.
echo (" <script> alert(document.cookie); my_cookie_function() ; </script> ") ;  // this way you'll have a firing function after php work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900