Js script doesn't read default value moment.js - javascript

I made a little page to track project times and write them into a database. I have two input fields for start and end time that will be calculated via a small JS script and the help of moment.js to show the total hrs. When there are values in the database already they are placed into the input fields as default value. When I now just tab through the input fields the script will calculate the value 0. I inspected the DOM and on both input fields it is correctly showing the right timestamps for both value and defaultvalue. What am I missing, and why is the JS triggered if I set it to be onChange?
JS
function TimeDifference(a) {
var start = moment(document.getElementById('Start_'+a).value,'HH:mm');
var stop = moment(document.getElementById('Finish_'+a).value,'HH:mm');
document.getElementById('sum'+a).value = moment.duration(stop-start).asHours();}
HTML
<input class='timebox' type='text' id='Start_" . $i . "' onkeypress='return isNumberKey(event) ' onChange='TimeDifference(" . $i . ")' value='{$db_start}' >
<input class='timebox' type='text' id='Finish_" . $i . "' onkeypress='return isNumberKey(event)' onChange='TimeDifference(" . $i . ")' value='{$db_stop}'>
<input id='sum" . $i . "' class='sumbox' readonly disabled='disabled' style='border:0px;'>

Related

How to handle inputs with the same name in php to add/update them in the database?

I have an HTML input form which is have an unknown number of inputs in some stages, like when the user wants to enter more than one document he click on an add button so a new field appears to him (using JavaScript for sure).
I make the names of the inputs just the same but in an array like <input name="something[]"/> .. the problem is I don't know if what I am doing gonna work or not ..
HTML PART
<input name="uat[]" type="date"/>
</td>
<td>
<input name="uatedate[]" type="date"/>
</td>
<td>
<input name="pgldate[]" type="date"/>
PHP PART
$j=0;
while(j<100){
$sql[j]= "insert into project scope values ('$uat[j]','$uatedate[j]' , '$pgldate[j]' ;)";
$j++;
}
^^ Just before the "try"
Replace your PHP PART code with this code:
$j = 0;
while ($j < count($_POST['uat'])) {
$sql[$j] = "insert into project scope values (" . $_POST['uat'][$j] . "," . $_POST['uatedate'][$j] . "," . $_POST['pgldate'][$j] . ")";
$j++;
}

2-step dynamical echo of a php in innerHTML

step 1: I have a form where input fields will be generated dynamically by innerHTML.
var d = document.getElementById("d1p_1");
d.innerHTML += "<input class='add' name='field_" + i + "' type='text'>";
step 2: now I would like to echo a php variable of each value to each dynamically generated input field. Something like:
d.innerHTML += "<input class='add' name='field_" + i + "' type='text' value='"<?php echo $field_" + i + "; ?>" + "'>
How can I archieve that?
Thanks in advance.
To give further information:
This is for a form where an user can add fields, depending from how many fields he needs and will be adding. Now it could be that an user adds more fields than he usually needs and enters data somewhere between field_1 and field_280. To catch the field_x where he entered data I need to echo the value of that input field.
$field_1 = value of field_1 if given;
...
$field_280 = value of field_280 if given;
The innerHTML will add the input fields dynamically by a counter for i. So I dont know what input will be given on which field. Thats why I need to generate the PHP echo part dynamical as well. Like:
<?php echo $field_" + i + "; ?>
The whole process:
form -> contains first just one input field (user will make some inputs) -> by clicking a button there will be added further input fields (1 click 1 field and user does not need to fill in data before he can add another fields) -> now imagine that a user will add 3 fields and has given input on first and third input field -> name="field_1" and name="field_3" ->
the name of each input field is generated by i++ -> the value is empty otherwise the form will be posted -> now the user will submit the form.
this means the value to echo would be $field_3 = (isset($_POST['field_3'])) ? $_POST['field_3']; : ''; -> this variable exist for all i so each variable is set in the php part before BUT to catch the right input name="field_i" with $field_i and to echo this match is the problem.
So by clicking submit the page would be reloaded and instead of only just one input field like from before now would be there 2 input fields. first would be name="field_1" and the second would be name="field_3" because user has left out input name="field_2" before. So name="field_3" needs to echo the right value depending from a dynamically generated name="field_"+ i +"what means that when the name tag is generated dynamically the php tag needs also to be generated dynamically to match each other.
i is a JavaScript variable so including it in a php declaration is giving you problems
You may implement your string concatenation out of the php code as follows
<?php
$field_="stavo";
?>
<script type="text/javascript">
var i=10;
var d = document.getElementById("d1p_1");
d.innerHTML+= "<input class='add' name='field_" + i + "' type='text'>";
d.innerHTML += "<input class='add' name='field_" + i + "' type='text' value='<?php echo $field_; ?>"+i+"'>";
</script>
Of course you must be having this in your Html
<div id="d1p_1"></div>

Create a submit form (wrap the table into a form and show it as a table again) from the products that were ordered

EDIT: Maybe its easier to create a new column in the database. Named: to_order.
Then create an UPDATE Query for each product . Something like : UPDATE products SET to_order = '[VALUE_INPUT]' WHERE id = '[ID_PRODUCT]'
The problem here is, where to excecute this query? How can I define the VALUE_INPUT and ID_PRODUCT for each row?
If this works, and I can UPDATE each row for that specific product etc.
I can easily create a mysqli_fetch_assoc again where only the [Product_name],[to_order] will be given.
Please help.
I'm working on a supplier order system. Where the manager of the restaurant can easily select a supplier, fill in how much stock he has now of that specific product. And it will automatically calculate how much you need to order.
This part is done.But now, once we fill in the form I want to get an overview of all products you need to order, and it needs to be printable.
For example in words explained:
We have some columns in our table [ID_product][ID_supplier][Product_name][Stock][Minimum][To_order]
ID_product, ID_supplier, Product_name and Minimum are all data from the database. Stock is what you need to fill in. And To_order is calculated by : Minimum-stock = To_order. (logic)
With a mysql_fetch_assoc command we can show all specific products with its specific id and minimum integer.
Now here is the part where my question is: Once everything is filled in, you need to click a button that refers to a next page. On this page your total input is shown, a full list.
Like: [Product_name][To_order]
So on this page you get an overview of your form where you filled in all these values. So you get a list (how big depends on how much products you have in your database) with all the calculated inputs from 'To_order'.
My problem is, if I create a Form Action into my fetch_assoc, it can read all element names, but as soon you submit the form and go to the next page. All the data is lost.
I need something where I can see the value from the previous page of that input. And then for all specific products.
My form.php (Where I need to fill in my stock in order to calculate the 'To_order' input). This is working fine.
<table width="600" border="1" cellpadding"1" cellspacing= "1" class="flatTable">
<tr class="headingTr">
<th>
Supplier code
</th>
<th>
Product
</th>
<th>
Stock
</th>
<th>
Minimum
</th>
<th>
To order
</th>
</tr>
<?php
while ($producten=mysqli_fetch_assoc($result_producten)) {
echo "<tr>";
echo "<td><form method='POST' action='lijst.php'><label name='".$producten['lev_id']."'>".$producten['lev_id']."</label></td>";
echo "<td><label name='".$producten['productnaam']."'>".$producten['productnaam']."</label></td>";
echo "<td>
<input id='".$producten['minimum']."' name='".$producten['id']."' type='text'
oninput='calculateBestelling(this.value,this.name, this.id)'
onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
</td>";
echo "<td><input id='".$producten['minimum']."' name='mytext2' type='text' readonly='true' value='".$producten['minimum']."' /></td>";
echo "<td><input id='".$producten['id']."' name='order[]' type='text' readonly='true' /></td>";
echo "</tr>";
}
?>
<script>
var minimum;
var stock;
var order;
function calculateBestelling(val,name,id){
minimum = document.getElementById(id).id;
stock = document.getElementById(id).value;
document.getElementById(name).value = minimum - val;
order = document.getElementById(name).value;
if (order < 0) {
document.getElementById(name).value = '0';
}
}
</script>
</table>
<p><input type='submit'/></p></form>
Then the next page (where the overview needs to be shown):
<header>
<h2>Complete order form - Supplier: (HERE THE SUPPLIER)</h2>
</header>
<div class="container">
<div class="row">
<table width="600" border="1" cellpadding"1" cellspacing= "1" class="flatTable">
<tr class="headingTr">
<th>
Product
</th>
<th>
To order
</th>
</tr>
<?php
//And I dont know what I need to do here. I want the values from the 3rd input the previous file. But how I can combine this with each row for a specific product?
while ($producten=mysqli_fetch_assoc($result_producten)) {
echo "<tr>";
echo "<td>PRODUCT HERE</td>";
echo "<td><input id='toorder' name='order[]' type='text' readonly='true' value='(THE VALUE OF THE PREVIOUS FILE FOR THAT PRODUCT)' /></td>";
echo "</tr>";
}
?>
</table>
</div>
</div>
I created an image, where you simple can see what I have in my mind.
URL TO IMAGE: http://i.stack.imgur.com/6AnTl.png
So we also need to check something like : if (!to_order > 0) { DONT SHOW ROW }
Feel free to change codes, maybe some other way that works? Like staying on the same page, and hide the stock and minimum values. So we only can see the ID's, Product names and To_order values?
Web pages are stateless, in other words, each request is separate to the web server. Because of this there is no link between two requests, for example form1 and form2.
In order to overcome this, you must use some form of storage that will persist across two (or more separate requests). This is called persistent storage. For long term persistent storage, use databases. For short term, use PHP sessions. You can read up on PHP sessions in the PHP manual.
To store your values from form 1, save the values into session.
In your form, you will point your form action to form1.php
form1_view.php:
<form action=<?php echo "form1.php" method="post">
<input name="field1">
<input type="form1_submit">
</form>
and your form input handler is form1.php
if (isset($_POST['form1_submit']) {
session_start();
$_SESSION['form1_inputs'] = serialize($_POST);
}
Then, when receiving the valued from form2, retrieve the values you stored from form1.
form2_view.php:
<form action=<?php echo "form2.php" method="post">
<input name="field2">
<input type="form2_submit">
</form>
Form2 is handled by its own handler.
form2.php
if (isset($_POST['form2_submit']) {
session_start();
$form1_values = unserialize($_SESSION['form1_inputs']);
$form2_values = $_POST;
// combine input from both forms into one variable.
$all_form_values = array_merge($form1_values, $form2_values)
// You can now save values from both forms. How you do this
// ...will depend on how you save the values. This is an example.
save_my_values($all_form_values);
}
After hours trying I finally figured it out.
I used $_SESSIONS to store data in. Thats one thing.
Then, made sure that the inputs are all array.
while ($producten=mysqli_fetch_assoc($result_producten)) {
echo "<form method='POST' action='verwerken.php'><tr>";
echo "<td><input name='ids[]' type='text' readonly='true' value='".$producten['id']."' /></td>";
echo "<td><input name='lev_id' type='text' readonly='true' value='".$producten['lev_id']."' /></td>";
echo "<td><input name='producten[]' type='text' readonly='true' value='".$producten['productnaam']."' /></td>";
echo "<td>
<input tabindex='1' id='".$producten['minimum']."' name='".$producten['id']."' type='text'
oninput='calculateBestelling(this.value,this.name, this.id)'
onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
</td>";
echo "<td><input id='".$producten['minimum']."' type='text' readonly='true' value='".$producten['minimum']."' /></td>";
echo "<td><input id='".$producten['id']."' name='test[]' type='text' readonly='true' /></td>";
echo "</tr>";
}
This is the while for the table.
Once you filled in all your STOCK values, it will automatically calculate the value to order.
Thats working fine. Now when you click the submit button a next page will be openend.
This page only inserts a query into a database.
For every row it will update the to_order value in the database.
$order_test = $_SESSION['hoeveelheid'] =$_POST['test'];
$ids_test = $_POST['ids'];
$producten = $_SESSION['product'] = $_POST['producten'];
foreach (array_combine($producten, $order_test) as $producten => $order_test) {
echo 'Product: ' . $producten . ' - Te bestellen: ' . $order_test.'<br>';
mysqli_query($conn, "UPDATE producten SET bestellen = '".$order_test."' WHERE productnaam ='".$producten."'");
}
$_SESSION['lev_id'] = $_POST['lev_id'];
$_SESSION['check'] = 'true';
header('Location: bestelformulier.php');
exit();
}
If the database is not that slow, you should not see this page.
After the query is done, you will be redirected to the overview page.
Bestelformulier.php.
Here you simply mysqli_fetch_assoc the values from the database again. And you have an updated form.
Thanks everyone for the help (:

Get database value from generated PHP-HTML table

I have been struggling with this for some time and cannot get it to work.
I have a form that sends a request to a php page. The php page returns the records
and outputs them as an html table. Certain fields in the table have a radio button, so that
the user can select that value. That value should be stored in an area, like a cart,
on the page. Here is what I have:
PHP page snippet:
while (oci_fetch($stmt)){
echo "<tr>\n"
. "<td class='v1'><input type='radio' name='price' value='STANDARD' id='rbt'>" . oci_result($stmt, 'STANDARD') . "</td>\n"
. "<td class='v2'><input type='radio' name='price' value='PREMIUM' id='rbt2'>" . oci_result($stmt, 'PREMIUM') . "</td>\n"
....
. "</tr>\n";
}
HTML code:
<td class='v1'><input type='radio' name='price' value='STANDARD' id='rbt'>798,4</td>
<td class='v2'><input type='radio' name='price' value='PREMIUM' id='rbt2'>965,87</td>
JavaScript:
$(document).ready(function(){
$("#rbt, #rbt2").click(function(){
alert($('input[name=fare]:checked').val());
});
});
This only shows an alert, which is not the goal.
So the question is: When a value, i.e. 798,4, is selected, it must be stored in the cart for a later calculation. Would JQuery solve this?
Any help would be appreciated!
$(document).ready(function(){
$("#rbt, #rbt2").click(function(){
//alert($('input[name=fare]:checked').val()); // this value come whcih is "STANDARD"
//so use
alert($(this).closest("td").text()); //alert 798,4
//to store in a div html use html();
$("divid").html($(this).closest("td").text());
});
});

Javascript - Checkbox onclick-+ operations

i'm working on a project and try to do something with checkboxes.
when a user click to a checked checkbox total value is - reverse is +
this is my code. i use recal function to do that check box is checked and send it to js as true
echo "<td><input type=checkbox name='check1' value='".$info['eventCategory']."' onclick='recal(" . $info['totalEvents'] . ",true)' checked></td><td>" . $info['id'] . " " . $info['name'] . "</td>";
function recal(val,sum)
{
if(sum)
{
var total = document.getElementById("total").innerHTML;
total+=val;
document.getElementById("total").innerHTML=total;
}
}
</script>
when sum is come as true it doesnt not sum the value :S
looks to me like a type casting problem - change this line:
var total = document.getElementById("total").innerHTML;
to this:
var total = parseInt(document.getElementById("total").innerHTML, 10);
Also try looking at this example it should give You more overview what could've go wrong.

Categories