Reputation: 939
I am trying to learn php today and I on the part of experimenting. I encountered problem using the drop down list, if and else and the function.
It's seems that its not working. I have no idea how to debug it. What I'm trying to do is that when the user selects "employed", it will simply return a "Do THIS!" text. but if the user selects any of the 3 (self employed, Voluntary member & OFW), it will display "DO THAT!".
It's really simple but i can't get it work, i just started php 6 hours ago. :)
Please help!
<form method="POST">
Salary: <input id="salarytext" type="text" name="salary" onkeypress="return isNumberKey(event)"><br>
Membership Type:
<select name="membershiptype">
<option value="employed">Employed</option>
<option value="SE">Self Employed</option>
<option value="VM">Voluntary Member</option>
<option value="OFW">OFW</option>
</select>
<br/>
<input type="submit" />
</form>
<?php
$a = (isset($_POST['salary'])) ? $_POST['salary'] : '';
$b = (isset($_POST['membershiptype'])) ? $_POST['membershiptype'] : '';
function employed () {
if (empty ($a)) {echo "";}
elseif ($a<10000) {$a * 2.0}
elseif ($a<20000) {$a * 2.3}
elseif ($a<30000) {$a * 2.7}
elseif ($a>30000) {$a * 3.0}
}
function sevmofw() {
if (empty ($a)) {echo "";}
elseif ($a<10000) { $a * 1.3}
elseif ($a<20000) { $a * 1.5}
elseif ($a<30000) { $a * 1.8}
elseif ($a>30000) { $a * 2.0}
}
if ( $_POST['membershiptype'] == 'employed' ){employed();
} elseif ( $_POST['membershiptype'] == 'SE' ){sevmofw();
} elseif ( $_POST['membershiptype'] == 'VM' ){sevmofw();
} elseif ( $_POST['membershiptype'] == 'OFW' ){sevmofw();
}
?>
Here's a flowchart of what i'm trying to do.
Upvotes: 0
Views: 5783
Reputation: 3447
Here's the answer for your help request after the question edit:
First of all, I love flow charts too! It's a clean way to plan a project or function.
I see you've used ternary operators. That's great :-)
Ok, so you've got a good idea of what you want to achieve (flowchart). I'll try to point you into the right direction with an example.
According to your flowchart "start" means the "form". This example shows the php part and expects that the form was submitted with the required data:
<?php
// START - Check if all required data was submitted
if ( isset($_POST['salary'], $_POST['membershiptype']))
{
// Get the form data
$salary = $_POST['salary'];
$member_type = $_POST['membershiptype'];
// USER SELECTS EMPLOYED?
$user_employed = ($member_type == 'employed') ? true : false;
// Create the multiplication table
// for the salary calculations
$multiBy = array(
'10000' => 1.3,
'20000' => 1.5,
'30000' => 1.8,
'30001' => 2.0,
'employed' => array(
'10000' => 2,
'20000' => 2.3,
'30000' => 2.7,
'30001' => 3.0,
),
);
// Create the calculation function
function calcSalary($s = 0, $employed = false)
{
// Use global here so you can access the
// $multiBy array from inside this function
// without the need to pass it separately
// like $s and $employed
global $multiBy;
// Check if $s is specified
if (empty($s)) return false;
// Round the $s value to be able to use
// it as key for the $multiBy array
// PHP Documentation: http://php.net/manual/en/function.round.php
$value = round($s, -4);
// Set the multiplication values
$multi = $multiBy;
if ($employed) $multi = $multiBy['employed'];
if ($value > 30000) $value = 30001;
if ($value < 10000) $value = 10000;
// Calculate the salary and return the result
return $s * $multi[$value];
}
// GET SALARY INPUT
// Putting the result in a var so you can process however you like
$calculated_salary = calcSalary($salary, $user_employed);
// END - Print the calculated salary
echo $calculated_salary;
}
else
{
// I guess this is self-explanatory :P
echo 'Please enter all required data and try again.';
}
This is a basic example. If you're going to accept user input (public form) remember to secure the input. With "secure" I mean htmlentities, CSRF (Cross Site Request Forgery), XSS (Cross Site Scripting), Code Injection (also SQL Injection if you're going to use Databases).
Also don't forget error handling e.g. missing form input (like if( ! isset($_POST['field'])) echo 'ERROR: Missing input'; else ...
).
Here are some resources related to php security you might find useful:
PHP Functions…
Articles with code examples…
and some StackOverflow Questions:
Have fun reading and happy coding!
Upvotes: 0
Reputation: 3447
I want to add this answer, because there is more potential in this topic and you're learning PHP so all bits add up.
Let's start with the PHP Tag. In your .php files you only need to add <?php
once at the top and no close tag ?>
at the end. Why? See this SO answer https://stackoverflow.com/a/4499749/2493918 .
Then put all the functions you want to be executed via the select in your script:
<?php
/**
* functions.php
*/
function employed()
{
return 'Employed function called.';
}
function sevmofw()
{
return 'Sevmofw function called.';
}
Note here that I replaced the " quotes with single ' quotes. Why? See this answer here: https://stackoverflow.com/a/3446286/2493918 .
Then create another .php file containing your form (let's call it form.php):
<?php include 'functions.php'; // include the functions ?>
<!DOCTYPE html>
<html>
<head>
<title>Form Function Test</title>
</head>
<body>
<?php
// Check if the form was submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset('membershiptype'))
{
// If the requested function exists, call it
if (function_exists($_POST['membershiptype']))
{
$func = $_POST['membershiptype'];
echo $func();
}
}
?>
<form action="" method="POST">
<div>
<label>
Salary: <input id="salarytext" type="text" name="salary" onkeypress="return isNumberKey(event);">
</label>
</div>
<div>
<label>
Membership Type:
<select name="membershiptype">
<option value="employed">Employed</option>
<option value="SE">Self Employed</option>
<option value="VM">Voluntary Member</option>
<option value="OFW">OFW</option>
</select>
</label>
</div>
<div>
<input type="submit" name="submit_btn" value="Submit form">
</div>
</form>
</body>
</html>
You see we include the functions into this forms page. This way you separate the functions from the so called view. Now you could include the same functions on other pages as well.
Then if you use PHP inside HTML you need to use both php tags <?php and ?>
to separate the php part from the html.
If you submit the form the php part will check if the form was submitted and if the membershiptype is set. Then it continues and checks if the requested function exists. If so, it calls the function and returns the output.
Remember: It's better to return values inside functions and echo them where you want (see the example above).
A good resource to learn about PHP are the docs which can be found here: For example the function_exists() documentation page http://www.php.net/manual/en/function.function-exists.php .
Try to switch to a PHP Framework as soon as you can. It'll help you immense. I recommend Codeigniter as it has a thorough documentation and a low learning curve. Later you could switch to something like Laravel, but I tell you, that's too soon now.
Good luck learning PHP :-)
Upvotes: 0
Reputation: 6854
<select name="membershiptype" method="POST">
<option value="Employed" name="employed">Employed</option>
<option value="SE" name="sevmofw">Self Employed</option>
<option value="VM" name="sevmofw">Voluntary Member</option>
<option value="OFW" name="sevmofw">OFW</option>
</select>
should be
<select name="membershiptype">
<option value="employed">Employed</option>
<option value="SE">Self Employed</option>
<option value="VM">Voluntary Member</option>
<option value="OFW">OFW</option>
</select>
and your code should be
if (isset($_POST['membershiptype'])) {
if ( $_POST['membershiptype'] == 'employed' ){
employed();
} elseif ( $_POST['membershiptype'] == 'SE' ){
sevmofv();
}
}
...
Upvotes: 1