Search code examples
phpajaxdynamichref

dynamic href url based on ajax call


Really not sure how to even go about asking this question but it is something like this.

How can I run some type of ajax(php script on server end) based on user's onclick action and dynamically launch url based on return from ajax and put it in href effectively launching that url?

so I want to say something like

enter code here

<a href="urlYetTobeDeterminedAtTheTimeOfOnClick" onclick=sometfunctionWhichREturnsUrl>

And say I have a php function which returns site:a.com if it's before midnight to 8am, site:b.com if it's 8am to noon and site:c.com if it's noon to midnight. I don't need a exact code but some sort of direction of how this can be arranged.. I have done lot of research on google but I do not see anything so far that I can use or point me to right direction. Can someone please advise?

thank you.


Solution

  • I'd just have the anchor point to the a url that determines the address to go to and then let it call a redirect to the url.

    <a href="script.php">
    

    script.php

    <?php
    $url = get_the_url_function();
    header('Location: '.$url);
    exit;