简体   繁体   English

通过超链接传递到其他页面的数据正在被切断

[英]Data passed to other pages via hyperlink is being cut off

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. 我有一个包含2 <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> 要做到这一点,每当select的状态发生变化时,第一个中的选定值将被传递到一个单独的页面,在那里它用于填充第二个<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. 通过url传递的选定值(在这种情况下为Food&Beverages)被中途切断,导致不完整的字符串被发送到第二个处理页面,这导致它无法运行。

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. 确定问题的步骤我已经回应了通过url传递的值,只得到了“Food”,其余的字符串被切掉了。 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. 我已经尝试将字符串值替换为Food and Beverage,整个过程完美无缺,导致我得出结论,由于&符号(&)导致计算机处理字符串的一部分,字符串被切断在&符之后作为另一个值通过URL传递。但是,因为我没有将它分配给变量,所以它没有被传递。

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 JQuery代码

$(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(); 编辑:尝试encodeURIComponent,但数据没有被编码,因为我可以从处理apge的url看到它在ampersand.HOWEVER被切断,如果我手动输入url作为字符串然后使用它编码encodeURIComponent,它工作得非常好。任何人都可以解释为什么我无法编码$('#BusinessCreateCategory')。val(); ? Thanks! 谢谢!

E.gThis works 这是有效的

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

Eg 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. 在URL中使用之前,您需要使用encodeURIComponentencodeURIComponent类别的值。

$('#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. Ampersand是一个特殊字符,它会伪装您要传递的URL。 Encoding the value should allow you to treat it as a single value. 对值进行编码应该允许您将其视为单个值。

There is a browser limit to how many characters can pass through. 浏览器限制可以通过的字符数。 Do you have an example of the complete string that you are trying to pass? 你有一个你试图传递的完整字符串的例子吗? I would initially suspect that this could be an encoding issue. 我最初怀疑这可能是一个编码问题。

encodeURIComponent to encode the string being passed. encodeURIComponent,用于对要传递的字符串进行编码。

The value should be encoded but when you query your db it might look for exact match, in case you fail to see any output via the encoded string use decodeURIComponent to decode the string before passing it to db. 该值应该被编码,但是当您查询数据库时,它可能会查找完全匹配,如果您未能通过编码字符串看到任何输出,请使用decodeURIComponent在将字符串传递给db之前对其进行解码。 Check the output at phymyadmin before your formally put the code. 在正式放置代码之前检查phymyadmin的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM