开发者

javascript variables to html?

here is the case:

ive got 2 lists on my website. country and region. when the user first enter the options are ALL and ALL. then he picks a country and a region. and i use jquery bbq to hash the url with different variables for remembering the unique ajax rendered pages. for example:

webpage#country=1&region=3
webpage#country=2&region=3

now i want the user to be able to send this url to another person and the browser will display the same html select lists.

so i came up with a solution:

first i grab the hash with jquery and turn them into cookies. then i've got the following html code:

<select>
<?php
    while($row = mysqli_fetch_assoc($countries))
    {
                if($row['id'] == $_COOKIE['country'])
                {
                    echo "<option value='" . $row['id'] . "' selected>" . $row['name'] . "</option>";
                }
                else
                {
                    echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
                }

    }
?>
</select>

this will show the option that the user had picked from the list. and the same thi开发者_JAVA百科ngs goes for the region select list.

but the problem is that it doesnt work in firefox. and in safari its too slow to be shown. the cookies are created after the html is shown. so i have to refresh the page a second time.

it just seems like its not a very good solution. and i dont want the html to render all the options and then afterwards change to the picked option with jquery. then the user sees the lists suddenly change and its ugly.

what are my options here? i think this issue has crossed a lot of ajax programmers mind.

(the site got more than 2 lists so i really want this to work)


I don't think cookies are moving you in the right direction. Cookies are for remembering something AFTER the first visit. Not before.

No matter what, if you code this on the client side using javascript or anything else, some one will have a connection/browser slow enough that they will see the options load one way and then get switched. It sounds more like you want the server to dynamically generate the options list. Make it so the generated links are

webpage?country=1&region=3
webpage?country=2&region=3

Then the server knows what was pre-selected and can generate the initial version with the right values pre-selected. But if the user comes in without passing in values for country/region, then just generate the page as you normally would.


Not sure with your exact requirements, but instead of cookies you can use session variables.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜