django save a value for unchecked checkbox - html

//mytemplate//
<tr>
<td><input type="text" name="language1" id="language1" /></td>
<td><input type="checkbox" name="read1" id="read1" value="yes" /></td>
<td><input type="checkbox" name="write1" id="write1" value="yes" /></td>
<td><input type="checkbox" name="speak1" id="speak1" value="yes" /></td>
</tr>
<tr>
<td><input type="text" name="language1" id="language1" /></td>
<td><input type="checkbox" name="read1" id="read1" value="yes" /></td>
<td><input type="checkbox" name="write1" id="write1" value="yes" /></td>
<td><input type="checkbox" name="speak1" id="speak1" value="yes" /></td>
</tr>
I wanted to save yes into my db if the checkbox is checked,
If the checkbox was unchecked i want to save no into db,..
How to do it?
EDIT
def edit_other_info(request):
if request.method == "POST":
user = request.POST.get('user_id')
language1 = request.POST.get('language1')
read1 = request.POST.get('read1')
write1 = request.POST.get('write1')
speak1 = request.POST.get('speak1')
language2 = request.POST.get('language2')
read2 = request.POST.get('read2')
write2 = request.POST.get('write2')
speak2 = request.POST.get('speak2')
p=language(user_id=user,language1=language1,read1=read1,write1=write1,
speak1=speak1,language2=language2,read2=read2,write2=write2,speak2=speak2)
p.save()
return HttpResponseRedirect('/accounts/Profile/')
else:
details1 = language.objects.filter(user_id=request.user.id)
return render_to_response('registration/language.html', {"details":details, "details1":details1}, context_instance=RequestContext(request))
Thanks in advance..

Test if the checkbox id is in the post parameters like so:
def myview(request):
if request.method == 'POST':
read1_val = "read1" in request.POST
MyModel(read1=read1_val).save()
If it is in there it was "yes". If it is not in there it was "no".
More here, here and here

If you provide a common name to all checkboxes you could retrive the value using
options = request.POST.getlist('name')
if nothing is checked, it returns a null list.

Related

HTML: How to save form values in local folder

I haven't played with HTML in over 15 years, and I've never done any other coding (I know... I'm a delight already, yeah?). This is only to be opened in browser on an offline PC for little kids. It's part of a homebrew html-based adventure game. I harvested code from several other sites to get this small bit - but idk how to make it work. I'd like kids to be able to answer the questions appropriately, and then save them as a text document (or really saved any way possible that I can view later) in the Downloads folder or any folder. There is no 'server' or any network - this is an old Windows 98 that kids play on.
edit:Something similar to this download function:https://stackoverflow.com/a/29376481/14739116
Here is what I have - please feel free to murder it:
<html><head>
<title>What did I do today?</title>
</head><body>
<form onsubmit="download(this['name'].value, this['form'].value)">
<h3>My name is...
<br>
<textarea rows=2 cols=25 name="text"></textarea>
<br><br>
Please select the appropriate response.
<table width="660" border>
<tr align="center">
<td width="500"></td>
<td width="50">1</td>
<td width="50">2</td>
<td width="50">3</td>
<td width="50">4</td>
</tr>
<tr align="center">
<td width="500">I brushed my teeth today.</td>
<td><input type="checkbox" id="1"></td>
<td><input type="checkbox" id="2"></td>
<td><input type="checkbox" id="3"></td>
<td><input type="checkbox" id="4"></td>
</tr>
<tr align="center">
<td width="500">I washed my hands today.</td>
<td><input type="checkbox" id="1"></td>
<td><input type="checkbox" id="2"></td>
<td><input type="checkbox" id="3"></td>
<td><input type="checkbox" id="4"></td>
</tr>
<tr align="center">
<td width="500">I ate all of my vegetables today.</td>
<td><input type="checkbox" id="1"></td>
<td><input type="checkbox" id="2"></td>
<td><input type="checkbox" id="3"></td>
<td><input type="checkbox" id="4"></td>
</tr>
<tr align="center">
<td width="500">I fed the fish this week.</td>
<td><input type="checkbox" id="1"></td>
<td><input type="checkbox" id="2"></td>
<td><input type="checkbox" id="3"></td>
<td><input type="checkbox" id="4"></td>
</tr>
</table>
<br>
<input type="submit" value="SAVE">
</body></html>
Your HTML is workable, though the main issue I see with it is the duplicate ids (which is invalid) -- what you would want instead is to set the value attribute.
The good thing is, you won't need a server or anything for downloading the responses as a file, as that can be done in JavaScript. You can try some of the solutions discussed here: JavaScript: Create and save file. (You just need to make sure the browser settings will download the file to your desired location by default).
You would also need to write some JS to extract the selected checkboxes & the entered name (which would be a suitable filename -- with some appropriate sanitization).
I assume on an old system like that, there's probably an old version of IE. If so, you'll be more limited with JS functionality. (If possible, try installing a later version or another browser to get things working).
Below is a snippet that works on Firefox/Chrome and IE10+ (also available in this fiddle).
The latter half of it is based on the answer that I previously linked here.
The JS will generate a CSV format of the answers and use the entered name as the filename.
You just need to set the browser settings to automatically download to your desired folder.
<html>
<head>
<title>What did I do today?</title>
</head>
<body>
<form onsubmit="saveResponses()">
<h3>My name is...</h3>
<textarea rows=2 cols=25 name="text"></textarea>
<br><br>
Please select the appropriate response.
<table width="660" border>
<tr align="center">
<td width="500"></td>
<td width="50">1</td>
<td width="50">2</td>
<td width="50">3</td>
<td width="50">4</td>
</tr>
<tr align="center">
<td width="500">I brushed my teeth today.</td>
<td><input type="checkbox" value="1"></td>
<td><input type="checkbox" value="2"></td>
<td><input type="checkbox" value="3"></td>
<td><input type="checkbox" value="4"></td>
</tr>
<tr align="center">
<td width="500">I washed my hands today.</td>
<td><input type="checkbox" value="1"></td>
<td><input type="checkbox" value="2"></td>
<td><input type="checkbox" value="3"></td>
<td><input type="checkbox" value="4"></td>
</tr>
<tr align="center">
<td width="500">I ate all of my vegetables today.</td>
<td><input type="checkbox" value="1"></td>
<td><input type="checkbox" value="2"></td>
<td><input type="checkbox" value="3"></td>
<td><input type="checkbox" value="4"></td>
</tr>
<tr align="center">
<td width="500">I fed the fish this week.</td>
<td><input type="checkbox" value="1"></td>
<td><input type="checkbox" value="2"></td>
<td><input type="checkbox" value="3"></td>
<td><input type="checkbox" value="4"></td>
</tr>
</table>
<br>
<input type="submit" value="SAVE">
</form>
<script type="text/javascript">
// StackOverflow question https://stackoverflow.com/questions/65084239
function saveResponses() {
// Windows-safe filename
var name = document.getElementsByTagName('textarea')[0].value.replace(/[<>:"/\|?*]/g, ' ') + '.txt';
var responses = [];
var questions = document.getElementsByTagName('tr');
for (var i = 1; i < questions.length; i++) {
var question = questions[i].getElementsByTagName('td')[0].innerHTML;
// Save responses in CSV format
responses.push('"' + question + '"');
var checkboxes = questions[i].querySelectorAll('input[type=checkbox]:checked');
for (var j = 0; j < checkboxes.length; j++) {
responses[responses.length - 1] += ',"' + checkboxes[j].value + '"';
}
}
responses = responses.join('\r\n');
// console.log(responses);
download(responses, name, 'text/plain');
}
// Borrowed from https://stackoverflow.com/a/30832210
function download(data, filename, type) {
var file = new Blob([data], {type: type});
if (window.navigator.msSaveOrOpenBlob) { // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
} else { // Others
var a = document.createElement('a');
var url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
</script>
</body>
</html>

The post method is not supported for this route

I'm new to laravel and I want to submit a form which have three attachments plus some arrays
but when I click submit button laravel says
"The POST method is not supported for this route. Supported methods: GET, HEAD."
I've searched a lot but couldn't find any errors
My View
<div>
<form action="insertquotation" method="post" enctype="multipart/form-data">
#csrf
<table border="1" id="formTable">
<thead>
<tr>
<td>procurment_request_number</td>
<td>quotationer</td>
<td>shop_name</td>
<td>shop_account_number</td>
<td>shop_permit</td>
<td>shop_quatation</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" name="procurment_request_number" id="procurment_request_number" value="{{$result->first()->request_number}}">
{{--<select name="procurment_request_number[]" id="procurment_request_number">--}}
{{--#foreach($result as $results)--}}
{{--<option value="{{$results->request_number}}">{{$results->request_number}}</option>--}}
{{--#endforeach--}}
{{--</select>--}}
</td>
<td><input type="text" id="quotationer1" name="quotationer1"></td>
<td><input type="text" name="shop_name1" id="shop1_name"></td>
<td><input type="text" name="shop_account_number1" id="shop1_account_number"></td>
<td><input type="text" name="shop_permit1" id="shop1_permit"></td>
<td><input type="file" accept="application/pdf" name="shop_quatation1" id="shop_quatation"></td>
</tr>
<tr>
<td></td>
<td><input type="text" id="quotationer" name="quotationer2"></td>
<td><input type="text" name="shop_name2" id="shop1_name"></td>
<td><input type="text" name="shop_account_number2" id="shop1_account_number"></td>
<td><input type="text" name="shop_permit2" id="shop1_permit"></td>
<td><input type="file" accept="application/pdf" name="shop_quatation2" id="shop_quatation"></td>
</tr>
<tr>
<td></td>
<td><input type="text" id="quotationer" name="quotationer3"></td>
<td><input type="text" name="shop_name3" id="shop1_name"></td>
<td><input type="text" name="shop_account_number3" id="shop1_account_number"></td>
<td><input type="text" name="shop_permit3" id="shop1_permit"></td>
<td><input type="file" accept="application/pdf" name="shop_quatation3" id="shop_quatation"></td>
</tr>
<tr>
<td>Item_ID</td>
<td>Quantity</td>
<td>Unit_ID</td>
<td>description</td>
<td>Shop1_price</td>
<td>Shop2_price</td>
<td>Shop3_price</td>
</tr>
#foreach($result as $results)
<tr>
<td><input type="text" name="itemid[]" id="itemid" value="{{$results->id}}"></td>
<td><input type="text" name="quantity[]" id="quantity" value="{{$results->quantity}}"></td>
<td><input type="text" name="units_id[]" id="units_id" value="{{$results->units_id}}"></td>
<td><input type="text" name="description[]" id="description" value="{{$results->description}}"></td>
<td><input type="text" name="shop1_price[]" id="shop1_price"></td>
<td><input type="text" name="shop2_price[]" id="shop2_price"></td>
<td><input type="text" name="shop3_price[]" id="shop3_price"></td>
</tr>
</tbody>
#endforeach
</table>
{{--<input value="addrow" type="button" id="addrow" onclick="javascript: addRow();">--}}
<input type="submit" value="submit">
</form>
</div>
My Controller
public function insertquotation(request $request)
{
if ($file = $request->file('shop1_quatation')) {
$name = $file->getClientOriginalName();
$file->move('procurement_files', $name);
$file2 = $request->file('shop2_quatation');
$name2 = $file2->getClientOriginalName();
$file2->move('procurement_files', $name2);
$file3 = $request->file('shop2_quatation');
$name3 = $file2->getClientOriginalName();
$file3->move('procurement_files', $name3);
$procurment_request_number = $request->input('procurment_request_number');
$quotationer1 = $request->input('quotationer1');
$quotationer2 = $request->input('quotationer2');
$quotationer3 = $request->input('quotationer3');
$shop_name1 = $request->input('shop_name1');
$shop_account_number1 = $request->input('shop_account_number1');
$shop_permit1 = $request->input('shop_permit1');
$shop_name2 = $request->input('shop_name2');
$shop_account_number2 = $request->input('shop_account_number2');
$shop_permit2 = $request->input('shop_permit2');
$shop_name3 = $request->input('shop_name3');
$shop_account_number3 = $request->input('shop_account_number3');
$shop_permit3 = $request->input('shop_permit3');
$numbers = $request->input('itemid');
for ($i = 0; $i < count($numbers); $i++) {
$itemid = $request->input('itemid');
$shop1_price = $request->input('shop1_price');
$shop2_price = $request->input('shop2_price');
$shop3_price = $request->input('shop3_price');
DB::insert("INSERT INTO procurment_quotations (`procurment_request_number`, `itemid`, `quotationer1`, `quotationer2`, `quotationer3`, `shop1_name`, `shop1_account_number`, `shop1_permit`, `shop1_quatation`, `shop1_price`, `shop2_name`, `shop2_account_number`, `shop2_permit`, `shop2_quatation`, `shop2_price`, `shop3_name`, `shop3_account_number`, `shop3_permit`, `shop3_quatation`, `shop3_price`, `created_at`, `updated_at`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,)", [$procurment_request_number, $itemid[$i],$quotationer1,$quotationer2,$quotationer3,$shop_name1,$shop_account_number1,$shop_permit1,$name,$shop1_price,$shop_name2,$shop_account_number2,$shop_permit2,$name2,$shop2_price,$shop_name3,$shop_account_number3,$shop_permit3,$name3,$shop3_price]);
}
}
}
and my route
Route::Post('insertquotation','Home#insertquotation');
The error is
"The POST method is not supported for this route. Supported methods: GET, HEAD."
Give the route a name in your routes/web.php file:
Route::post('insertquotation','Controllername#methodname')
->name('insertquotation');
And then change the form action in your blade file to point to that route:
<form action="{{ route('insertquotation') }}" method="post" enctype="multipart/form-data">
Couple of things which I need to point out on your code
It is always better to use route names while creating routes so that when you need to change the route URL to something else, you only need to change it inside routes file (not view files)
Route::post('insertquotation','Home#insertquotation')->name('insertquotation');
Now inside your view, you need to do this
<form method="POST" enctype="multipart/form-data" action="{{ route('insertquotation') }}"> ... </form>
In the future when you want (maybe not) to change the URL to /quotation/insert, all you have to do is to change it inside the routes file.
Now, inside your controller,
There is a function to check whether the file exists $request->hasFile('shop1_quatation') which more correct to use inside an if condition rather passes in the file.
Also, it is much better to use Laravel eloquent for inserting data into the database. Learn more here
I think from action="insertquotation", you should make it into action="{{url('insertquotation')}}"
And may I see the codes in your routes/web.php file? you should declare the route insertquotation there
For example:
Route::post('insertquotation', 'YourController#yourMethod');

How to update the existing rows add insert new rows in table in laravel for multiple array values

i have a table of data with array input values , as like below
<table>
<tr>
<td>Name</td>
<td>Email</td>
<td>Phone</td>
</tr>
<tr>
<td><input type="text" name="user_id[]" value="1"></td>
<td><input type="text" name="name[]"></td>
<td><input type="text" name="email[]"></td>
<td><input type="text" name="phone[]"></td>
</tr>
<tr>
<td><input type="text" name="user_id[]" value="2"></td>
<td><input type="text" name="name[]"></td>
<td><input type="text" name="email[]"></td>
<td><input type="text" name="phone[]"></td>
</tr>
<tr>
<td><input type="text" name="user_id[]" value=""></td>
<td><input type="text" name="name[]"></td>
<td><input type="text" name="email[]"></td>
<td><input type="text" name="phone[]"></td>
</tr>
<tr>
<td><input type="text" name="user_id[]" value=""></td>
<td><input type="text" name="name[]"></td>
<td><input type="text" name="email[]"></td>
<td><input type="text" name="phone[]"></td>
</tr>
</table>
And i m trying to update the first two rows with user_id value and trying to insert last two rows as new rows.
I m using the below insert query in laravel controller :
if(!empty(Input::get('user_id')))
{
for($i = 0; $i < count(Input::get('name')); $i++)
{
$id = Input::get('user_id')[$i];
$data = User::find($id);
$data->name = Input::get('name')[$i];
$data->email = Input::get('email')[$i];
$data->phone = Input::get('phone')[$i];
$data->save();
}
}
if(empty(Input::get('user_id')))
{
for($i = 0; $i < count(Input::get('name')); $i++)
{
$new = new User();
$new->name = Input::get('name')[$i];
$new->email = Input::get('email')[$i];
$new->phone = Input::get('phone')[$i];
$new->save();
}
}
But its not working properly . can somebody suggest me any solution for this ?
You should be using a foreach loop to, well, loop through the fields and do something if it is empty or not.
Maybe, something like this would help you.
$fields = Input::get('user_id');
foreach($fields as $field)
{
if(! empty($field))
{
// field is not empty
// update here
}
else
{
// field is empty
// do something here
}
}

Append text from HTML to XML

G'day,
As a part of my assignment, I have to store the details entered by a user in a registration form into an XML document.
I have managed to do that BUT the problem is when a new user registers, the old details are over-written with the details of the new user. So, there is ONLY 1 user details in the XML document in the end.
I was wondering if there's any way to save the details of new user while "preserving" the old details.
Any help is appreciated :)
HTML Code -
<form id="rego" name="rego" method="get" action="register2.php">
<table width="719" border="0">
<tr>
<td><div align="right">Email Address</div></td>
<td><label for="email"></label>
<input type="text" name="email" id="email" maxlength="50" /></td>
</tr>
<td width="376"><div align="right">First Name</div></td>
<td width="333"><label for="firstName"></label>
<input type="text" name="firstName" id="firstName" maxlength="15" /></td>
<td><div id="underInput"></div></td>
</tr>
<tr>
<td><div align="right">Last Name</div></td>
<td><label for="lastName"></label>
<input type="text" name="lastName" id="lastName" maxlength="20" /></td>
</tr>
<tr>
<td><div align="right">Phone Number</div></td>
<td><label for="phoneNumber"></label>
<input type="text" name="phoneNumber" id="phoneNumber" maxlength="10" /></td>
</tr>
<tr>
<tr>
<td><div align="right">Password</div></td>
<td><label for="password"></label>
<input type="password" name="password" id="password" maxlength="30" /></td>
</tr>
<tr>
<td><div align="right">Re-type password</div></td>
<td><label for="confirmPassword"></label>
<input type="password" name="confirmPassword" id="confirmPassword" maxlength="30" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="Submit" name="Submit" id="Submit" value="Submit"/><!--onclick="saveForm();"--></td>
</tr>
PHP Code (I'm using DOM) -
<?php
$CustomerEmail = $_GET["email"];
$CustomerFName = $_GET["firstName"];
$CustomerLName = $_GET["lastName"];
$CustomerPhoneNumber = $_GET["phoneNumber"];
$CustomerPassword = $_GET["password"];
$CustomerConfirmPassword = $_GET["confirmPassword"];
$doc = new DomDocument('1.0');
$root = $doc->createElement('customers');
$root = $doc->appendChild($root);
$customer = $doc->createElement('customer');
$customer = $root->appendChild($customer);
$email = $doc->createElement('email');
$email = $customer->appendChild($email);
$valueEmail = $doc->createTextNode($CustomerEmail);
$valueEmail = $email->appendChild($valueEmail);
$fName = $doc->createElement('firstname');
$fName = $customer->appendChild($fName);
$valueFName = $doc->createTextNode($CustomerFName);
$valueFName = $fName->appendChild($valueFName);
$lName = $doc->createElement('lastname');
$lName = $customer->appendChild($lName);
$valueLName = $doc->createTextNode($CustomerLName);
$valueLName = $lName->appendChild($valueLName);
$phone = $doc->createElement('phone');
$phone = $customer->appendChild($phone);
$valuePhone = $doc->createTextNode($CustomerPhoneNumber);
$valuePhone = $phone->appendChild($valuePhone);
$password = $doc->createElement('password');
$password = $customer->appendChild($password);
$valuePassword = $doc->createTextNode($CustomerPassword);
$valuePassword = $password->appendChild($valuePassword);
$confirmPassword = $doc->createElement('confirmpassword');
$confirmPassword = $customer->appendChild($confirmPassword);
$valueConfirmPassword = $doc->createTextNode($CustomerConfirmPassword);
$valueConfirmPassword = $confirmPassword->appendChild($valueConfirmPassword);
$doc->save('customer2.xml');
?>
I apologise for any inconvience.
You need to load your existing file, before creating a new one each time and override the previous.
.....
$doc = new DomDocument('1.0');
if($xml = file_get_contents( 'customer2.xml')){
$doc->loadXML( $xml, LIBXML_NOBLANKS );
$root=$doc->getElementsByTagName('customers')->item(0);
}else{
$root = $doc->createElement('customers');
$root = $doc->appendChild($root);
}
$customer = $doc->createElement('customer');
$customer = $root->appendChild($customer);
.....
$doc->save('customer2.xml');

HTML Validation Issue

Line 101, Column 86: document type does not allow element "input" here; missing one of "th", "td" start-tag
Here is the code:
<input type="submit" name = "b1" value = "Calculate" onclick = "calculate()"/>
<form name="frm1" action="">
<fieldset>
<table>
<tr>
<td>Weight:</td>
<td><input type="text" name="number1"/></td>
<td>Distance:</td>
<td><input type="text" name="number2"/></td>
<td>Answer =</td>
<td><input type="text" name="number3"/></td>
<input type="submit" name = "b1" value = "Calculate" onclick = "calculate()"/>
</tr>
</table>
</fieldset>
</form>
Here is the function:
<script type="">
function calculate()
{
A = document.frm1.number1.value;
B = document.frm1.number2.value;
C = (A*B*1.036);
document.frm1.number3.value = C
}
</script>
It sounds like you are trying to put an input element as a child element of a table row.
That isn't allowed and does not make sense.
Either place it outside the table or inside a table cell.
It should look like this
<form name="frm1" action="">
<fieldset>
<table>
<tr>
<td>Weight:</td>
<td><input type="text" name="number1"/></td>
<td>Distance:</td>
<td><input type="text" name="number2"/></td>
<td>Answer =</td>
<td><input type="text" name="number3"/></td>
<td><input type="submit" name = "b1" value = "Calculate" onclick = "calculate()"/></td>
</tr>
</table>
</fieldset>
</form>