开发者

Code Igniter - Get Hash Value With window.location.hash and Use in set_value()?

开发者 https://www.devze.com 2023-01-18 21:59 出处:网络
I need to get the hash # value from the current window url with javascript (can\'t get it with开发者_运维技巧 PHP), and then use that to set the value attibute of a form input with CI\'s set_value() f

I need to get the hash # value from the current window url with javascript (can't get it with开发者_运维技巧 PHP), and then use that to set the value attibute of a form input with CI's set_value() function.

Any ideas how I can grab the value in javascript then put it into the PHP function? :-S

Thanks in advance!


I can't quite tell if you fully understand the relationship between Javascript and PHP, so I'm going to assume that you don't. PHP runs on the server and outputs HTML to the browser (along with some headers and other stuff). Once the HTML is sent to the browser, what the user does with your site is out of your control until his browser performs another GET or POST request (or opens a persistent web socket connection). As you mentioned, it's impossible for PHP to outright read the value of the hash, as it's meant to be confined to the browser, accessible only via Javascript. Since your PHP script for that particular request has stopped running and the page data has already been output to the browser, what sense does it make to grab the value using Javascript, send it back to PHP with another http request, use the set_value() function, and then what?

If you use window.location.hash and set the value of your input box equal to it, you will be able to use set_value() when the user submits the form. Take a look at CI's set_value() helper function:

function set_value($field = '', $default = '')
    {
        if (FALSE === ($OBJ =& _get_validation_object()))
        {
            if ( ! isset($_POST[$field]))
            {
                return $default;
            }

            return form_prep($_POST[$field], $field);
        }

        return form_prep($OBJ->set_value($field, $default), $field);
    }

It first checks to see if you've set up form validation object. If you have, it runs the form_prep() function using that object's set_value (presumably to manage any rules you've set up in your form validation. If you haven't, it sees if that field exists in the $_POST array. If it doesn't, it returns the default value. If it does, it returns the value from the $_POST array. So if you a) set up a form validation rule for it and b) give that input box the name you want to use in your set_value() function, you will be able to retrieve the value of the hash in PHP which you have entered into that input box using Javascript.


In Javascript you can achieve this vis hash = window.location.hash;

In PHP you can access it using parse_url()

parse_url() : Return Values

On seriously malformed URLs, parse_url() may return FALSE. Otherwise an associative array is returned, whose components may be (at least one):

    * scheme - e.g. http
    * host
    * port
    * user
    * pass
    * path
    * query - after the question mark ?
    * fragment - after the hashmark #

0

精彩评论

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

关注公众号