Reputation: 11
I need help figuring out correct PHP coding for isset function.
I am having problems understanding exactly what I need to do with the isset()
function. Below I have the assignment directions, I have completed up to the end of part b, using the isset()
function to check that the parameters exists in the query string. When I built the form, the form action is "GET", because it needs to be in the query string, is that also correct?
My only two variables are Value1
and Value2
, which are input by the user.
SimpleCalculator.php - Write a script that retrieves two values from the querystring, adds them together and displays the results.
Steps:
Copy the code from HelloForm.php and modify it to display two text boxes. Assign the values entered to variables named
value1
andvalue2
.In your PHP code assign the parameters from the querystring to local variables named
$value1
and$value2
. You will need to use theisset()
function to check that the two parameters are in the querystring.Echo the variables to the browser to make sure they have been retrieved correctly.
Add and print the values with the statement:
echo 'Sum is: ' . ($Value1 + $Value2);
This is what I have so far:
<html>
<head>
<title>Sample web form</title>
<link href="/sandvig/mis314/assignments/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="pageContainer centerText">
<h2>Simple Calculations </h2>
<hr />
<form method="get" >
Please enter two numbers:<br><br>
Value 1: <input type="text" name="Value1" autofocus> <br><br>
Value 2: <input type="text" name="Value2" autofocus> <br><br>
<input type="submit" value="Add">
</form>
<?php
$value1 = $_GET ['Value1'];
$value2 = $_GET ['Value2'];
//Retrieve name from querystring. Check that parameter
//is in querystring or may get "Undefined index" error
echo 'Value 1 was:' . ($value1);
echo 'Value 2 was:' . ($value2);
echo 'Sum is: ' . ($value1 + $Value2);
?>
</div>
</body>
</html>
Upvotes: 1
Views: 2300
Reputation: 395
Consider this example of my get() function:
<?php
$arr = [];
var_dump( get($arr,'a','b') ); // NULL
$arr['a']['b'] = 'ab';
var_dump( get($arr,'a','b') ); // 'ab'
//var_dump(get($arr,'aa'));
/*
Get with safety
@author: boctulus
@param array
@param index1
@param index2
..
*/
function get(){
$numargs = func_num_args();
$arg_list = func_get_args();
$v = $arg_list[0];
for ($i = 1; $i < $numargs; $i++)
{
if (isset($v[$arg_list[$i]]))
$v = $v[$arg_list[$i]];
else
return null;
}
return $v;
}
You see ? you can get without any risk :)
Upvotes: 0
Reputation: 7269
Firstly, check this manual about isset()
function.
How it works?
$value1 = isset($_GET ['Value1']) ? $_GET ['Value1'] : null;
$value2 = isset($_GET ['Value2']) ? $_GET ['Value2'] : null;
Now, if user send something as Value1
parameter in query-string, so $value1
variable will be assigned with that data; if no null
will be assigned. Same for value2
.
Simple :)
Upvotes: 1
Reputation: 8960
<?php
if(isset($_GET))
{
$value1 = isset($_GET ['Value1']) ? $_GET ['Value1'] : null;
$value2 = isset($_GET ['Value2']) ? $_GET ['Value2'] : null;
//Retrieve name from querystring. Check that parameter
//is in querystring or may get "Undefined index" error
echo 'Value 1 was:' . ($value1);
echo 'Value 2 was:' . ($value2);
echo 'Sum is: ' . ($value1 + $Value2);
}
?>
Upvotes: 0
Reputation: 270
It does not say what to do if the value is not set, however this is the best way to get the values. You could display an error if $value1 or $value2 is not set. In the example below, they will be null.
$value1 = isset($_GET['Value1']) ? $_GET['Value1'] : null;
$value2 = isset($_GET['Value2']) ? $_GET['Value2'] : null;
Upvotes: 0