[英]How to get value from url while using Jquery script?
code: 码:
$(document).ready(function(){
$(".uni_type").click(function(){
uni_type = this.id;
location.href = "filter-colleges.php?uni_type="+uni_type;
});
});
html code: html代码:
<ul>
<li class="uni_type" id="central university">Central University</li>
<li class="uni_type" id="state university">State University</li>
<li class="uni_type" id="private university">Private University</li>
<li class="uni_type" id="deemed university">Deemed University</li>
<li class="uni_type" id="open university">Open University</li>
<li class="uni_type" id="autonomus">Autonomus Institute</li>
</ul>
filter-college.php filter-college.php
<?php
include('conn.php');
$uni_type = $_GET['id'];
echo '<script>alert('.$uni_type.')</script>';
?>
In this code I want to get uni_type
value from the URL but when I click on any list item I'm moved to next page (ie filter-colleges.php ) and given an alert to $uni_type
on filter-colleges.php page, but no value is displayed in the alert box. 在这段代码中,我想从URL中获取
uni_type
值,但是当我单击任何列表项时,我将移至下一页(即filter-colleges.php ),并在filter-colleges.php页面上向$uni_type
发出警报,但是警报框中没有显示任何值。 How can I get value from URL or alert $uni_type
? 如何从URL或
$uni_type
警报中获取价值?
You're fetching the wrong information in your $_GET
: 您在
$_GET
获取了错误的信息:
<?php
include('conn.php');
$uni_type = $_GET['uni_type'];
echo '<script>alert('.$uni_type.')</script>';
?>
Seems you are getting wrong id
of <li>
.Use $(this).attr('id')
to get 'id' attribute of <li>
. 似乎您弄错了
<li>
id
。使用$(this).attr('id')
获得<li>
'id'属性。 Follow it - 跟着它 -
<script>
$(document).ready(function(){
$(".uni_type").click(function(){
var uni_type = $(this).attr('id');
location.href = "filter-colleges.php?uni_type="+uni_type;
});
});
</script>
And also you have put wrong id ( $_GET['id']
instead of $_GET['uni_type']
) in your php code. 而且您在PHP代码中输入了错误的ID(
$_GET['id']
而不是$_GET['uni_type']
)。 See it - 看见 -
<?php
include('conn.php');
$uni_type = $_GET['uni_type'];
echo '<script>alert('.$uni_type.')</script>';
?>
And dont keep space in id
, class
or other attribute of tag. 并且不要在
id
, class
或tag的其他属性中保留空间。 For best practice. 最佳实践。 It can cause unwanted output.
可能导致不必要的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.