简体   繁体   中英

How to pass variables in javascript to PHP

Im trying to pass the js variable fq and fd to PHP variable this is my HTML form

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

and my this is my JS code

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

and this is my PHP code

i was trying to echo the variables testing out if the variables are received from the PHP code but it doesnt.

<?php
    if(isset($POST['fq'])){
        $output = $POST['fq'];
        $output2 = $POST['fd']
        echo "this is : $output";
        echo "this is : $output2";
    }
    else
        echo "nothing";
?>

the problem is that when i click the button in the form, it will echo "nothing" so probably the variables from JS wasnt passed to PHP

Your sending data as GET so use following code,

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

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

Try this,it should work-

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

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