# How to avoid improper update of Dynamic in a Dialog?

I want to request user input of an expression of f(x) from a dialog and then use ToExpression on the input.

Questions:

1. When I execute the code below, I got r before I click the button. So that I get r before I actually enter the expression. Is it because I'm using Dynamic[input]?
2. How to execute the last line after I hit the button?

x = Table[i, {i, 20}];
input = "";
CreateDialog[
Grid[
{{"f(x)=", InputField[Dynamic[input], String]},
{"", "Input expression f(x)"},
{"", DefaultButton["OK", DialogReturn[ret = input]]}},
Spacings -> {0.5, Automatic},
Alignment -> Center
],
Modal -> True,
WindowTitle -> "Input information"];


Not sure why you define x and then never use it but despite that try this:

ret = DialogInput[
DialogNotebook[{
DynamicModule[{input},
Grid[{{"f(x)=", InputField[Dynamic[input], String]}, {"",
"Input expression f(x)"}, {"",
DefaultButton["OK", DialogReturn[input]]}},
Spacings -> {0.5, Automatic}, Alignment -> Center]
]
}],
ShowStringCharacters -> False,
WindowTitle -> "Input information"];

• Hi Mike, sorry about the confusion. I was trying to use the input expression to manipulate vector x. Your answer solved my question perfectly. Thank you very much! – Hayward Li Apr 2 '16 at 18:38