как разобрать плагин jQuery address - PullRequest
1 голос
/ 28 января 2011

Я использую плагин http://www.asual.com/jquery/address/ и пытаюсь разобрать какой-то URL, вот что я получил:

<script>$(function(){
      $.address.init(function(event) {

                // Initializes the plugin
                $('a').address();

            }).change(function(event) {

 $('a').attr('href').replace(/^#/, '');

    $.ajax({
  url: 'items.php',
  data:"data="+event.value,
  success: function(data) {
    $('div#content').html(data)

      }
   })

});


})</script>

, затем HTML:

<a href="items.php?id=2">link02</a>
<a href="items.php?id=3">link03</a>

тогда я вызываю item.php, который пока имеет только

echo $_GET["data"];  

поэтому после завершения запроса ajax он повторяет:

/items.php?id=2
/items.php?id=3

как я могу разобрать это, чтобы я получил только значения var? это лучше делать на стороне клиента? а также, если мой html href что-то вроде <a href="items.php?id=2&var=jj">link02</a> Адрес jQuery будет игнорировать &var=jj

ура

1 Ответ

2 голосов
/ 28 января 2011

Вы можете использовать parse_url () , чтобы получить строку запроса, а затем parse_str , чтобы получить строку запроса, проанализированную в переменных.

Пример:

<?php

// this is your url
$url = 'items.php?id=2&var=jj';

// you ask parse_url to parse the url and return you only the query string from it
$queryString = parse_url($url, PHP_URL_QUERY);

// parse_str can extract the variables into the global space or can put them into an array
// NOTE: for simplicity no validation is performed but in production you should perform validation
$params = array();
parse_str($queryString, $params);
// you can access the values like thid
echo $params['id']; // will output 2
echo $params['var']; // will output 'jj'
// I prefer this way, because it eliminates the posibility of overwriting another global variable
// with the same name as one of the parameters in the url.

// or you can tell parse_str to extract the variables into the global space
parse_str($queryString);
// or if you want to use the global scope
echo $id; // will output 2
echo $var; // will output 'jj'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...