0

I have a PHP page in wich I charge result of a query into a Table that i defined like so:

<?php
$json=file_get_contents("http://www.example.com/myfile.php");
$data =  json_decode($json);

if (count($data->result)) {
echo "<table id= 'tabclb'>";
foreach ($data->result as $arr => $result) {
echo"<tr>";
echo "<td><a href=sel_clb.php?id=$result->ID>$result->ID</a></td>";  
echo"<td>$result->Name</a></td>";
echo"</tr><td>";
        }
echo "</table>";
} 
?>

As you can see, my table will have an hyperlink to a php file to recall that name using the ID record. In php file where i have stored my query, I use GET to execute query Json. Well, can someone tell me how I can take ID and Name from query and put them into two textbox that I already have created in my php page like so?

</form>
          <form action="upd_collab.php" method="post" name="form" id="form">
            <label for="nome">Recall</label>
            <input name="idx" type="text" placeholder="idx"/>
            <label for="pass"><br />
            <br />
            Nome<br />
            <input name="Nome" type="text" placeholder="Nome"/>
            <br />
                            Sesso (M/F)</label>
            <p><input name="Sesso" type="Sesso" placeholder="Sesso"/>
            </p>
            <p>
              <input name="submit" type="submit" value="Modifica"/>
            </p>
            <p> </p>
          </form>

I hope it's clear. Thanks

Jayelef
  • 135
  • 12
  • You can either print these out by printing php direct in the HTML or you can print from PHP to JavaScript to update these HTML elements. In the first case the update happens at page creation time on the server. In the latter case, the update happens after the page has loaded in the user's browser. – sunny Oct 28 '15 at 14:38
  • Thanks. I thought so, but i don't know how to do. I.e. if I want to do from php file? Once executed query, how I can tell to put json result into textbox of page that has called query execution? Or from php initial page, by clicking on link? – Jayelef Oct 28 '15 at 14:42

2 Answers2

0

I'll elaborate on my comment that you can do this by interspersing PHP with HTML to achieve this server-side, per your request.

Assuming the HTML for your form is inside your PHP file but outside your PHP tags, you can add the value of a php variable anywhere in the HTML like so:

    <input name="idx" type="text" placeholder="idx" value='<?php echo $valueOfIDX; ?>'/>

For this to work you must not get to this HTML/variable printing section until after you have the value of your relevant variable, since PHP script is executed line by line without any hoisting.

I'll also add this question has been answered here: PHP - Set value in HTML-Form

And you will also find many more examples with an internet search.

Community
  • 1
  • 1
sunny
  • 3,853
  • 5
  • 32
  • 62
  • Well, you are very clear! But I have a question yet: Can i specify the name of php file in this string? – Jayelef Oct 28 '15 at 15:26
  • I don't understand your question. As in you want to include a php file rather than echo a php variable? – sunny Oct 28 '15 at 15:29
  • Sorry, maybe I was no clear then you. I try to exlain better. I the first secrion above, when i charge page, i refer to a php file than sends data to fill my table, it' "myfile.php". By clicking on every row however, the row pass his value to another file sel_clb.php that execute a query and gives ID NAME AND SEX. Well, in the same page, I have three text boxes and I should want to assign id,name, and sex from result query inside them. Sorry if my issue doesn't explained well. – Jayelef Oct 28 '15 at 15:53
  • Then you want to do an update once the page is already loaded in the browser no? In that case you need to use JavaScript. – sunny Oct 28 '15 at 16:02
  • Yes! It's just I shoud want! Page charge first time, in it i have a table created from query and 3 textbox. Once i click on link on my table, this execute another php and result comes in every textbox... How to do with javascript!? :-) – Jayelef Oct 28 '15 at 16:07
  • @Jayelef here's a few links to check out: http://www.javascript-coder.com/javascript-form/javascript-form-value.phtml http://stackoverflow.com/questions/10962809/get-number-value-from-form-calculation-and-updating-html-content-with-javascri http://stackoverflow.com/questions/1414713/creating-form-in-javascript-without-html-form http://stackoverflow.com/questions/24772051/change-javascript-variable-to-value-in-html-form-field – sunny Oct 28 '15 at 16:08
0

You can change your php file as:

<?php
    $json=file_get_contents("http://www.example.com/myfile.php");
    $data =  json_decode($json);

    if (count($data->result)) {
?>
<table id= 'tabclb'>
<?php
foreach ($data->result as $arr => $result) {
?>
    <tr>
         <td><input name="idx" type="text" placeholder="idx" value="<?=$result->ID; ?>"/></td>
         <td><input name="Nome" type="text" placeholder="Nome" value="<?=$result->Name; ?>"/></td>
    </tr>
<?php
} //End foreach
?>
<tr>
    <td><input name="submit" type="submit" value="Modifica"/></td>
</tr>
</table>

<?php
    } //End if
?>

Hope it be helpful for you.

Linh
  • 121
  • 4