Laravel add row to table without refreshing page - html

I'm trying to add row to the table without refreshing page is it possible without using js? Basically I have a table and input in the modal my problem is that I need to refresh the page whenever I'm adding data to table which closes modal and data from the table has to be saved only when pressing save button in the modal. Maybe it is possible to do that using Laravel Controllers?
<table class="table" style="margin-top: 16px">
<tbody>
<tr>
<td class="table-text">
<div style="float: left">TASK NAME</div>
</td>
<td style="width: 10%">
<div style="float: end">DELETE</div>
</td>
</tr>
</tbody>

This is not possible without javascript.
Assuming you are using jquery, I would do something like this:
$('form').on('submit', function(e) {
e.preventDefault(); // Disable the sending of the form
var one = $('#one').val();
var two = $('#two').val();
$('#one').val('');
$('#two').val('');
$.ajax({
url: "/api/products/add",
method: 'post',
data: {
product: one,
price: two
},
success: function(){
appendToTable(one, two);
}
});
appendToTable(one, two); // The Success function in this ajax would never reach, because the url is not set. Thats because the function is called here.
});
function appendToTable(one, two) {
$('table').append(`
<tr>
<td>${one}</td>
<td>${two}</td>
</tr>
`);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="/api/products/add" method="post">
<input id="one" name="product" type="text" placeholder="product" required>
<input id="two" name="price" type="number" placeholder="price" required>
<button>Add</button>
</form>
<table>
<tr>
<th>Product</th>
<th>Price</th>
</tr>
<tr>
<td>PC</td>
<td>500</td>
</tr>
</table>
You disable the forms default behavior (submitting) and do the nessesarry work with js (or jquery). If the user hover disables js, the form submits as normal and sends the data.

Related

Angular, Get and show submit time after click submit button

Is it possible to get the date and time when the user click the submit button in angular? For example i have a form with name and email input. When user done filling the input and click submit, the date and time will be showed to the table along with name and email. Here some snippets:
//app.service.ts
insertData(data: App) {
this.dataList.push({
name: data.name,
email: data.email,
});
}
//app.component.ts
onSubmit(form: NgForm) {
this.AppService.insertData(this.AppService.selectedData);
}
<!--app.component.html-->
<form #dataForm="ngForm" (ngSubmit)="onSubmit(dataForm)">
<label>Name</label>
<input class="form-control" name="nama" #nama="ngModel" [(ngModel)]="AppService.selectedData.name"><br>
<label>Email</label>
<input class="form-control" name="email" #email="ngModel" [(ngModel)]="AppService.selectedData.email"><br>
<button type="submit">Submit</button>
</form>
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Time submitted</th>
</tr>
<tr *ngFor="let data of data">
<td>data.name</td>
<td>data.email</td>
<td>Time submitted</td>
</tr>
</table>
Just do :
this.dataList.push({
nama: data.nama,
email: data.email,
time : new Date(),
});
This will store the current time when you are inserting data and then use simply on the template side.
{{data.time | date: 'dd/MM/yyyy'}}

Store persistent HTML form data in Google Script

I wrote a Google Script web app to back up selected Gmail labels to a user-defined Google Drive location. Forml.html presents the data in a tabular format. On each row lives index (which is the checkbox), label and drive target.
We would like the data in each row to persist from one page load to the next so that a selected row stays selected. After I figure out how to make the data persist, then I can build a trigger which will make the app run on a schedule and back up the selected labels. Any thoughts on how best to achieve the effect of storing HTML form data over time? I'm thinking of using the properties service, but I don't know how I can change the initial doGet to choose between a blank form and a form with preexisting data.
Form.html
<div id="formDiv">
<form id="myForm" target="_self">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">
<h3 span style="font-family:arial,helvetica,sans-serif;">Select</span></th>
<th scope="col">
<h3 span style="font-family:arial,helvetica,sans-serif;">Label</span></th>
<th scope="col">
<h3 span style="font-family:arial,helvetica,sans-serif;">Drive target</span></th>
</tr>
</thead>
<tbody>
<? var label = GmailApp.getUserLabels();
for (i = 0; i < label.length; i++) { ?>
<tr>
<td style="text-align: center;">
<span style="font-family:arial,helvetica,sans-serif;"><input id="index" type="checkbox" name="index" value="<?= [i]; ?>" /></span></td>
<td>
<span style="font-family:arial,helvetica,sans-serif;"><input id="label" type="text" style="border:none" size="60" name="label" value="<?= label[i].getName(); ?>" readonly /></span></td>
<td>
<span style="font-family:arial,helvetica,sans-serif;">Gmail Backup/<input id="target" type="text" size="60" maxlength="128" name="target" value="<?= label[i].getName(); ?>" /></span></td>
</tr>
<? } ?>
</tbody>
</table>
<p><input type="submit" value="Test" onclick="google.script.run.gmailBackup(this.parentNode.parentNode)" />
</p>
</form>
</div>
Code.gs
function doGet() {
var template = HtmlService.createTemplateFromFile("Form")
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.NATIVE);
return template;
}
function gmailBackup(template) {
// Set HTML form variables
var indices = template.index;
var labels = template.label;
var targets = template.target;
for (i = 0; i < indices.length; i++) {
var index = indices[i].toString();
var label = labels[indices[i]].toString();
var target = "Gmail Backup/" + targets[indices[i]].toString();
...
}
I ended up using the PropertiesService to store HTML form properties on a per-user basis. This looks like it will work quite well (especially once I find out exactly where the data is stored per Where does Google Script PropertiesService store data?).
From Form.html
<input onclick="google.script.run.setProperties(this.parentNode.parentNode);" type="submit" value="Save" >
From Code.gs
function setProperties(form) {
var filledForm = PropertiesService.getUserProperties().setProperty("FORM", form);
}

HTML disable submit button if drop down option not selected

I'm having some problems thinking of a solution to this issue. Basically I would like to disable my submit/save button if the user has not selected a value from a dropdown list that I have. When the page loads, it defaults to the "-Select-" option, but I'm getting server errors if the user forgets to actually select an option. The only way to do so that I can think of is what I mentioned before, disable the submit/save if they haven't selected a value and the "-Select-" option is still selected. Any suggestions?
<tr>
<td class="fieldName_td">
#Html.Label("Copy Internal Form Group Actions:")<span class="requiredField">*</span>
</td>
<td class="fieldData_td">
<div id="form_shower">
<select id="myselect" name="ddlInternalAssociations">
<option value="4" selected="selected">-Select-</option>
<option value="1">Remove the association</option>
<option value="2">Copy and create new association</option>
<option value="3">Maintain the old association</option>
</select>
</div>
<div class="show-hide" name="form_name1" id="form_name1" style="display:none">
</div>
<div class="show-hide" name="form_name3" id="form_name3" style="display:none">
</div>
</td>
</tr>
<tr>
<td class="fieldName_td">
<div class="show-hide" name="form_name2" id="form_name2" style="display:none">
#Html.Label("Name for newly created internal form group:")
</div>
</td>
<td class="fieldData_td">
<div class="show-hide" name="form_name4" id="form_name4" style="display:none">
<input id="newInternalFormName" name="newInternalFormName" type="text" value="#Model.DetailObject.SuggestedInternalFormName" size="25" />
</div>
</td>
</tr>
}
<tr>
<td class="fieldName_td"></td>
<td class="fieldDataCenter_td">
#Html.HiddenFor(p => p.returnUrl)
#Html.HiddenFor(p => p.DetailObject.FormGroupId)
#Html.NWFormSubmitButtonFor(model => model, "btn_SaveButton", "Save", "btn_SaveButton", "SaveCollection", "disableOnSubmit")
</td>
</tr>
You can use jQuery to:
On page load test value and enable/disable the submit button
$(document).ready(function () {
var selectedVal = $('#myselect').val();
if(selectedVal == 4) {
//disable submit button
} else {
//enable submit button
}
});'
Track the changed of the select as
$('#myselect').change(function() {
//Check the change and use above logic to disable/enable submit button
});
Of course you can group this code in common method.

Loading jQuery in Div

I am trying to call a jQuery action within a div (subpage) that was generated by JavaScript. I pretty new at this so bear with me.
In the index.html I am populating a div, via a JavaScript load command.
<ul>
<li>Add new Group</li>
<li>List Groups</li>
</ul>
<div id="content"></div>
Then on the page that is generated (which isn't a full HTML page, just a form).
<?php
include "../incl/db_class_inc.php";
?>
<script>
$('input').live('click', function(){
$("input:.datepicker").datepicker();
});
</script>
<form action="./data/save_group.php" method="post" enctype="application/x-www-form-urlencoded">
<hr>
<table>
<tr>
<td>Group number:</td>
<td><input type="number" name="name" required /></td>
</tr>
<tr>
<td>Start date:</td>
<td><input type="date" name="start_date" class="datepicker" required /></td>
</tr>
<tr>
<td>End date:</td>
<td><input type="date" name="end_date" class="datepicker" /></td>
</tr>
I am trying to run a jQuery script, and it will not run for anything. I know it has something to do with the .live() function right?
How do I get this jQuery function to run in the new DIV that is created?
Your selector is wrong:
$("input:.datepicker").datepicker();
You need to delete the :
$("input.datepicker").datepicker();
Note that live is deprecated, if you're using jQuery version 1.7+ use the on function instead
$('body').on('click', 'input',function(){
$("input.datepicker").datepicker();
});
Replace body with the closest static element that holds the dynamic inputs (div\form)
Move the inline scripts to the js file (separate of concerns). The HTML should only take care of the presentation of the page, not on the scripts at all.

Auto submitting to cfm

I've created a form that posts to a cfm file. When running a script onLoad that fills in the form values and tries to submit...The site takes me back to the login screen.
<script>
function f()
{
document.getElementById("email").value = "asdf#asdf.com";
document.getElementById("password").value = "asdf";
document.getElementById("form1").submit();
}
</script>
Please help!
update:
Here is the code....When the values for email and password are filled ini manually and the enter button is pressed it will direct me to the home page. However, when writing a script that submits the form onload, it redirects me to the login page.
<form action="https://www.asdf.com/dev/mem/login/login.cfm" method="post" id="caring" name="login" style="margin: 0px; padding: 0px;">
<input name="page" type="hidden" id="page" value="https://www.asdf.com/dev/mem/home/home.cfm">
<table>
<tr>
<td rowspan="2" class="title"></td>
<td class="label"><label for="email"></label></td>
<td class="element"><input name="email" id="email" value ="asdf#asdf.com" /></td>
<script>
//<td rowspan="2" class="action"><input type="submit" name="login" value="submit" alt="go" title="go" /></td>
</script>
</tr>
<tr>
<td class="label"><label for="username"></label></td>
<td class="element"><input name="password" value = "asdf" id="password" /></td>
</tr>
<td rowspan="2" class="action"><input type="submit" name="login" value="enter" alt="go" title="go" /></td>
<tr>
<td></td>
<td colspan="2"> </td>
<td> </td>
</tr>
</table>
</form>
It's hard to tell without the HTML of the form itself, but my guess would be that the action="" attribute of your form is blank. If that attribute is blank, the browser will post the form back to the same page.
Since you're using coldfusion, check to see if there is code generating your action="" value, and look for bugs in it if so. It may help to view the rendered HTML source of the page.
Make sure you aren't using a <cflocation> (or some other redirect) on your destination cfm page that could be redirecting the user back to the form page.
(It would help if you would post your full code)
Make sure to set the action. Here's the code:
<script type="text/javascript">
function f() {
document.getElementById("email").value="asdf#asdf.com";
document.getElementById("password").value="asdf";
document.getElementById('caring').action=document.getElementById("page").value;
document.getElementById('caring').submit();
}
</script>
And of course add the onload attribute to your body tag:
<body onload="f();">
There's a couple of other things to fix in your code. Check it with Total Validator. It's a great tool that will make you a better coder.