Hello - I'm very new to coldfusion and programming...
# cfml-beginners
a
Hello - I'm very new to coldfusion and programming for that matter. I'm trying to create a quadratic equation calculator program in Coldfusion. However - I don't know how to take the inputs from a user. I have a simple calculator function for the quadratic equation and have looked up various tutorials on how to take inputs. It appears that maybe I have to do a separate form but how do I pass information from that form into my program? Here's what I've done so far
Copy code
<cfscript>


    function computeQuadratic(a,b,c) {
        var discriminant1 = (-1 * b) + sqr((b*b) -(4*a*c));

        var discriminant2 = (-1 * b) - sqr((b*b)-(4*a*c));

        var root1 = discriminant1/(2*a)
        var root2 = discriminant2/(2*a)

        writeOutput ("<p> the answer for root1 is #root1#</p>");
        writeOutput ("<p> the answer for root2 is #root2#</p>");

    }  computeQuadratic(3,8,4);
a
Back up slightly. Submitting forms is an HTML thing, so perhaps best to start getting a proof of concept going there before worrying about the CFML side of things. Even before that... do you have a web server and CFML server set up that servers requests? eg, you can browse to
<http://localhost/some/path/someFile.cfm>
and it... works?
You probably need to go look at some basic HTML & CFML tutorials before getting to where you are starting from here.
Also... probs best to separate the output from the logic in that function. Functions should generally do one thing.
but don't worry about that for now.
a
Sort of - I've set up a server through commandbox
Ahh ok
a
OK so the CFML docs will probs explain how to create an HTML form and submit it to another URL and process the results.
Ugh. Let me just bleat @ @Mark Takata (Adobe) over in the docs channel for a second...
😳 1
s
if you put this in an index.cfm page and fill in the form, it may help you see how user input is getting used in coldfusion
Copy code
<cfdump var=#form#>
<form action="index.cfm" method="post">
    <div> Quadratic Formula Calculator</div>
    <div class="calculatorInputs">
        ax^2 + bx + c = 0
        <div>
            <label for="a">a =</label>
            <input name="a" id="a" value="1">
        </div>
        <div>
            <label for="b">b =</label>
            <input name="b" id="b" value="2">
        </div>
        <div>
            <label for="c">c =</label>
            <input name="c" id="c" value="3">
        </div>
    </div>
    
    <input type="submit" value="Calculate" >
</form>
NOTE: this code works because you are creating it as an
index.cfm
page and the form
action
is submitting the form to the
index.cfm
page
the
<cfdump var="#form#">
is taking the user input in the HTML form once its submitted and allowing you to use it in coldfusion via the
form
scope
a
(there are docs for form handling here - https://helpx.adobe.com/coldfusion/developing-applications/requesting-and-presenting[…]retrieving-and-formatting-data/using-forms-in-coldfusion.html - but they are terrible and outdated and someone @ Adobe should be shot. So ignore those)
😂 1
m
Hey let's not go shooting anyone Adam. Gently scolding will do just nicely.
😀 1
a
Can we compromise and gently shoot them?
😂 1
@Amado can I recommend you run through https://www.learncfinaweek.com/ before you dive too deeply into stuff that is gonna need a lot of explaining and a lot of cross references to the docs to be helpful?
a
This is amazing! Thank you so much Adam and Scott!
a
It'll be way easier to get stuff done with a bit of initial groundwork
a
@Adam Cameron ok - I'll give that try - thanks for the link
a
Ping questions here though. I don't mean to suggest "go away and do that". Scott's given you a good start with the code above.
b
@Amado Since you're already using CommandBox, there is another way to play with CFML from the commandline that does not require a web server or a browser
You can use a Task Runner to test your logic by creating a task with the command
Copy code
task create --open
and just put your code in the
run()
method
Copy code
component {

	function run(
		required a,
		required b,
		required c
	) {

		var discriminant1 = (-1 * b) + sqr((b*b) -(4*a*c));

		var discriminant2 = (-1 * b) - sqr((b*b)-(4*a*c));

		var root1 = discriminant1/(2*a)
		var root2 = discriminant2/(2*a)

		print.line("the answer for root1 is #root1#");
		print.line("the answer for root2 is #root2#");
	}

}
now run it like
Copy code
❯ task run :a=1 :b=50 :c=100
the answer for root1 is -2.087121525221
the answer for root2 is -47.912878474779
OR just type
Copy code
task run
and it will prompt you for the required values!
Copy code
❯ task run
Enter a :1
Enter b :50
Enter c :100
the answer for root1 is -2.087121525221
the answer for root2 is -47.912878474779
a
Oh yeah - this is great!
@Adam Cameron I didn't get that impression at all from your response at all - I want to learn this well and I do have quite a bit of work to do.
2
a
Nice one 😄