Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.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
Php 基于按钮类名调用Ajax函数单击_Php_Jquery_Ajax - Fatal编程技术网

Php 基于按钮类名调用Ajax函数单击

Php 基于按钮类名调用Ajax函数单击,php,jquery,ajax,Php,Jquery,Ajax,我已经用php创建了几个图像按钮。我给他们分配了一个共同的班级。当我基于按钮类名调用jquery函数时,单击。这很好,但当我尝试调用ajax函数时,它不起作用。没有看到任何错误 PHP创建一个按钮 function printpic($name, $picpath, $category, $pic) { $style = " margin=0px; background-color=transparent; border=none;"; //$functionname= "s

我已经用php创建了几个图像按钮。我给他们分配了一个共同的班级。当我基于按钮类名调用jquery函数时,单击。这很好,但当我尝试调用ajax函数时,它不起作用。没有看到任何错误

PHP创建一个按钮

function printpic($name, $picpath, $category, $pic)
{  
    $style = " margin=0px; background-color=transparent; border=none;";
    //$functionname= "selectedpic(this.id)";
    $functionname= "myCall(this.id);";
    $styleimage = "HEIGHT= 120  WIDTH= 120  BORDER=0";
    $eventimage1= "zoomin(this)";
    $eventimage2= "zoomout(this)";
    $btnclass="btnclass";   
    $j=0;
    $spa=0;
    $i=0;
    for($k=0; $k<4; $k++)
    {
        echo "<tr>";
        for($j=0; $j<4; $j++)
        {   
            echo"<td>"; 
            $btn= "btn".$category[$i];
            echo "<span id='" . $spa. "'>";
            echo "<button name='" . $btn. "'
            margin ='".$style."' 
            class='".$btnclass."'
            onClick='".$functionname."'
            >";

            echo "<img src='". $picpath[$i]."/".$name[$i]."' 
            id ='".$pic[$i]."'
            alt ='".$name[$i]."'
            .$styleimage.
            onMouseMove='".$eventimage1."'
            onMouseOut='".$eventimage2."'

            >";
            echo "</button >";
            $spa++;
            echo"</span>";
            echo"</td>"; 
            $i++;
        } // wfor

    echo "</tr>";
           }// for
         }      // end function
        ?>
})

indexverification.php

<?php
  session_start();      
  $picno=$_SESSION['picno']; // picno from db
  $answer=$_SESSION['answer']; // answer from db
  $id=$_POST['id']; // id of picture clicked
  $ans=$_SESSION['ans']; // answer type


  if (($id==$picno) && ($answer==$ans))
 {
    echo '1';
 }

 else 
{
    echo '2';   
}
?>

我认为你使用了错误的语法。试试这个:

//AJAX request
$.ajax({
     url: "indexverification.php",
     type: "POST",          
     data: datastring,      
})
//Success action
.success(function( html ) {
    if(responseText == 1) { alert ("hi");}
    else { alert (datastring); };
}) 
//Error action
.fail(function() {
    alert("Request failed.");
}); 

您能否提供一个已创建元素的HTML,例如printpic$name、$picpath、$category、$pic创建的元素?在我看来,您的语法对于ajax调用是正确的。您是否收到了警报数据字符串,或者您甚至没有做到这一点?您在控制台日志中是否收到任何错误?@dustinochran是的,我能够警报数据字符串。在这之后,它似乎不起作用了。@Prashant没有错误日志:-错误日志中没有显示任何内容,因为我相信您的所有代码都是正确的。警报后不应发生任何事情,因为没有任何东西可以触发。由于您收到数据字符串警报,意味着$id==$picno&&$answer==$ans不相等并返回“2”。您需要在indexverification.php中检查您的值,以确保它们是您期望的值。使用print\u r$\u SESSION和print\u r$\u POST,确保它是您所期望的。dude您的代码是正确的……但是上面的代码也是正确的。您可以通过多种方式编写ajax函数。
//AJAX request
$.ajax({
     url: "indexverification.php",
     type: "POST",          
     data: datastring,      
})
//Success action
.success(function( html ) {
    if(responseText == 1) { alert ("hi");}
    else { alert (datastring); };
}) 
//Error action
.fail(function() {
    alert("Request failed.");
});