#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Toronto,Ontario,Canada
    Posts
    324
    Rep Power
    15

    PHP in a javascript function?


    Hi All,

    I;m hoping someone can put me on a clear path of how to do what I am asking here.

    I Have a shopping cart... and in this shopping cart I want to feature some items in a pop_up window when the user is ready to check out - if they are not already selected.

    For example.. Someone buy a Hockey Stick but don't buy hockey tape..

    when when they go to check out.. I want a pop_up to be launched asking the user if they want to add Hockey tape to their order for onlu $$$.

    For this to happen I need to have an onLoad on the checkout page that calls a javascript function that has PHP code to find out IF a paritcular product is selected to see if the other has been selected too, if it's NOT selected then launch the pop_up.

    Does ANYONE have any idea's as to how this can be accomplished??

    Anyhelp is appreciated!!!!

    thanks all.

    Aaron
  2. #2
  3. Super Genius
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Orlando, Florida
    Posts
    314
    Rep Power
    13
    You would probably have to build an array of "Suggested" items and store it somewhere within your session. When the checkout page is opened have the script pull the info from the array, format it the way you want it to be displayed, and write the dynamic javascript inside the checkout page.
    Sleep is highly overrated.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Toronto,Ontario,Canada
    Posts
    324
    Rep Power
    15
    Originally posted by kingmike
    You would probably have to build an array of "Suggested" items and store it somewhere within your session. When the checkout page is opened have the script pull the info from the array, format it the way you want it to be displayed, and write the dynamic javascript inside the checkout page.

    Soooo.. what you are saying is that it is not possible to do what I am suggestiong above?

    Aaron
  6. #4
  7. Super Genius
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Orlando, Florida
    Posts
    314
    Rep Power
    13
    No that's not what I said. It is very possible. In fact, it wouldn't be hard at all.
    Sleep is highly overrated.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Toronto,Ontario,Canada
    Posts
    324
    Rep Power
    15
    Originally posted by kingmike
    No that's not what I said. It is very possible. In fact, it wouldn't be hard at all.
    Ok then.. I need an example of how to accomplish this.

    I've always understood that PHP and Javascript cannot interact (with Db functions) except that PHP can output Javascript that can be run on the client machine (because Java is client side and PHP is server side).

    Do you have an idea as to how to accomplish this?

    Again.. any help/ideas are appreciated.

    Thanks.
  10. #6
  11. Super Genius
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Orlando, Florida
    Posts
    314
    Rep Power
    13
    >> Ok then.. I need an example of how to accomplish this.

    I'm not going to write your code unless you want to pay me for my time spent on the project.

    >> I've always understood that PHP and Javascript cannot interact (with Db functions) except that PHP can output Javascript that can be run on the client machine (because Java is client side and PHP is server side).

    You are absolutely correct. That is why I said that PHP will need to write the dynamic javascript based upon the values inside of the suggested array.

    PHP is a server-side technology, however, it's output is mostly client-side. This allows PHP to write client side code but not the other way around.
    Sleep is highly overrated.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Toronto,Ontario,Canada
    Posts
    324
    Rep Power
    15
    Originally posted by kingmike
    >> Ok then.. I need an example of how to accomplish this.

    I'm not going to write your code unless you want to pay me for my time spent on the project.

    >> I've always understood that PHP and Javascript cannot interact (with Db functions) except that PHP can output Javascript that can be run on the client machine (because Java is client side and PHP is server side).

    You are absolutely correct. That is why I said that PHP will need to write the dynamic javascript based upon the values inside of the suggested array.

    PHP is a server-side technology, however, it's output is mostly client-side. This allows PHP to write client side code but not the other way around.
    I am not looking for you to write code FOR me.

    I am SIMPLY looking for a kick start to a problem I do not know how to tackle but thanks for the option you did supply - I'll take a look into it.
  14. #8
  15. Super Genius
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Orlando, Florida
    Posts
    314
    Rep Power
    13
    I think you misinterperated my reply thinking that I meant for the JavaScript to have the PHP code within the function when that wasn't what I meant at all.

    Here's another suggestion:

    During checkout, have PHP search through all items in the shopping basket and compare the items with the suggested items and build an array of suggested items. If any of the suggested items are in the basket then have it remove that element from the array.

    Next have PHP loop through the array and write the onLoad script.
    Sleep is highly overrated.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Toronto,Ontario,Canada
    Posts
    324
    Rep Power
    15
    Ok...

    lemme see if I understand what you mean here..

    I'll try to speaking in english as opposed to code


    Before and header output...

    check all the items currently in the customer's cart..

    check these against items against the suggested items.

    Build an $ids (product ids) for each suggested item that is not in the users cart ($ids=1,2,3).

    In the <body> tag add php to check that $ids exists (if $ids != "") and if so build the Onload (with the $ids) function to open a pop_up page.

    In the pop_up page.. explode the $ids to call the products as I want them displayed with the ability to add to the shopping cart which then refreshes the parent page with the new items.

    Is this how you were thinking?

    Thanks.

    Aaron
  18. #10
  19. PHP Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    England
    Posts
    163
    Rep Power
    12

    Talking


    PHP Code:
    <?PHP

    function alert($what)
    {

        if (
    $what == 'yep')
        {
            
    ?><SCRIPT>alert('Aye') </SCRIPT><?
        
    }
        else
        {
            
    ?><SCRIPT>alert('Nah') </SCRIPT><?
        
    }

    }

    alert('yep');

    ?>
    or

    PHP Code:
    <?
        $what 
    'hi there';
    ?>

    <SCRIPT>

    function see()
    {
        alert('<? print $what ?>');
    }

    see();

    </SCRIPT>
    Hope this helps.
    Last edited by andywhitt; September 10th, 2002 at 12:32 PM.
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    New Jersey
    Posts
    32
    Rep Power
    13
    I had a similar need on a project. I used PHP to write a look-up array in JS from a PHP array of items upon loading the page, then my JS checked what was already ordered/selected against the array.

    Convert a PHP array to a JS array,

    PHP Code:
    function phpArray2JSArray$phpArray$name )
      {  
      
    $text "";  
      
    $tags "\n<script language=\"JavaScript\" type=\"text/javascript\">\n";
      
    $declare "var " $name " = new Array();\n";
      
    $close "\n</script>\n";
      
    $len count($phpArray);
     
        for(
    $i 0$i $len; ++$i )
        {
            
    $text .= $name "[" $i "] = '"$phpArray[$i][0]  . "'; \n";
        } 
       
    $tags .= $declare;
       
    $tags .= $text;
       
    $tags .= $close;
        return 
    $tags;
      }


      
    $jsArray phpArray2JSArray($itemArray$arrayName );
    echo 
    $jsArray
    Eviscerate all Tele-Tubbies!!!
    PHP 4.2.xx as module w/ Apache2 on Windows - how to
    NetCaptor - another browser


    "The problem with designing a system that's absolutely foolproof is that one invariably underestimates the ingenuity of absolute fools" - Douglas Adams RIP...
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Toronto,Ontario,Canada
    Posts
    324
    Rep Power
    15
    Hey All,

    Thanks for the tips.. but I think I'll go with the concept that I along with Mike's comments came up with.

    My only problem now is that I don't want the damn pop-up opening every single time the check out page refreshes is by chance someone DOES decide to add one of the suggested items to their shopping cart.

    Thanks again and perhaps I'll post the working code back here once I am done to show how it could work for those non programming genusis like myself.

    Peace.

    Aaron
  24. #13
  25. Super Genius
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Orlando, Florida
    Posts
    314
    Rep Power
    13
    To avoid a reoccuring popup window try using cookies.
    Sleep is highly overrated.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Toronto,Ontario,Canada
    Posts
    324
    Rep Power
    15

    Re: Re: PHP in a javascript function?


    Originally posted by bapolis


    I can't stand sites that have features like this! This type of feature is more of an annoying advertisement, then actual help - especially when a pop-up window is involved. If you want to *help* your customers, I suggest putting "related items" links in the item page for the hockey stick. That is much more acceptable (and useful). Just my ideas...
    I tend to agree with you and it is STILL something I am considering.

    I just needed to workout the logistic behind the code beforehand.

    I don't agree with your placement, however. I think it would be better served as an "add to cart" link on a Shopping Cart page.

    Just my thoughts.
  28. #15
  29. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    New Jersey
    Posts
    32
    Rep Power
    13
    One point to consider...more and more people are using pop-up killer software, and it's pretty certain that the next major relase of IE and Netscape will employ some sort of pop-up killer. I believe Mozilla already does, and I know Netcaptor does.

    So to be 100% certain all your customers see the data you want them to, you should probably nix the pop-up idea.

    Just a thought...

    PAS
    Eviscerate all Tele-Tubbies!!!
    PHP 4.2.xx as module w/ Apache2 on Windows - how to
    NetCaptor - another browser


    "The problem with designing a system that's absolutely foolproof is that one invariably underestimates the ingenuity of absolute fools" - Douglas Adams RIP...

IMN logo majestic logo threadwatch logo seochat tools logo