SoundStage
SoundStage

Reputation: 833

Extract a table row data in html

Please see the below while loop which displays the data from a table on loading of the page.

<?php 
$idcount;
$jobcount;
$flagcount;
while ( $row = sqlsrv_fetch_array ( $stmt, SQLSRV_FETCH_ASSOC ) ) 
    {
        if ($row ['Active'] == '1') {
            $flag = 'checked = "checked"';
        } else {
            $flag = '';
        }
        echo ("<tr>
            <td id = jid$idcount>".$row['JobTypeID']."</td>
            <td id = jtype$jobcount>".$row['Jobtype']."</td>
            <td align='center'><input id=active$flagcount type='checkbox' name='activecheck' $flag disabled ></td>
            <td align='center'><a class = 'toedit' href=# onclick = 'edit($idcount)'>Edit</a> | <a href=# onclick = 'dele($idcount)'>Delete</a></td>
            </tr>");
        $flag = '';
        $idcount += 1;
        $jobcount += 1;
        $flagcount += 1;
    }
    ?>

My aim is that, when I click on the Edit link, I need to fill a form with the corresponding row data.

I am intending to use javascript to carry out the processing after the data from the row has been copied, but I am not clear about how to extract the corresponding row data for every Edit link.

I am very new to PHP, so please guide me.

EDIT: Below is the code of the form that I am using

<form name="job" id="ForNew" method="post"  action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

        <input type="hidden" id="JID" value="">

        Job Type <input type="text" id="JType"name="jobtype" value="<?php echo htmlspecialchars($JobTypeValue);?>">
        <span class="error"><?php echo $JobTypeErr;?></span><br> 

        Active? Yes <input type="radio" id = "JRadio"name="radio"<?php if (isset($ActiveValue) && $ActiveValue == "yes") echo "checked"; ?>value="yes"> 
        No <input type="radio" id ="JRadio" name="radio"<?php if (isset($ActiveValue) && $ActiveValue == "no") echo "checked"; ?>value="no"> 
        <span class="error"><?php echo $ActiveErr;?></span><br>

        <input type="submit" name="sub" value="Submit!">
    </form>

EDIT 2: I have written the function edit() as follows, but the value that gets returned is in the form of Object object. But the value that I need is id name of each specific <td>, so that I can copy its value into the form textbox.

<script type="text/javascript">
function edit(Data)
{
    //Get data with reference to ID
    var Num = String(Data);
    alert("Entered Function! " + Num);
    alert("JID n Data"+$('jid'+Num));
    alert("JID value " + Num);
    alert(console.log(Num));
    $('#JID').val($('jid'+Data).html());
    $('#JType').val($('jtype'+Data).html());
    $('#JRadio').val($('active'+Data).html());
}

Even if I do not convert var Num = String(Data); to String, I am getting the same result of Object object.

Upvotes: 2

Views: 1294

Answers (2)

SoundStage
SoundStage

Reputation: 833

I just solved my own question. The problem was that the edit(Data) function was passing the correct value, but the id value of the element which needed to be taken the data from, $('#JID').val($('jid'+Data).html());, was incorrectly referenced.

The statement $('#JID').val($('jid'+Data).html()); needs to be like $('#JID').val($('#jid'+Data).html());. The # before the 'jid' was missing, which represents the "id" part of an element.

Upvotes: 1

use below code in link

onclick="edit(this)"

In edit function use this code

    function edit(e){
var x=$(e).parents(tr);
$(x).childern("#$idcount") //gives first td value
$(x).childern("#$jobcount") //gives secondtd value
$(x).children("td:nth-child(3)").children("#flagcount").val() //gives input value
}

this may help you

Upvotes: 0

Related Questions