简体   繁体   中英

javascript innerhtml won't send a variable to a link

i am working on laravel using ajax i send data to server and i get users then i wont to edit view and delete the users am using javascript innerhtml to create a table for the users but when i created the link for edit view and delete it redirect but doesn't send the data instead it send the variable as link.

here is my ajax

<script>
function getstudent(str) {
    var xmlhttp;
    var dep = document.getElementById("department").value;

    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {

            var data = xmlhttp.responseText;
            var obj = JSON.parse(data);
            var x = obj.length;
            var j;

            var table = document.getElementById("myTable");
            var head = document.getElementById("head");
            // table.innerHTML="<tbody> </tbody>"
            head.innerHTML = " <tr> <th>identification</th><th>first name</th><th>last name</th> <th>gfname</th> </tr>"
            //document.getElementById("mytable").innerHTML=' ';
            table.appendChild(head);

            for (j = 0; j <= x; j++) {

                var row = table.insertRow(j + 1);

                // Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
                var cell1 = row.insertCell(0);
                var cell2 = row.insertCell(1);
                var cell3 = row.insertCell(2);
                var cell4 = row.insertCell(3);
                var cell5 = row.insertCell(4);
                var cell6 = row.insertCell(5);
                var cell7 = row.insertCell(6);
                // Add some text to the new cells:
                cell1.innerHTML = obj[j].identification;
                cell2.innerHTML = obj[j].name;
                cell3.innerHTML = obj[j].fname;
                cell4.innerHTML = obj[j].gfname;
                var user = obj[j].id;
                cell5.innerHTML = "<a href='{{ action("
                UserController@show "," + user + ") }}' class='btn btn-default'>View</a>&emsp;";
                cell6.innerHTML = "<a href='{{ action("
                UserController@edit "," + user + ") }}' class='btn btn-default'>View</a>&emsp;";
                cell5.innerHTML = "<a href='{{ action("
                UserController@destroy "," + user + ") }}' class='btn btn-default'>View</a>&emsp;";

            }
        }
    }

    xmlhttp.open("GET", "indexstudget/" + str + "/" + dep, true);
    xmlhttp.send('');
}

here is some part of my view

<div class="form-group">
    <label class="col-md-4 control-label">Department</label>
    <div class="col-md-6">
        <select id='department' class="form-control" name="department" onchange="year(this.value)">
            <option>select</option>@foreach( $departments as $department)
            <option name="department" value={{ $department->id }}> {{$department->name}}</option>@endforeach</select>
    </div>
</div>
<br>
<br>
<div class="form-group">
    <label class="col-md-4 control-label">Year</label>
    <div class="col-md-6">
        <select id='demo' name='demo' class="form-control" onchange="getstudent(this.value)">
            <option>select</option>
        </select>
    </div>
</div>@if ( $students->isEmpty() ) There are no students. @else
<table class="table table-striped" id="myTable">
    <thead id="head"></thead>
</table>@endif
<div class="panel-body"> <a href="{{ action('RegistrarController@create') }}" class="btn btn-primary">Create user</a>

    </form>
</div>
</div>

it create the table with list of user correct but when i click it it send the variable instead of the data

like this

url http://project/edituser/+user+

I change the controller action into direct route and it worked.like this

"<a href='{{ action("UserController@show","+user+") }}' class='btn btn-default'>View</a>&emsp;"
"<a href='showuser/"+user+"' class='btn btn-default'>View</a>&emsp;"

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