I am trying to pass a value from a textbox to another textbox (Same window) with conditions, Lets say. If the value from the first textbox is 1 then the output on the other textbox will be 2, and if the first textbox is 2 then the output on the other will be 3 and so on so forth.
Here is what Ive done so far.
Text box 1:
<input type="text" name="basic_salary" value="<?php
if ($emp_salary->basic_salary <= 15999) {
echo $emp_salary->provident_fund = 120;
} elseif ($emp_salary->basic_salary >= 16000) {
echo $emp_salary->provident_fund = 1;
} else {
echo "Have a good night!";
}
?>" class="form-control">
and for the textbox 2:
<label class="control-label" >SSS </label>
<input type="text" name="provident_fund" value="<?php
if (!empty($emp_salary->provident_fund)) {
echo $emp_salary->provident_fund;
}
?>" class="form-control">
am using Codeigniter. Thank you!
I would do this in the controller before passing the data to the view.
// defaults
$content_data['basic_salary'] = $emp_salary->basic_salary;
$content_data['provident_fund'] = "";
// exception(s) - can be expanded
if ($emp_salary->basic_salary <= 15999) {
$content_data['provident_fund'] = 120;
}elseif ($emp_salary->basic_salary >= 16000) {
$content_data['provident_fund'] = 1;
}
$this->load->view('content', $content_data);
Then in your view do something like
<input type="text" name="basic_salary" value="<?php echo $basic_salary; ?>">
<input type="text" name="provident_fund" value="<?php echo $provident_fund; ?>">
Might need some tuning from your side.
Related
My goal: I wanted to make a list system that stores an array that has 3 values...
"Product's Name",
"Product's Price",
"Amount of Products".
And I want the user to keep adding products and be able sum it all up by multiplying the price by products and summing it all together if there's two or more products in the array.
Expectations:
"Milk",
2.99,
40
"Apples",
3.00,
5
My problem is the array input is replacing index 0.
Result:
Milk
2.99
40
I tried adding the other value, it replaces index 0.
I need some help to understand this problem.
<html>
<body>
<input id="inventory" name="p-name" type="text" placeholder="product name">
<input id="inventory" name="p-price" type="text" placeholder="product name">
<input id="inventory" name="p-amount" type="text" placeholder="product name">
<input id="inventory" name="s-name" type="text" placeholder="product name">
<input id="inventory" name="s-price" type="text" placeholder="product name">
<input id="inventory" name="s-amount" type="text" placeholder="product name">
<input type=submit name=submit[AddToList] value='Add to list'>
<input type=submit name=submit[ClearAllList] value='Clear All List'>
<?php
$listOfInventories = array();
if (isset($_POST["submit"])) { // Checks if user clicked btn.
$sub = $_POST["submit"];
$time = $_POST["time"];
$pName = $_POST["p-name"];
$pPrice = $_POST["p-price"];
$AmountOfProducts = $_POST["p-amount"];
$sName = $_POST["s-name"];
$sPrice = $_POST["s-price"];
$TimesWorkersLabored = $_POST["s-amount"];
if (isset($sub["AddToList"])) {
echo " Added to list <br>";
array_push($listOfInventories, array($pName, $pPrice, $AmountOfProducts)) ;
foreach ($listOfInventories as $value) {
foreach ($value as $x) {
echo $x;
}
}
echo count($listOfInventories);
// Save something;
} elseif (isset($sub["ClearAllList"])) {
$listOfInventories = [];
// Delete something
}
?>
</body>
</html>
Adding first product
Adding second product
Edit1: Possibly the fault is the initialization array, but where should I put it? It might not work before the array_push.
Please use SESSION to do what you want (this is a rather standard way to handle things like "shopping cart")
Hence, try something like $_SESSION["listOfInventories"] = array(); and then array_push($_SESSION["listOfInventories"], array("Apple", 10.5, 1)) ;
Please try to run the following PHP script, and RELOAD to see the effect - it will preserve the data and add more item(s)
<?php
session_start();
if (!isset($_SESSION["listOfInventories"]))
{$_SESSION["listOfInventories"] = array();}
array_push($_SESSION["listOfInventories"], array("Apple", 10.5, 1)) ;
echo count($_SESSION["listOfInventories"]);
echo "....<br>";
array_push($_SESSION["listOfInventories"], array("Orange", 10.5, 1)) ;
echo count($_SESSION["listOfInventories"]);
echo "....<br>";
?>
Use method="POST", put your Type="submit" attribute to Submit in Double Quote and Value="".. also put your } at the end of the if statement, add session_start() into the Login file and call only the number of inputs in the Field. use Localhost to store your Timestamp.
I have few checkboxes, I want to find whether all the checkboxes are checked and if yes return a message.
<label class="control-label col-md-3">L4 Deliverables</label>
<?php
while($subd_row=$subd_result->fetch_assoc()){
if($sub_row['selected'] == 1)
{
?>
<input class="flat" type="checkbox" name="L4d[]" value="<?php echo $subd_row['d_name'];?>" checked><?php echo $subd_row['d_name'];?></input>
}
Using the above code the checkboxes are displayed. The message could be for example: " 14 checkboxes are checked".
You can use $i to increment when it goes in that if statement it will increment,
<label class="control-label col-md-3">L4 Deliverables</label>
<?php $i = 0;
while ($subd_row = $subd_result->fetch_assoc()) {
if ($sub_row['selected'] == 1) {
$i++;
?>
<input class="flat" type="checkbox" name="L4d[]" value="<?php echo $subd_row['d_name']; ?>" checked><?php echo $subd_row['d_name']; ?></input>
<?php
}
}
?>
<label><?php echo ($i <= 1 ? "$i checkbox is ": "$i checkboxes are ")."checked"; ?></label>
On the backend, you can always check the length of the variables/array passed by a form.
L4d[] will have the values of checked checkboxes only.
You can simply check as:
if(count($_POST['L4d']))== 14) {...}
If you want something like an alert box to be popped up when all the checkboxes are checked, then you may call a javascript function 'onChange' of your checkbox field
I'm on the learning mode.
<?php
if (!empty($_POST['fifty']) || !empty($_POST['sixty'])) {
$fifty = (isset($_POST['fifty'])) ? (int)$_POST['fifty'] : 0;
$sixty = (isset($_POST['sixty'])) ? (int)$_POST['sixty'] : 0;
echo $fifty + $sixty;
} else {
echo "No selection selected";
}
?>
<form method="post">
<input type="radio" name="fifty" value="50"/>
<input type="radio" name="sixty" value="60"/>
<input type="submit" name="submit" value="Submit"/>
</form>
This only works when I select both radio buttons. How do I make this work when I just select 1 radio button instead of 2?
And is the way I coded the PHP the good way to write it? I get an idea that I do double work with checking :P
And how do I do it with three radio options? Can you give me a example with a third radio option called seventy with value 70?
My idea is to make it + count the values if 1 and 3 are slected it must to 50+70
if 2 and 3 are selected it must do 60 + 70, etc, etc.
Use ||for OR, && for AND
if(!empty($_POST['fifty']) || !empty($_POST['sixty'])){
$fifty = (isset($_POST['fifty']))? (int)$_POST['fifty'] : 0;
$sixty = (isset($_POST['sixty']))? (int)$_POST['sixty'] : 0;
echo $fifty + $sixty;
} else {
echo "No selection selected";
}
A few things to change.
The <input /> tags.
<input type="radio" name="fifty" value="50" />
<input type="radio" name="sixty" value="60" />
The logic you used (which you have corrected).
if (!empty($_POST['fifty']) || !empty($_POST['sixty']))
Remove unnecessary code:
$submit = $_POST['submit'];
Currently I have 100 text input boxes named contact0 through contact101. I am trying to get the Post data and name each string to itself. Meaning $contact0 = $_POST['contact0'];all the way up to $contact101 = $_POST['contact101']; there has to be a simpler way to set them in a loop or something. Overall the end result is I just want the data entered in the textbox to become the value of the textbox when submitted. Any suggestions will help I might be doing this wrong for the results I want.
for ($i = 0; ;$i++){
if($i < 101){
$contact.$i = $_POST['contact'].$i;
echo $contact.$i;
}
else{
break;
}
}
for ($i = 0; $i <= 101; $i++){
${"contact".$i} = $_POST['contact'.$i];
echo ${"contact".$i};
}
You may want to consider amending your HTML form. Rather than create 100 new variables, you can assign all the contacts to an array and then reference them with the array index.
HTML
<input type="text" name="contacts[]" value="Contact 1 name"/>
<input type="text" name="contacts[]" value="Contact 2 name"/>
// etc...
PHP
$contacts = $_POST['contacts'];
var_dump($contacts);
// prints array(0 => 'Contact 1 Name', 1 => 'Contact 2 Name'...
As it now an array, you can reference the contact e.g. $contacts[34] and know it will be a valid entry.
EDIT
A working example:
<?php
if (isset($_POST['contacts'])) {
$contacts = $_POST['contacts'];
echo "<p>The contacts are below:</p>";
print_r($contacts);
} else {
echo "<p>Please enter the contacts</p>";
}
?>
<form method="post" action="">
<?php for($x = 0; $x < 100; $x++): ?>
<input type="text" name="contacts[]" value="<?php echo (isset($contacts[$x])) ? $contacts[0] : ''; ?>"/>
<?php endfor; ?>
<input type="submit" name="submit" value="submit"/>
</form>
Edit 2
I have now made the form a loop, meaning it will create all our contact inputs for us. On top of this, if we have already posted the form each field will have the correct contact values.
If you are not aware of the syntax, echo isset($contacts[$x])) ? $contacts[$x] : ''; is a ternary operator which is a one line if/else statement.
Which can also be tested here: http://phpfiddle.org/api/run/ugb-cta
I have a form where the user clicks various radio groups, this generates a 'score' that is calculated with javascript, and populates a textbox at the top of the form with a running total.
When the form is submitted, the POST value for this textbox is empty, so when it moves onto the output sheet there is no value to display.
All other textboxes that are manually typed in work fine. All Names/Ids etc are correct, is there something I'm missing in order to get it to correctly retain the value generated by the JS in the POST value when the form is submitted?
This (populated from JS) doesn't work :
<input name="Result_AutoFail" type="text" class="ResultsBox" id="Result_AutoFail" size="2" maxlength="2" value="<?php echo htmlspecialchars($_POST['Result_AutoFail']); ?>"/>
but this (Manually typed in) does:
<input name="CustName" type="text" id="CustName" size="25" maxlength="25" value="<?php echo htmlspecialchars($_POST['CustName']); ?>"/>
The JS used to populate the box is this for each question, this works fine and puts the value into the box correctly as the user clicks each radio button for each question, just doesn't send it off to POST:
for (i=0;i<2;i++)
{
if (document.MonitorForm.SBI_CA027[i].checked == true)
{
CA027Selected = document.MonitorForm.SBI_CA027[i].value
}
}
if (CA027Selected == "Yes")
{
if (AutoFailCount == 0)
{
document.MonitorForm.Result_AutoFail.style.backgroundColor = "#008000";
}
else if (AutoFailCount >= 1)
{
document.MonitorForm.Result_AutoFail.style.backgroundColor = "#FF0000";
}
}
else if( CA027Selected == "No")
{
AutoFailCount = (AutoFailCount + 1);
if (AutoFailCount == 0)
{
document.MonitorForm.Result_AutoFail.style.backgroundColor = "#008000";
}
else if (AutoFailCount >= 1)
{
document.MonitorForm.Result_AutoFail.style.backgroundColor = "#FF0000";
}
}
Then is written into the textbox with:
document.MonitorForm.Result_AutoFail.value = AutoFailCount ;
Cheers!
Try writing the result into hidden input.
<input type="hidden" value="some_value" name="result" id="result" />
if you dont submit it from a formfield like input or textfield it wont come in the $_POST array
if you want to submit additional data in your post you have to write them in hidden inputs