如何将javascript中的变量传递给PHP

时间:2018-02-09 05:00:08

标签: javascript php html

我试图将js变量fq和fd传递给PHP变量 这是我的HTML表单

<form style="width: 60%; margin-top: 1%;" class="w3-modal-content w3-animate-top w3-padding w3-round w3-light-gray" action="trytesting.php" method="POST">
    <span onclick="document.getElementById('form').style.display='none'" class="w3-button w3-round w3-hover-red w3-teal w3-display-topright">&times;</span>
    <p>
        <label>Title : </label>
        <input class="w3-input w3-border w3-border-blue" type="text" name="name"></p>
    <label>From : </label>
    <input class="w3-input w3-border w3-border-blue" type="text" name="name"></p>

    <label>To :</label>
    <input class="w3-input w3-border w3-border-blue" type="text" name="company"></p>

    <table class="w3-table w3-striped w3-white" id="myTable">

        <tr class="w3-teal ">
            <th class="w3-padding-16">QTY</th>
            <th class="w3-padding-16">DESCRIPTION</th>
            <th class="w3-padding-16">Update</th>
        </tr>
        <tr>
            <td>1</td>
            <td>FOR INSPECTION</td>
            <td>edit</td>
        </tr>

    </table>
    <p>Quantity : <input class="w3-input w3-border w3-border-blue" type="text" name="qty" id="qty1"></p>
    <p>Description : <input class="w3-input w3-border w3-border-blue" type="text" name="desc" id="desc1"></p>
    <a href="#" class="w3-btn w3-teal w3-round" name="btnaddTT" onClick="addFunction()" style="width: 20%">Add Item</a>
    <p>
        <label>Training Date:</label>
        <input type="date" name="tdate" value="<?php echo Date('Y-m-d'); ?>">
    </p>
    <p>
        <label>Recieved by :</label>
        <input class="w3-input w3-border w3-border-blue" type="text" name="recieved"></p>

    <button class="w3-btn w3-cyan w3-round" name="btnaddItem" onclick="getData()" style="width: 100%">Add Contact</button>
    <a href="#" onclick="getData()">Try</a>
    <br />

</form>

我的这是我的JS代码

function getData() {
    var oTable = document.getElementById('myTable');
    var rowLength = oTable.rows.length;
    var qty = [];
    var desc = [];
    var fq = [];
    var fd = [];
    var ctr = 0;
    for (i = 1; i < rowLength; i++) {
        var oCells = oTable.rows.item(i).cells;
        var cellLength = oCells.length;
        for (var j = 0; j < cellLength; j++) {
            if (j == 0)
                qty[j] = oCells.item(j).innerHTML;
            else
                desc[j] = oCells.item(j).innerHTML;
            var cellVal = oCells.item(j).innerHTML;
        }
        fq[ctr] = qty[0];
        fd[ctr] = desc[0];
        ctr++;
    }
    window.location.href = "trytesting.php?fq=" + fq + "fd=" + fd;
}

这是我的PHP代码

我试图回应变量测试,如果变量是从PHP代码收到的,但它没有。

<?php
    if(isset($POST['fq'])){
        $output = $POST['fq'];
        $output2 = $POST['fd']
        echo "this is : $output";
        echo "this is : $output2";
    }
    else
        echo "nothing";
?>
问题是,当我点击表单中的按钮时,它会回显“没有” 所以JS中的变量可能没有传递给PHP

3 个答案:

答案 0 :(得分:2)

您以GET格式发送数据,请使用以下代码

if(isset($GET['fq'])){
    $output = $GET['fq'];
    $output2 = $GET['fd']
    echo "this is : $output";
    echo "this is : $output2";
}
else
    echo "nothing";

答案 1 :(得分:1)

我应该$_POST和/或$_GET,请注意下划线。

答案 2 :(得分:0)

试试这个,它应该有效 -

if(isset($GET['fq'])){
    $output = $GET['fq'];
    $output2 = $GET['fd']
    echo "this is : ".$output;
    echo "this is : ".$output2;
}
else
    echo "nothing";