Javascript POST data goes to GET PHP array -
i spent hours figuring why ajax script wasn't working. found out request variables sent post went $_get. why that? here final working code.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>groups</title> <link rel="stylesheet" href="main.css"> </head> <body> <?php include "navigation.php"; $conn=new mysqli('',databaseuser,databasepassword,database); $q="select * user_groups"; $r=$conn->query($q); if($r) { echo "<table><tr><th>name</th><th>delete topics</th><th>edit others' content</th><th>delete comments</th> <th>deactivate users</th><th>delete own topics</th></tr>"; $number=0; while($row=$r->fetch_array()) { $n=$row['groupname']; $dt=$row['candeletetopics']; $dc=$row['candeletecomments']; $du=$row['candeactivateusers']; $dot=$row['candeleteowntopics']; $eo=$row['caneditotherscontent']; echo "<tr><td id='name$number'>$n</td> <td><button id='candeletetopics$number' onclick='toggle(this)'>$dt</button></td> <td><button id='caneditotherscontent$number' onclick='toggle(this)'>$eo</button></td> <td><button id='candeletecomments$number' onclick='toggle(this)'>$dc</button></td> <td><button id='candeactivateusers$number' onclick='toggle(this)'>$du</button></td> <td><button id='candeleteowntopics$number' onclick='toggle(this)'>$dot</button></td> </tr>"; $number++; } echo "</table>"; } ?> <script> function toggle(button) { var id=button.id var position=id.search(/[0-9]/) var columnname=id.slice(0,position) var number=id.slice(position); //console.log(number) var id2='name'+number; var group=document.getelementbyid(id2).innerhtml var request=new xmlhttprequest(); request.onreadystatechange=function () { if(request.readystate==4) { location.reload() console.log(request.responsetext) } } request.open('post','editclass.php?columnname='+columnname+'&class='+group,true) request.send(null) } </script> </body> </html>
editclass.php:
<?php $columnname=$_get['columnname']; $name=$_get['class']; include "constants.php"; $conn=new mysqli('',databaseuser,databasepassword,database); if($conn) { $q="update user_groups set $columnname = not $columnname groupname='$name'"; $res=$conn->query($q); if($res) echo "toggled ".$columnname; else echo $conn->error; } else echo $conn->error;
if use $_post, appears empty.
you need send post parameters in send()
call this:
var group=document.getelementbyid(id2).innerhtml var params = "id="+button.id+"&position="+id.search(/[0-9]/)+"&columnname="+id.slice(0,position)+"&number=id.slice(position)+"&id2="+group+"&name="+number; request.send(params);
Comments
Post a Comment