Good evening, everyone.
Perhaps someone can help with the solution to this problem.

function demo_generator()
  local num1 = math.random(1, 10)
  local num2 = math.random(1, 10)
  local answer = num1 + num2
  return answer
end

local VariableB = demo_generator()

local test_html = [[
<!DOCTYPE html>
<html>
<body>

]] .. VariableB .. [[

<form action="" method="post">
  <label for="digs">Code:</label><br>
  <input type="text" id="digs" name="digs" value=""><br>
  <input type="submit" value="Submit">
</form>
</body>
</html>
]]

if ngx.req.get_method() == "POST" then
	ngx.req.read_body()
	local post_args = ngx.req.get_post_args()["digs"]

	ngx.say("user send : ", post_args)
	ngx.say("original value:", VariableB)
	return
end

ngx.say(test_html)

In this example, the code of the function , is called twice.
As a result, it is not possible to check what the user has entered with what we have received.
Perhaps someone can suggest a solution to this problem ?

3 months later

How did you confirm that the function was called twice?
Was it called in the same phase like access_by_lua_block?

    2 months later

    OpenResty 1.27v Release
    Can we know when OpenResty 1.27v Release will be provided for supporting latest nginx version which LTS
    1.25v LTS supported ended by May 2024
    Please could you check and do provide your response earliest we are waiting for this upgrade
    email id - hemanth.chitta@gmail.com

      2 months later

      Yes, I am agree with @zhuizhuhaomeng. I am also not sure about that so I would suggest you to watch this: cognos training.

      I hope this tutorial will help you to clear all the doubts.

      Thanks.

        5 months later

        why dont send new discussion

          11 days later

          Exactly! The function keeps generating a new value, making validation difficult. Storing VariableB in a session, cookie, or hidden form field would help maintain consistency while handling different flavors of user input

            12 days later

            In the provided Lua script, the function demo_generator() is only called once and the result is stored in the variable VariableB. However, you may be concerned about how the value of VariableB is being used and the potential misunderstanding that the function is being called twice.

            Here’s a breakdown of the code behavior:

            First call to demo_generator(): The function is called once at the beginning and stores the generated result in VariableB. This is done when you assign VariableB = demo_generator().

            HTML generation: The value of VariableB is then used in the HTML content in the line ]] .. VariableB .. [[. This means that VariableB (which holds the sum of num1 and num2 generated by demo_generator()) is dynamically inserted into the HTML.

            Handling POST Request: After the user submits the form, the script checks if the request method is POST. If it is, it reads the submitted value from the input field digs and compares it with the original value of VariableB (which is stored in the variable). It then prints both the submitted value and the original value.

            Why might you think it's called twice?
            The potential confusion could arise because:

            The VariableB value is used in the HTML output and compared in the POST request handling section.
            However, demo_generator() is only called once, at the point where VariableB is initially assigned. The value of VariableB does not change unless the function is explicitly called again.
            Possible Solution for Calling demo_generator() Twice
            If your intent is to generate a new random value each time the form is submitted (so that the function is called both when the page is first generated and when the form is submitted), you should call the function demo_generator() again when handling the POST request.

              8 days later

              Qualitest is a global leader in AI-powered quality engineering, offering advanced automation, performance, and security testing solutions. We help businesses enhance software quality, accelerate releases, and ensure seamless digital experiences across industries.

                Write a Reply...