通过超链接传递给其他页面的数据正在被切断 [英] Data passed to other pages via hyperlink is being cut off

查看:79
本文介绍了通过超链接传递给其他页面的数据正在被切断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含2 < select> 的表单,第一个select在页面加载时自动填充,第二个select根据选择的选项填充自身在第一次选择。



要做到这一点,无论何时选择的状态发生变化,第一个选定的值将被传递到一个单独的页面,用于填充第二< select>



问题



通过url传递的选定值(在这种情况下为食品和饮料)正在被中途切断,导致将不完整的字符串发送到处理页面第二,导致它无法运行。



确定问题的步骤
我回应了通过url传递的值,只有食物,其余的字符串被切断。我尝试将字符串值替换为食品和饮料,并且整个事情完美地工作,导致我得出结论,由于&符号导致计算机处理部分的字符串字符串作为另一个要通过URL传递的值。但是,因为我没有将它分配给变量,所以它没有被传递。



问题



有没有办法让我在不切断它的情况下传递值?



代码提取:

处理页面 b

 <?PHP 
include(cxn.inc);

$ query = $ cxn-> prepare(SELECT * FROM`BusinessSubCategory` WHERE`BusinessCategory` =:businesscategory);
$ query-> bindValue(:businesscategory,$ _ GET ['category']);
$ query-> execute();
$ count = $ query-> rowCount();
if($ count> 0)
{
echo< option id ='subcategory'value =''>请选择一个子类别< / option>;
while($ result = $ query-> fetch(PDO :: FETCH_ASSOC))
{
$ subcategory = $ result ['BusinessSubCategory'];
echo< option id = $ subcategory value = $ subcategory> $ subcategory< / option>;



{
echo'< option id ='subcategory'value =''>错误,提取查询不运行<选项>中;
}
?>

JQuery代码

  $(document).ready(function(){

$('#BusinessCreateCategory')。load('getbusinesscategory.php');

$('#BusinessCreateCategory')。change(function(){

var category = $('#BusinessCreateCategory')。val();
window.location .href ='getbusinesssubcategory.php?category ='+ category;

});

编辑:尝试encodeURIComponent,但数据没有被编码,因为我可以从处理apge的URL中看到,它在& ;.HOWEVER处截断,如果我手动输入url作为字符串和然后使用encodeURIComponent对其进行编码,它的工作原理非常奇妙。任何人都会对我为何无法编码$('#BusinessCreateCategory')。val();?感谢!



E.gThis works

  var category =Food& Beverages; 
var encoded = encodeURIComponent(category );
wi ndow.location.href = getbusinesssubcategory.php类别=? +编码;

例如,这不是

  var category = $('#BusinessCreateCategory')。val(); 
var encoded = encodeURIComponent(category);
window.location.href ='getbusinesssubcategory.php?category ='+ encoded;

如果有帮助,我想通过网址传递的数据来自我的数据库。 / p>

解决方案

在使用它之前,您需要 encodeURIComponent 类别的值URL。

  $('#BusinessCreateCategory')。change(function(){

var category = $('#BusinessCreateCategory')。val();
var encoded = encodeURIComponent(category);
window.location.href ='getbusinesssubcategory.php?category ='+ encoded;

});

&符号是一个特殊字符,用于填充您尝试传递的URL。对值进行编码应该允许您将其视为单个值。


I have a form that contains 2 <select>, the first select auto-populates itself upon page load, while the second select populates itself based on the choice selected in the first select.

To accomplish this, whenever the the select's state changes, the selected value in the first would be passed to a seperate page where it is used to populate the 2nd <select>

Problem

The selected value( Food & Beverages in this case) which is passed through the url is being cut off halfway, causing an incomplete string to be send to the processing page for the 2nd , which causes it to be unable to run.

Steps taken to identify the issue I've echoed the values that is passed through the url and only got "Food", with the rest of the string cut off. I've tried replacing the string values to Food and Beverage, and the whole thing works perfectly, leading me to conclude that the string is being cut off due to the ampersand(&) sign which causes the computer to treat the part of the string after the ampersand as another value to be passed through the URL.However, as i did not assign it to a variable, it is not being passed through.

Question

Is there any way for me to pass the value without it being cut off?

Code Extracts:

Processing Page

<?PHP
include("cxn.inc");

$query=$cxn->prepare("SELECT * FROM `BusinessSubCategory` WHERE `BusinessCategory`=:businesscategory");
$query->bindValue(":businesscategory",$_GET['category']);
$query->execute();
$count=$query->rowCount();
if($count>0)
{
    echo"<option id='subcategory' value=''>Please select a SubCategory</option>";
    while($result=$query->fetch(PDO::FETCH_ASSOC))
    {
        $subcategory=$result['BusinessSubCategory'];
        echo"<option id=$subcategory value=$subcategory >$subcategory</option>";
    }
}
else
{
    echo"<option id='subcategory' value=''>Error,fetch query not run. </option>";
}
?>

JQuery Code

$(document).ready(function(){

$('#BusinessCreateCategory').load('getbusinesscategory.php');

$('#BusinessCreateCategory').change(function(){

    var category=$('#BusinessCreateCategory').val();
    window.location.href='getbusinesssubcategory.php?category='+category;

});

EDIT:Tried encodeURIComponent, but the data is not being encoded as i can see from the url of the processing apge that it is cut off at the ampersand.HOWEVER, if i were to manually enter the url as a string and then code it using encodeURIComponent, it works wonderfully.CAn anyone shed some light on why i am unable to encode $('#BusinessCreateCategory').val(); ? Thanks!

E.gThis works

var category="Food & Beverages";
    var encoded =encodeURIComponent(category);
    window.location.href='getbusinesssubcategory.php?category='+encoded;

E.g This does not

var category=$('#BusinessCreateCategory').val();
    var encoded= encodeURIComponent(category);
    window.location.href='getbusinesssubcategory.php?category='+encoded;

If it helps, the data i am trying to pass through the url is taken from my database.

解决方案

You need to encodeURIComponent the value for category before using it in a URL.

$('#BusinessCreateCategory').change(function(){

    var category=$('#BusinessCreateCategory').val();
    var encoded = encodeURIComponent(category);
    window.location.href='getbusinesssubcategory.php?category='+encoded;

});

Ampersand is a special character that garbles the URL you are trying to pass. Encoding the value should allow you to treat it as a single value.

这篇关于通过超链接传递给其他页面的数据正在被切断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆