duanji1482 2014-06-18 10:17
浏览 288
已采纳

通过ajax发送数据时会自动添加转义字符

I want to send the SQL condition statement using the id of the link element which is the category of the product to be generated by php file and loaded back to the div with class load_area. I get the category with .attr() method and then send this data as the string {'category='"+cat+"'} to the php file. The string sent should be

category="bag"

but this is what I got as a print result in php file

category='\"bag\"'

As you can see that the php file automatically added the escape character to the string I sent to it and the query couldn't be successfully processed. I am so frustrated trying to find the right way solving the problem. So please help ...

This is my HTML

<a id="bag" href="#">Bag</a>
<div class="load_area"></div>

This is javaScript

$(document).ready(function() {
    $('a').click(function() {
        var cat = $(this).attr('id');
        $('.load_area').load('controllers/genproduct.php', {'condition':'category='"+cat+"'});
    });
});

This is my php script

<?php
$condition = explode("=", $_POST['condition']); // Get condition
$limit = $_POST['limit']; // Get limit
require_once("../engines/store_manager.class.php");
$store = new store_manager(mysql_connect("localhost", "root", "1234"), "gemalong",     "cart", "products");
if(isset($condition) || $condition != "") {
if(isset($limit) || $limit != "") {
    $getquery = $store->product_gen($condition, $limit);
}
else {
    $getquery = $store->product_gen($condition);
}
}
else {
$getquery = $store->product_gen();
}
while($data=mysql_fetch_array($getquery)) {
$pid = $data['id'];
$pname = $data['name'];
$pdesc = $data['description'];
$pprice = $data['price'];
$ppix = $data['pix'];
// Start generating view
echo "<div class='each_product'>";
    echo "<div class='img_container'>";
        echo "<a id='ppixlink".$pid."' href='#' alt='".$pname."'><img      src='".$ppix."' title='".$pname."' /></a>";
    echo "</div>";
    echo "<p class='product_name'><a id='plink".$pid."' href='#'     alt='".$pname."'>".$pname."</a></p>";
    echo "<div class='down_pb clearfix'>";
        echo "<p>$ ".$pprice."</p>";
        echo "<button type='button' id='productaddbtn".$pid."' class='addcart-btn'>Add to cart</button>";
    echo "</div>";
echo "</div>";
}
// Close connection
mysql_close();
?>
  • 写回答

1条回答 默认 最新

  • douqi1212 2014-06-18 10:25
    关注

    Dont send a string that needs manipulating in php, send two fields instead:

    //js
    $('.load_area').load('controllers/genproduct.php', {field: 'category', value: cat});
    
    //php
    $condition = array($_POST['field'], $_POST['value']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵