0

here is part of my page 1 what i want is , when the user click on "accept" button "accept"function is called with parameters (every row has different parameters)

in php file

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

in html file

    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();
    }

I know that my way of using the function is wrong , and I shouldn't use onclick, but I don't know how to send the parameters of every row

1
  • Pack them into a json. Commented Dec 22, 2016 at 14:54

3 Answers 3

1

I am positive there are better ways, but currently, if I want to pass multiple parameters, I do something like this:

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

And in PHP:

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

It's rather "quick and dirty", but it works :)

Sign up to request clarification or add additional context in comments.

Comments

0

To achieve your goal of using an onclick event handler in a more correct manner you might find the following helpful.

The initial PHP is (virtually) unchanged other than some code formatting for legibility so if there are errors there then they were there before ;-)

$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);
    }
}

Every element in the HTML that has an ID attribute MUST have a unique ID to be considered valid and to allow correct targeting of the element programmatically - as the ID for each button was not really required they were removed. Instead though each button has dataset attributes assigned with the relevant pieces of information from the db - these dataset attributes will be accessed programmatically by the javascript function.

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

Rather than using inline event handlers the preferred way, in vanilla javascript, is to use something like the following to assign the event listeners to the relevant HTML elements.

To find all the correct buttons for which we wish to assign the ajax function ( as event handler ) using querySelectorAll provides a straightforward mechanism. If there are elements found that match the pseudo XPath expression iterate through the collection and assign a function to each element.

<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>

For the record the above is not tested ... hope it helps

Comments

0

get the value and pass it to // server script. For example

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); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.