0

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

1
  • your putting html into a url without encoding it? that's a no no. Commented Feb 9, 2018 at 6:32

3 Answers 3

2

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";
Sign up to request clarification or add additional context in comments.

Comments

1

I should be $_POST and/or $_GET, mind the underscore.

Comments

0

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

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.