user3810795
user3810795

Reputation: 162

Display variable from link into input value

I have this link to an image inside td table:

....   
echo "</tr><tr>";
for ($i=0; $i<count($prov_name);$i++) {

echo "<td><a onclick='showDiv()' ><img src='images/approved.png'/></a></td>";
}
     }
        echo "</tbody>";                  
        echo "\t</table>\n";
        echo "</div>";
?>

and then I have a hide div that appears when I clicked the link above.

<div id="approve" style="height: 300px;">
<input type="text" name="ProvSelected" value="" readonly>
....

and what I want is when I click the link image pass the variable $prov_name[$i] to the input value inside the div that appears.

How can I do that? I hope you can help me. Thanks.

First Click enter image description here

then.. with variable inside input value.. enter image description here

Upvotes: 1

Views: 157

Answers (2)

I would rewrite the code like this:

Remove the <a> tag...you don't need it

Rewrite <img> with onclick added:

echo "<td><img src='images/approved.png' onclick='showDiv(\"".$prov_name[$i]."\")'/></td>";

Add an ID for the input

<input id="myInput" name="ProvSelected" value="" readonly>

Update the function showDiv()

function showDiv(val) {
  //your code plus
  document.getElementById("myInput").value = val;
}

Upvotes: 1

Barmar
Barmar

Reputation: 780724

Pass the prov name as an argument to showDiv().

foreach ($prov_name as $prov) {
    echo "<td><a onclick='showDiv(\"$prov\")' ><img src='images/approved.png'/></a></td>";
}

Then showDiv should be defined as something like:

function showDiv(prov) {
    $("#approve").show().find("input[name=ProvSelected]").val(prov);
}

Upvotes: 2

Related Questions