PHP Load Variable in Field

by 6 replies
7
How would I populate a field form by clicking on a link?
I am pulling info from a database in to a table with many rows

[PHP]
<?php echo $row["zip"]; ?>
[PHP]

I want a person to be able to click the result and have the result load into another for field

PHP Code:
<input name="zip_pu" type="text" required class="zip_pu" pattern="[0-9 -]{7,20}" size="20" value="<?PHP echo $_SESSION['zip_pu'?>">
I am not sure if _SESSION is the way to go, I just know that I want to attempt doing it without javascript
#programming #field #load #php #variable
  • Maybe something along these lines would work?
    PHP Code:
    <td><a href="<?PHP echo $_SERVER['PHP_SELF']; ?>" onclick="<?php $_POST['zip_pu'] = 'zip' ?>"><?php echo $row["zip"]; ?></a></td>
  • If it's not possible without JS then that's fine, the user can copy/paste or type in the data if they need
  • This would be done with javascript. I am not an expert with JS but this should get you in the right direction. I would do it with some jQuery (ie: be sure to load the jquery library or it won't work)

    http://jsfiddle.net/hkSNB/3/

    Code:
    $('td').click(function() {
       tdval = $(this).html();
       $('#yourinputidhere').val(tdval);
    });
    You are just copying the content from the td tag to the input of your choice. Pretty basic stuff.
    • [ 1 ] Thanks
  • Gonna give that a try as soon as these updates finish, thank you!
  • I made some changes that work for what I needed, if anyone else is looking for something like this
    http://jsfiddle.net/hkSNB/5/
  • jquery ended up being kind of bulky for something so small. I figured out another way where I wouldn't need to load a whole library.
    Code:
    <td class="zip_hover"><a onclick="document.getElementById('zip_pu').value='<?php echo $row["zip"]; ?>';"><?php echo $row["zip"]; ?></a></td>
    Code:
    <td class="zip_hover"><a onclick="document.getElementById('zip_do').value='<?php echo $row["zip"]; ?>';"><?php echo $row["zip"]; ?></a></td>

Next Topics on Trending Feed