繁体   English   中英

带OOP PHP的Ajax请求

[英]Ajax request with OOP PHP

这是一个简单的应用程序,可根据输入结果以及悬停时的职业和姓名来显示图像。 我正在收到以下错误: 注意:未定义的索引:C:\\ xampp \\ htdocs \\ Ajax \\ Ajax_image \\ PHP_AJAX.php中的src和注意:未定义的索引:C:\\ xampp \\ htdocs \\ Ajax \\ Ajax_image \\ PHP_AJAX.php中的名称

我是Ajax的新手,所以对我们的帮助表示赞赏。

 $(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>"; } 

您应该在比较ex之前检查isset() $arr[$i]->getname() == $_POST['name']

所以:

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

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

暂无
暂无

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

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