如何使用php ajax发送多个参数

时间:2016-12-22 14:51:44

标签: php html ajax

这是我页面的一部分  1 我想要的是,当用户点击“接受”按钮时,“接受”功能被调用参数(每行有不同的参数)

在php文件中

    $connected = mysqli_connect("localhost", "root", "", "web");

    if(isset($_GET['t']))
{   $idd = $_GET['p'];
    $tit = $_GET['q'];
    $typ = $_GET['t'];

  if( '$typ' == 'accept')
    {    $sql = "INSERT INTO coursestudent (stuID, cTitle) VALUES ('$idd', '$tit')";
  $result = mysqli_query($connected,$sql); 
  $sql2 = "DELETE FROM studentrequest WHERE sID = '$idd' AND cTitle = '$tit' AND tID = '1111'";
$result2 = mysqli_query($connected,$sql2);    }
  else
{     $sql2 = "DELETE FROM studentrequest WHERE sID = '$idd' AND cTitle = '$tit' AND tID = '1111'";
$result2 = mysqli_query($connected,$sql2); }}

$teacherId = "1111";
 $sqll = "SELECT cTitle, student.studentName, sID FROM student, studentrequest WHERE studentrequest.tID=$teacherId AND studentID=sID";

$res = mysqli_query($connected, $sqll);
$acc = 'accept';
$rej = 'reject';
if(mysqli_num_rows($res) > 0)
{       echo "<table border = 1> <tr> <th>Title</th> <th> student name </th> <th> student ID </th> <th>  </th> </tr> ";
     while ($row = mysqli_fetch_array($res))
        echo "<tr> <td> $row[cTitle] </td> <td> $row[studentName] </td> <td>     $row[sID] </td> <td> <input type='button' id= 'accept'onclick='taa($acc,   $row[cTitle],$row[sID]);' value='accept'><input type='button' id= 'reject' onclick='taa($rej, $row[cTitle],$row[sID]);' value='reject'> </td></tr>" ; 
    echo " </table>";
}
else
    echo "<strong> no record found<strong>"; 

在html文件中

    function taa(typ , tit , idd){

        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {

            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("fh5co-content").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","StudentRequest.php?q="+tit+"&p="+idd+"&t="+typ,true);
        xmlhttp.send();
    }

我知道我使用该函数的方式是错误的,我不应该使用onclick,但我不知道如何发送每一行的参数

3 个答案:

答案 0 :(得分:0)

为了实现以更正确的方式使用onclick事件处理程序的目标,您可能会发现以下内容对您有所帮助。

初始PHP(几乎)没有变化,除了一些代码格式化的易读性,所以如果有错误,那么他们之前就在那里; - )

$connected = mysqli_connect("localhost", "root", "", "web");
if( isset( $_GET['t'], $_GET['p'], $_GET['q'] ) ){

    $idd = $_GET['p'];
    $tit = $_GET['q'];
    $typ = $_GET['t'];

    if( '$typ' == 'accept') {

        $sql = "INSERT INTO coursestudent (stuID, cTitle) VALUES ('$idd', '$tit')";
        $result = mysqli_query($connected,$sql); 

        $sql2 = "DELETE FROM studentrequest WHERE sID = '$idd' AND cTitle = '$tit' AND tID = '1111'";
        $result2 = mysqli_query($connected,$sql2);

    } else { 
        $sql2 = "DELETE FROM studentrequest WHERE sID = '$idd' AND cTitle = '$tit' AND tID = '1111'";
        $result2 = mysqli_query($connected,$sql2);
    }
}

HTML中具有ID属性的每个元素都必须具有唯一ID才能被视为有效,并允许以编程方式正确定位元素 - 因为实际上不需要每个按钮的ID,所以删除它们。相反,尽管每个按钮都有dataset个属性,这些属性分配了来自数据库的相关信息 - 这些数据集属性将通过javascript函数以编程方式访问。

$teacherId = "1111";
$sqll = "SELECT cTitle, student.studentName, sID FROM student, studentrequest WHERE studentrequest.tID=$teacherId AND studentID=sID";

$res = mysqli_query( $connected, $sqll );
$acc = 'accept';
$rej = 'reject';

if( mysqli_num_rows( $res ) > 0 ){ 

    echo "<table id='students' border=1>
        <tr>
            <th>Title</th>
            <th> student name </th>
            <th> student ID </th>
            <th></th>
        </tr>";

    $row=0;

     while ( $row = mysqli_fetch_array( $res ) ){

        $title=$row['cTitle'];
        $sid=$row['sID'];


        echo "
        <tr>
            <td>{$row['cTitle']}</td>
            <td>{$row['studentName']}</td>
            <td>{$row['sID']}</td>
            <td><!--// Each button has `dataset` attributes //-->
                <input type='button' data-title='{$title}' data-sid='{$sid}' data-action='{$acc}' value='accept' />
                <input type='button' data-title='{$title}' data-sid='{$sid}' data-action='{$rej}' value='reject' />
            </td>
        </tr>"; 

        $row++;
    }
    echo " </table>";


} else {
    echo "<strong> no records found</strong>";
}

在vanilla javascript中,首选方法不是使用inline事件处理程序,而是使用类似下面的内容将事件侦听器分配给相关的HTML元素。

使用querySelectorAll找到我们希望为其分配ajax函数(作为事件处理程序)的所有正确按钮,提供了一种简单的机制。如果找到与伪XPath表达式匹配的元素,则遍历集合并为每个元素分配一个函数。

<script type='text/javascript'>
    var col = document.querySelectorAll( 'table#students td input[type="button"]' );
    if( col ){
        for( var n in col )if( col[ n ].nodeType==1 )col[ n ].addEventListener('click',function(e){
            var bttn = e.target;
            var action=bttn.dataset.action;
            var title=bttn.dataset.action;
            var sid=bttn.dataset.sid;

            var xhr = new XMLHttpRequest();
                xhr.onreadystatechange=function(response){
                    if ( xhr.readyState == 4 && xhr.status == 200 ) document.getElementById("fh5co-content").innerHTML = response;
                };
                xhr.open( 'GET', 'StudentRequest.php?q='+title+'&p='+sid+'&t='+action );
                xhr.send();

        }.bind( col[ n ] ), false );
    }
</script>

为了记录,以上未经过测试......希望有所帮助

答案 1 :(得分:0)

我很肯定有更好的方法,但是目前,如果我想传递多个参数,我会这样做:

"someFunc.php?param=" + var1 + "|" + var2 + "|" + var3

在PHP中:

$var1 = explode('|', $_GET['param'])[0];
$var2 = explode('|', $_GET['param'])[1];
$var3 = explode('|', $_GET['param'])[2];

它相当&#34;快速而肮脏&#34;,但它有效:)

答案 2 :(得分:0)

获取值并将其传递给  //服务器脚本 例如

var test = document.getElementById('test').value;
var test2 = document.getElementById('test2').value;

 var queryString = "?test=" + test ;
 queryString +=  "&test2=" + test2;
 ajaxRequest.open("GET", "test.php" + queryString, true);
 ajaxRequest.send(null);