Jump to content

Put values to text fields after submit


Temporary_Failure
Go to solution Solved by ginerjm,

Recommended Posts

<?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?

Link to comment
Share on other sites

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 by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.