weixin_33725515 2018-11-03 21:46 采纳率: 0%
浏览 30

带OOP PHP的Ajax请求

It is a simple application which shows an image based on the input result as well as its occupation and name when hovered. I am receving the following errors: Notice: Undefined index: src in C:\xampp\htdocs\Ajax\Ajax_image\PHP_AJAX.php and Notice: Undefined index: name in C:\xampp\htdocs\Ajax\Ajax_image\PHP_AJAX.php

I am farely new to Ajax so any help is appreciated.

$(document).ready(function() {
    $('#view').click(function() {
        var namevalue = $('#name').val();
        $.post("PHP_AJAX.php", {
            name: namevalue
        }, function(data) {
            $('#bar').html(data);
            $("img").hover(function(e) {
                var s1 = "<img src=";
                var s2 = " width='110px' height='120px' />";
                var srcval = s1.concat($(this).attr('src'), s2);
                $.post("PHP_AJAX.php", {
                    src: srcval
                }, function(data1) {
                    $('#info').css({
                        top: e.clientY,
                        left: e.clientX,
                        backgroundColor: 'yellow'
                    }).fadeIn().html(data1);
                });
            }, function() {
                $('#info').fadeOut();
            });
        });
    });
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    <script src="PHP_AJAX.js"></script>
    <style>
        #info {color:black; border:5px blue solid; width:150px; height:100px;display:none;position:absolute;}
    </style>
</head>
<body>
    <p id='bar'>Please enter name to view image! </p>
    <p id='info'>info</p>
    <form>
    <p>Name : <input type='text' name='name'id ='name'/></p>
    </form>
    <button id='view' name ='view'>View</button>
</body>
</html>

class Person
{
// some properties
    private $name;
    private $occupation;
    private $image;
// constructor
    public function __construct($nameval, $occuval, $imgval)
    {
        $this->name       = $nameval;
        $this->occupation = $occuval;
        $this->image      = "<img src=" . $imgval . " width='110px' height='120px' />";
    }
// get name property
    public function getname()
    {
        return $this->name;
    }
// get occupation property
    public function getoccupation()
    {
        return $this->occupation;
    }
// get image property
    public function getimage()
    {
        return $this->image;
    }
}

$obj1 = new Person("Picasso", "Painter", "pi1.jpg");
$obj2 = new Person("Ronaldo", "Football Player", "ronaldo.jpg");
$obj3 = new Person("Picasso", "Teacher", "pi2.jpg");
$obj4 = new Person("Madonna", "Singer", "madonna.jpg");
// storing objects in an array
$arr   = array($obj1, $obj2, $obj3, $obj4);
$count = 0;
for ($i = 0; $i < 4; $i++) {
// if name already exist
    if ($arr[$i]->getname() == $_POST['name']) {
        echo "<p>Image of " . $arr[$i]->getname() . "</p>";
        echo "<p>" . $arr[$i]->getimage() . "</p>";
        $count++;
    }
    if ($arr[$i]->getimage() == $_POST['src']) {
        echo "<p>Name: " . $arr[$i]->getname() . "</p>";
        echo "<p> Occupation:" . $arr[$i]->getoccupation() . "</p>";
        $count++;
    }
}
// if name doesn't exist
if ($count == 0) {
    echo "<h3> NOT FOUND! </h3>";
}

</div>
  • 写回答

1条回答 默认 最新

  • 妄徒之命 2018-11-03 21:54
    关注

    you should check isset() before compare ex. $arr[$i]->getname() == $_POST['name']

    so:

    if (isset($_POST['name']) && $arr[$i]->getname() == $_POST['name']) {
        ...
    }
    
    if (isset($_POST['src']) && $arr[$i]->getimage() == $_POST['src']) {
        ...
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭