1

I have a website which has OAuth and Google Sign-In .

What i want to do is select from selectmenu when client enter the site. So every client will see different filtered page according to their email adresses.

I can already create a session and write client name on screen.

 <?php if ($_SESSION["email"] == "xxx@gmail.com") { ?> 
      <h2>Thank you <?php echo $_SESSION["name"] ?> </h2>
        <?php }   ?>

I have below selectMenu with dimension country values like ( a,b,c,d,e ) I want when email adresss xxx is signed in he/she have to see only a country , and when yyy email adresss is signed in he/she have to see only b country , so on ...

Function have to select automatically and display it on screen.


selectField = dc.selectMenu('#country')
                        .dimension(mostSold)
                        .title(function (d) { return d.key })
                        .promptText('Which country')
                        .group(mostSoldGroup)

So far I found below way but it doesn't work and give error.

  <?php if ($_SESSION["email"] == "xxx@gmail.com") { ?> 
      <?php selectField.replaceFilter([["a"]]).redrawGroup(); ?>
        <?php }   ?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
mrtc
  • 143
  • 2
  • 13
  • *"but it doesn't work and give error."* - Being what exactly? – Funk Forty Niner Nov 16 '19 at 16:58
  • You're mixing js with php; you can't do that. – Funk Forty Niner Nov 16 '19 at 16:59
  • I’m not sure that it’s right to close this question as a duplicate. But @FunkFortyNiner is right that you can’t mix PHP and JS since the PHP will be executed on the server before any JS is executed on the client (browser). You could use PHP to conditionally generate some JS code, or initialize some JS variables, to do what you want. – Gordon Nov 18 '19 at 02:19
  • @Gordon Thanks for pinging me here Gordon. I chose the first one as the original duplicate and added a few more. Did the first one not answer the question? Help me to understand :) If I did wrong, I'll reopen. – Funk Forty Niner Nov 18 '19 at 02:22
  • @FunkFortyNiner, I don’t think that question is related. The querent is trying to understand how to initialize a JavaScript library based on some state which is known during page generation in PHP. So it’s a question of how to generate the right JS code in PHP. I think the intent is to conditionally include the line of JS when the PHP expression evaluates true. I don’t know PHP well enough to answer this. – Gordon Nov 18 '19 at 02:39
  • Alright @Gordon I understand. I reopened. Edit: It would have been nice if they would have answered [my question](https://stackoverflow.com/questions/58893074/php-select-from-dc-selectmenu-according-to-email-address-of-client?noredirect=1#comment104051289_58893074). – Funk Forty Niner Nov 18 '19 at 02:40
  • Agree, including the error message is pretty important. Presumably it’s something like “selectField is undefined” since it would try to evaluate JS as PHP. – Gordon Nov 18 '19 at 02:51
  • It gives error : Warning: Use of undefined constant selectField - assumed 'selectField' (this will throw an Error in a future version of PHP) in.... (the root of document) @FunkFortyNiner – mrtc Nov 18 '19 at 18:34

1 Answers1

1

As @Gordon and @FunkFortyNiner explained, you are mixing two different languages (PHP and js).

To make it easier, start to limit how much stuff need to move between php and js, by writing (in php) a js variable that you can then read later in a "clean" js code

 <?php 
 if ($_SESSION["email"] == "xxx@gmail.com") {  
   echo "Thank you ".$_SESSION["name"];
   echo "<script>var countryFilter='a';</script>";
 } 
 ?>

and further down in your page (or better separate js file)

 <script>

 selectField = dc.selectMenu('#country')
                    .dimension(mostSold)
                    .title(function (d) { return d.key })
                    .promptText('Which country')
                    .group(mostSoldGroup)

  if (countryFilter) 
    selectField.replaceFilter([[countryFilter]]);

 </script>

General advice: avoid mixing different languages (php+html+js): it's way harder to read, and way easier to write insecure code.

Gordon
  • 19,811
  • 4
  • 36
  • 74
Xavier
  • 1,157
  • 9
  • 29
  • Hello @Xavier . That code worked PERFECTLY !!. Thank you for that. But now when I click on Reset All (a href="javascript:dc.filterAll();) again all other countries values come to screen. So when it comes to giving privileges to see different filter to each client I guess I need to handle it from Database ? If you know other methods on this subjects I will look into them. – mrtc Nov 18 '19 at 18:39
  • After going through some research Is it posible to make csv file manipulation with PHP ? I mean after getting csv data into (d3.csv, "example.csv") and before making all dimension stuff can i filter this csv file according to each client ? – mrtc Nov 18 '19 at 20:23
  • These are separate questions, please ask them as new questions. But if you are trying to implement privilege access on the js side, you are on the wrong track, because they will still have downloaded the full list in csv, no matter what filters are applied later – Xavier Nov 19 '19 at 05:46