Temporary_Failure Posted October 11, 2021 Share Posted October 11, 2021 <?php error_reporting(E_ALL & ~E_NOTICE); if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['someAction'])) { func(); } function func() { $sku = $_POST['sku']; $haku = file_get_contents("https://mydomain.org/wp-json/wc/v3/products/" . $sku . "?consumer_key=ck_notrealtjejyj8b0f84cf68a9afd9ba6&consumer_secret=cs_notreal68d8rku5757k5yu2094747cfb4c1e2af"); //echo strip_tags($haku); $haku = json_decode($haku, true); echo "<br><br>Tuotenumero: ".$haku['sku']."<br>"; echo "Varastosaldo: ".$haku['stock_quantity']."<br>"; echo "Hinta: ".$haku['price']."<br>"; $hinta = $haku['price']; $stock = $haku['stock_quantity']; } ?> <!DOCTYPE html> <html> <head> <title>Horror</title> <meta charset="utf-8"> </head> <form action="API.php" method="post"> <table> <tr><td><label for="sku">Tuotenumero</label></td> <td><input type="text" id="sku" name="sku" value="150000"></td></tr> <tr><td><label for="stock">Varastosaldo</label></td> <td><input type="text" id="stock" name="stock" value="<?php echo $stock; ?>"></td></tr> <tr><td><label for="price">Hinta</label></td> <td><input type="text" id="price" name="price" value="<?php echo $hinta; ?>"></td></tr> </table> <input type="submit" name="someAction" value="Lähetä" /> </form> </html> Something little missing here. I have code which gets stock_quantity and price when submitting product sku. It works fine. I get echoed right results with that code. but problem is that I need to get them to text fields after submitting. Stock and price fields just stays empty. What is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/313943-put-values-to-text-fields-after-submit/ Share on other sites More sharing options...
Solution ginerjm Posted October 11, 2021 Solution Share Posted October 11, 2021 They are local to the function. Pass them or make them global 1 Quote Link to comment https://forums.phpfreaks.com/topic/313943-put-values-to-text-fields-after-submit/#findComment-1590885 Share on other sites More sharing options...
Barand Posted October 11, 2021 Share Posted October 11, 2021 (edited) In two words - variable scope. Variables created inside a function are not available outside of it. Have your function return an array return ["hinta" => $hinta, "stock" => $stock ]; Call $data = func(); then put those array values in your fields. [edit] PS Just return $haku. Edited October 11, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/313943-put-values-to-text-fields-after-submit/#findComment-1590886 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.