2

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.

4
  • Have you tried anything to generate the form? Commented Jan 29, 2015 at 6:27
  • Yes. I have added my code of the form now.
    – SoundStage
    Commented Jan 29, 2015 at 6:38
  • pass $idcount in edit() function and get the related row with that id
    – Afsar
    Commented Jan 29, 2015 at 9:26
  • @zan I have done what you have suggested. But the value that is returned is in the form of Object object. Could you tell what mistake I have made?
    – SoundStage
    Commented Jan 30, 2015 at 7:24

2 Answers 2

1

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.

0

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

9
  • 1
    I am getting the following error on Firebug ReferenceError: $ is not defined var Row = $(Data).parents(tr); I modified the code to var Row = $(Data).parents(tr); alert($(Row).childern("#$idcount")); //gives first td value to check if the table value is being retrieved
    – SoundStage
    Commented Jan 29, 2015 at 7:31
  • Dont use $ in id,~stackoverflow.com/questions/70579/… Commented Jan 29, 2015 at 9:29
  • 1
    I removed the $ from var Row = $(Data).parents(tr); and now it says tr is not defined.
    – SoundStage
    Commented Jan 29, 2015 at 9:43
  • Changed to ("tr") and I am now getting an error that says TypeError: Data.parents is not a function
    – SoundStage
    Commented Jan 29, 2015 at 10:02
  • The issue is that there is no data that is being sent from onclick = 'edit(this)'. How do I pass values stored in each row, that was dynamically created by PHP, as a single object to the function?
    – SoundStage
    Commented Jan 29, 2015 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.