简体   繁体   中英

How to pass variables in a url using javascript and receive them using php?

What I am trying to do is to pass in variables from javascript to a php file using the url. The php is suppose to then parse the url into a json string and then store the json string on a separate text file. The php will then return the json string to the javascript for it to display. After a lot of testing I do believe my code isn't opening the php file or the php isn't parsing the url correctly. here is the javascript:

var xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) {
                        obj = this.responseText;
                        var data = JSON.parse(obj);
                        alert(data);
                        txt += "<tr><th>Student Id</th><th>Student Name</th><th>Type</th></tr>";
                        for (x in data.student){
                          txt += "<tr><td>" + data.student[x].id + "</td>";
                          txt += "<td>" + data.student[x].lname + ", " + data.student[x].fname + "</td>";
                          txt += "<td>" + data.student[x].type + "</td></tr>";
                        }
                        document.getElementById("display").innerHTML = txt;
                    }
                    
                    xhttp.open("GET", "assign13.php?type="+type+"&fname="+fname+"&lname="+lname+"&id="+id+"&fname2="+fname2+"&lname2="+lname2+"&id2="+id2, true);
                    xhttp.send();
                    alert("all done");
                }

Here is the php file:

<?php
    class student{
        public $type;
        public $fname;
        public $lname;
        public $id;
        function setAll($type, $fname, $lname, $id){
            $this->$fname = $fname;
            $this->$type = $type;
            $this->$lname = $lname;
            $this->$id = $id;
         }
    }
    $s1 = new student();
    $s2 = new student();
    $type = $_GET["type"];
    $fname = $_GET["fname"];
    $lname = $_GET["lname"];
    $id = $_GET["id"];
    $f2 = $_GET["fname2"];
    $l2 = $_GET["lname2"];
    $i2 = $_GET["id2"];
    $s1->setAll($type, $fname, $lname, $id);
    $s2->setAll($type, $f2, $l2, $i2);
    if ($type == "duet"){
      $directory = array($s1, $s2);
    }
    else{
      $directory = array($s1);
    }
  
    $str = json_encode($directory);
    file_put_contents("../data/data.txt", $str);
    echo $str;
?>

You have errors on your php class, it should be like this.....

<?php
    
    class student{
            public $type;
            public $fname;
            public $lname;
            public $id;
            function setAll($type, $fname, $lname, $id){
                $this->fname = $fname;
                $this->type = $type;
                $this->lname = $lname;
                $this->id = $id;
             }
        
             function getAll(){
                 return [
                   'fname' => $this->fname,
                   'type' => $this->type,
                   'lname' => $this->lname,
                   'id' => $this->id,
                 ];
             } 
        }
        $s1 = new student();
        $s2 = new student();
        $type = "A";
        $fname = "B";
        $lname = "C";
        $id = "12";
        $f2 = "Test";
        $l2 = "Test";
        $i2 = "10";
        $s1->setAll($type, $fname, $lname, $id);
        $s2->setAll($type, $f2, $l2, $i2);
        if ($type == "duet"){
          $directory = array($s1->getAll(), $s2->getAll());
        }
        else{
          $directory = array($s1->getAll());
        }
      
        $str = json_encode($directory);
        echo $str;
    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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