Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery AJAX在另一个页面上发布结果_Javascript_Php_Jquery_Mysql_Ajax - Fatal编程技术网

Javascript jQuery AJAX在另一个页面上发布结果

Javascript jQuery AJAX在另一个页面上发布结果,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我搞不懂阿贾克斯 如何在index.php上显示选中的结果,单击链接(AJAX-javascript函数)在results.php页面上显示这些结果 database.sql CREATE TABLE IF NOT EXISTS `compare` ( `id` int(11) NOT NULL AUTO_INCREMENT, `company` varchar(20) NOT NULL, `image` varchar(20) NOT NULL, `size` varcha

我搞不懂阿贾克斯

如何在index.php上显示选中的结果,单击链接(AJAX-javascript函数)在results.php页面上显示这些结果

database.sql

  CREATE TABLE IF NOT EXISTS `compare` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `company` varchar(20) NOT NULL,
  `image` varchar(20) NOT NULL,
  `size` varchar(20) NOT NULL,
  `price` int(11) NOT NULL,
  `color` varchar(20) NOT NULL,
  `shape` varchar(100) NOT NULL,
  `weight` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

INSERT INTO `compare` (`id`, `company`, `image`, `size`, `price`, `color`, `shape`, `weight`) VALUES
(1, 'Adidas', '1.jpg', '7', 600, 'Blue', 'Normal', '50 gram'),
(2, 'Adidas', '2.jpg', '7', 700, 'Red Blue', 'Sports', '60 gram'),
(3, 'Nike', '3.jpg', '8', 800, 'Red Blue', 'Sports', '60 gram'),
(4, 'Puma', '4.jpg', '8', 1800, 'Red Black', 'Normal Sports', '70 gram'),
(5, 'Reebok', '5.jpg', '9', 2000, 'White Black', 'Normal Sports', 'N/A');
database.php

<?php
$connect = mysql_connect('localhost','root','pass'); 
if (!$connect) { 
die('Could not connect to MySQL: ' . mysql_error());
 }
 mysql_select_db('compare_test',$connect);
?>

index.php

<script type="text/javascript">
function compare()
{
var total_check = new Array();
$('.products:checked').each(function () {
    total_check.push($(this).val());
});

if (total_check != '') {
    if (total_check.length == '2') {
    var i = 0;
    var pidArray = new Object();
    $('.products:checked').each(function () {
    total_check.push($(this).val());
    var id = $(this).attr('id');
    pidArray[i] = {
        pid: id
    };
    i++;
});
var data = JSON.stringify(pidArray);
var href = this.href;
//e.preventDefault();
$('#wait').show();
        $.ajax({
            url: "compare.php",
            type: "POST",
            data: {pids:data,type:'compare'},
            cache: false,
            success: function (data) {
            $('#wait').hide();
                document.location = "http://localhost/usp/results.php";
            }
        });
    } else {
        alert("Please select two products ");
        return false;
    }
} else {
    alert("Please select minimum two products");
    return false;
}
}
 </script>

<tr>
    <td width="10%"><a href="javascript:void(0)" onclick="compare();" style="color:green;font-size:15px;"><b>Compare</b></a></td>
    <td width="20%">Product Image</td>
    <td width="20%">Comany Name</td>
    <td width="20%">Price</td>
    <td width="20%">Details</td>
</tr>
<?php $sql =mysql_query("Select * FROM compare");
    while($data = mysql_fetch_assoc($sql)) {?>
<tr>
    <td><input type="checkbox" name="products[]" class="products" id="<?php echo $data['id']; ?>"></td>
    <td><img src="image/<?php echo $data['image']; ?>" width="80" height="80px;"></td>
    <td><?php echo $data['company']; ?></td>
    <td><?php echo $data['price']; ?></td>
    <td><a href="javascript:void(0);" class="detail" id="detail-<?php echo $data['id']; ?>">Details</a></td>
</tr>
<?php } ?>

函数比较()
{
var total_check=新数组();
$('.products:checked')。每个(函数(){
总检查推送($(this.val());
});
如果(总检查次数!=''){
如果(总检查长度='2'){
var i=0;
var pidArray=新对象();
$('.products:checked')。每个(函数(){
总检查推送($(this.val());
var id=$(this.attr('id');
pidArray[i]={
pid:id
};
i++;
});
var data=JSON.stringify(pidArray);
var href=this.href;
//e、 预防默认值();
$('#wait').show();
$.ajax({
url:“compare.php”,
类型:“POST”,
数据:{pids:data,类型:'compare'},
cache:false,
成功:功能(数据){
$('#wait').hide();
document.location=”http://localhost/usp/results.php";
}
});
}否则{
警告(“请选择两种产品”);
返回false;
}
}否则{
警告(“请至少选择两种产品”);
返回false;
}
}
产品形象
公司名称
价格
细节
<?php
    include('database.php');
    $Totalpids = (array)json_decode(stripslashes($_GET['pids']));
    foreach($Totalpids as $product)
    {
       $sql = mysql_query("SELECT * FROM compare where id=".$product->pid."");
       $data = mysql_fetch_assoc($sql);
       $res .="<table cellspacing='2' cellpadding='0' align='left' width='240'>
       <tr><th align='left'><strong>Product Details</strong></th></tr>
       <tr height='75' align='center'><td align='left'><img src='image/".$data['image']."' width='75px' height='75px'></td></tr>
       <tr height='40' align='center'><td align='left'>".$data['company']."</td></tr>
       <tr height='40' align='center'><td align='left'>".$data['size']."</td></tr>
       <tr height='40' align='center'><td align='left'>".$data['price']."</td></tr>
       <tr height='40' align='center'><td align='left'>".$data['color']."</td></tr>
       <tr height='40' align='center'><td align='left'>".$data['shape']."</td></tr>
       <tr height='40' align='center'><td align='left'>".$data['weight']."</td></tr>";
       $res .= "</table>";
       }
       echo $res;
       ?>
<?php
  // what here to display taht results?

?>
<!DOCTYPE html>
<html dir="ltr" lang="hr">
<head>
<title>asd</title>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="application/javascript">
(function($){
$.ajax({
    type: "GET",
    url: "compare.php",
    dataType: "json",
    success: function (data) {
        $.totalProducts = data || 0;
    }
    });
}(jQuery));
</script>
</head>

<body>
</body>
</html>