Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #16053
| From | Matthew Carter <m@ahungry.com> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: test prog inpu tpost |
| Date | 2016-01-01 21:40 -0500 |
| Organization | Ahungry (http://ahungry.com) |
| Message-ID | <87fuygd7qu.fsf@ahungry.com> (permalink) |
| References | <n64mdp$ppg$1@reader1.panix.com> <kplhy.63798$MN2.2950@fx07.iad> <n662gh$hm$1@jstuckle.eternal-september.org> <5686B656.3040603@arnowelzel.de> <n66ikt$thi$1@jstuckle.eternal-september.org> |
Jerry Stuckle <jstucklex@attglobal.net> writes:
> On 1/1/2016 12:24 PM, Arno Welzel wrote:
>> Jerry Stuckle schrieb am 2016-01-01 um 15:32:
>>
>>> On 12/31/2015 9:22 PM, Lew Pitcher wrote:
>>>> On Thursday December 31 2015 20:57, in
>>>> comp.lang.php, "vjp2.at@at.BioStrategist.dot.dot.com"
>>>> <vjp2.at@at.BioStrategist.dot.dot.com> wrote:
>>>>
>>>>>
>>>>> Should this work?
>>>>
>>>>> I'm stuck with lynx browser for a few days and can't tell
>>>>
>>>> That shouldn't stop you, assuming that you have a PHP-enabled webserver to
>>>> host the page on.
>>>>
>>>> Lynx, like all browsers, just renders the html sent to it by the server.
>>>>
>>>> PHP code executes on the server.
>>>>
>>>> If the html with PHP is served from a PHP-enabled web server, lynx will render
>>>> the results correctly.
>>>>
>>>>> <html>
>>>>> <head>
>>>>> <title>Quadratic</title>
>>>>> </head>
>>>>> <body>
>>>>> <?php
>>>>> $t;
>>>>
>>>> What is $t ?
>>>> Why are you trying to execute it as a php statement?
>>>>
>>>>> $b=$_POST['b'];$a=$_POST['a'];$c=$_POST['c'];
>>>>
>>>> Here, you should test whether the page is invoked from a GET or a POST. None
>>>> of the $_POST variables will be set on the initial GET, and your later
>>>> computation will be invalid.
>>>>
>>>
>>> In addition, you should check to see if the values are actually set.
>>> Even if it came here from a POST request, there is no guarantee the
>>> values have been set.
>>>
>>> $b = isset($_POST['b']) ? $_POST['b'] : '';
>>>
>>> puts an empty string in $b $_POST['b'] is not set. You could also use 0
>>> instead of '' or any other default value you wish.
>>>
>>>>> $x;$y;
>>>>
>>>> Same question as above with $t. What is $x? What is $y? Why are you trying to
>>>> execute two variables as php statements?
>>>>
>>>>> $t=(pow($b,2))-(4*$a*$c);
>>>>
>>>> Meaningless (and possibly errant) results on GET
>>>>
>>>
>>> The advantage to using '' as a default value above is you can test here:
>>>
>>> if ($a == '' || $b == '' || $c = '') {
>>
>> Of course this should be the opposite as one would do the calculation
>> only if *all* values are set and not if any of the value is empty:
>>
>> if ($a != '' && $b != '' && $c != '') {
>>
>> It would also be nice to see the values entered as default in the input
>> elements and to have valid HTML. And finally it is not neccessary to use
>>
>> echo sprintf(...)
>>
>> as there is printf() as well which outputs the result directly.
>>
>> So the whole script could be like this:
>>
>>
>> <!DOCTYPE html>
>> <html>
>> <head>
>> <title>Quadratic</title>
>> </head>
>> <body>
>> <?php
>> $a = isset($_POST['a']) ? $_POST['a'] : '';
>> $b = isset($_POST['b']) ? $_POST['b'] : '';
>> $c = isset($_POST['c']) ? $_POST['c'] : '';
>>
>> if ($a != '' && $b != '' && $c != '') {
>> $t=(pow($b,2))-(4*$a*$c);
>> if($t<=0)
>> $t=(-1)*$t;
>> $x=(-2*$c)/($b+(sqrt($t)));
>> $y=(-2*$c)/($b-(sqrt($t)));
>> printf(
>> "<p>Solution in Addition = %f in Subtraction = %f</p>",
>> $x, $y);
>> }
>> ?>
>> <form method="post">
>> <label>What is a?
>> <input type="number" name="a"
>> value="<?php echo htmlspecialchars($a); ?>"></label><br />
>>
>> <label>What is b?
>> <input type="number" name="b"
>> value="<?php echo htmlspecialchars($b); ?>"></label><br />
>>
>> <label>What is c?
>> <input type="number" name="c"
>> value="<?php echo htmlspecialchars($c); ?>"></label><br />
>>
>> <input type="submit">
>> </form>
>> </body>
>> </html>
>>
>>
>> But next time you promise to do your homework on your own, yes? ;-)
>>
>>
>
> Sorry, I meant the code to go in the else clause, with an empty then clause.
Also - unless you're using an outdated version of PHP for some reason,
you no longer have to write code like:
$x = isset($_GET['x']) ? $_GET['x'] : '';
You can use the null coalesce operator:
$x = $_GET['x'] ?? '';
--
Matthew Carter (m@ahungry.com)
http://ahungry.com
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
test prog inpu tpost vjp2.at@at.BioStrategist.dot.dot.com - 2016-01-01 01:57 +0000
Re: test prog inpu tpost Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-12-31 21:22 -0500
Re: test prog inpu tpost Matthew Carter <m@ahungry.com> - 2015-12-31 21:40 -0500
Re: test prog inpu tpost "J.O. Aho" <user@example.net> - 2016-01-01 09:40 +0100
Re: test prog inpu tpost Jerry Stuckle <jstucklex@attglobal.net> - 2016-01-01 09:32 -0500
Re: test prog inpu tpost Arno Welzel <usenet@arnowelzel.de> - 2016-01-01 18:24 +0100
Re: test prog inpu tpost Jerry Stuckle <jstucklex@attglobal.net> - 2016-01-01 14:07 -0500
Re: test prog inpu tpost Matthew Carter <m@ahungry.com> - 2016-01-01 21:40 -0500
Re: test prog inpu tpost Jerry Stuckle <jstucklex@attglobal.net> - 2016-01-01 21:53 -0500
Re: test prog inpu tpost Arno Welzel <usenet@arnowelzel.de> - 2016-01-02 11:39 +0100
Re: test prog inpu tpost Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-02 22:43 +0100
Re: test prog inpu tpost Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-12-31 21:39 -0500
Re: test prog inpu tpost vjp2.at@at.BioStrategist.dot.dot.com - 2016-01-07 03:19 +0000
csiph-web