In my MySQL, I have created a table called categories with a trending and a status column, both are set to have a default value of 0:
![the structure of the table categories in xampp mysql][1]
But when I insert a new record, the columns are set to 1, no matter if I give a value or not.
After the insert:
![the data in the table categories where none of trending and status field is selected even though showing 1 values for both trending and status][2]
Can anyone help me out to solve this error?
-code to push the data in the database is given below:
<div class="card card-outline card-info">
<div class="card-header">
<h4>
New Category
<button type="button" class="btn btn-primary btn-md
float-right" data-toggle="modal" data-target="#modal-
default">
ADD
</button>
</h4>
</div>
<div class="card-body"></div>
</div>
<div class="modal fade" id="modal-default">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Add New Category</h4>
<button type="button" class="close" data-
dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="code.php" method="POST" id="cat_form">
<div class="modal-body">
<div class="form-group">
<label for="">Category Name</label>
<input type="text" name="name"class="form-
control" required>
</div>
<div class="form-group">
<label for="">Description</label>
<textarea name="description" class="form-
control" required rows="5">
</textarea>
</div>
<div class="form-group">
<label for="">Trending</label>
<input type="checkbox" name="trending">Trending
</div>
<div class="form-group">
<label for="">Status</label>
<input type="checkbox"name="status">Status
</div>
</div>
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-default"
data-dismiss="modal">Close</button>
<button type="submit" name="category_save"
class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
-code for submit button:
if (isset($_POST['category_save']))
{
$name = $_POST['name'];
$description = $_POST['description'];
$trending = $_POST['trending'] = true ? '1':'0';
$status = $_POST['status'] = true ? '1':'0';
$category_query = "INSERT INTO categories
(name,description,trending,status) VALUES
('$name','$description','$trending','$status')";
$cate_query_run = mysqli_query($con, $category_query);
if ($cate_query_run)
{
echo "<script>
alert(' - Category Inserted Succesfully!');
window.location.href='category.php';
</script>";
}
else
{
echo "<script>
alert(' - Category Insertion Failed!');
window.location.href='category.php';
</script>";
}
}
table structure before data insertion
[1]: https://i.stack.imgur.com/SEX7H.png
table after data insertion
[2]: https://i.stack.imgur.com/dPfmo.png
thanks in advance
If you just want to get "1" when checked and "0" when unchecked.
The HTML and PHP code below should be able to help you
<form name="myForm" id="myForm" method="post" action="my_page.php">
<input type="checkbox" name="myCase" id="myCase" value="1"><label for="myCase">My Case</label>
</form>
And "my_page.php" code
$myCase = (isset($_POST["myCase"]))?$_POST["myCase"]:"0";
echo "<br />myCase : ".$myCase;
Related
I am new to the Django framework. I have developed a page with a table and Add button. When the Add button clicks, it will open the form inside a modal. Here the problem is when I first tried, the data in the form filled to the table as expected. But after, when I did the same thing, it gave 200 for POST, and nothing filled in the table. And no errors showing.
This is my sample code.
<!--modal for Create record-->
<div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<form method="POST" class="modal-dialog modal-md" action=".">{% csrf_token %}
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel"><strong>Add Company</strong></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="form-group row">
<label for="companyName" class="col-12 col-md-4"><strong>Company Name</strong></label>
<div class="col-12 col-md-10">
<input type="text" class="form-control" id="companyName" name="companyName" placeholder="Company Name">
</div>
</div>
<div class="form-group row">
<label for="address" class="col-12 col-md-4"><strong>Address</strong></label>
<div class="col-12 col-md-10">
<input type="text" class="form-control" id="address" name="address" placeholder="Address">
</div>
</div>
<div class="modal-footer">
<button type="reset" class="btn btn-secondary">Reset</button>
<button type="submit" class="btn btn-primary">Add</button>
<button type="close" class="btn btn-light" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
And here is the code in views.py
def addcompany(request):
if request.method == 'POST':
if request.POST.get('companyName') and request.POST.get('address'):
saverecord = company()
saverecord.CompanyName = request.POST.get('company')
saverecord.Address = request.POST.get('address')
saverecord.save()
return render(request, 'newapp/companypage.html')
else:
return render(request, 'newapp/companypage.html')
I would be very grateful if anyone can help me to solve this problem. Thank you.
I think the error in the company name you should write it like this:
saverecord.CompanyName = request.POST.get('companyName')
You can also read more about how to build forms from models with Django
I'm pretty new to HTML5 and have inherited someone else's code, which turns out not to be working.
I have a site with 8 different categories and 'add to email list' buttons, one for each category.
We use formspree to create the form code and then connect with our marketing systems. Instead of integrating 8 different forms, the previous coder added one from as a function*, then referred to that function on each of the 8 buttons.
Edit:*Its not actually a function, what is below is the only code involved.
To determine which of the categories the form came from, its meant to pass over a tag, but it's that tag that is not working and I would appreciate help on.
<!-- Waitlist modal -->
<div class="modal fade" id="waitlist-modal" tabindex="-1" role="dialog" aria-labelledby="modal-label" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title heavy-text" id="modal-label">Request Access</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="contact-form" action="https://formspree.io/email#gmail.com" method="POST" >
<div class="form-group">
<label for="full-name">Full name</label>
<input type="text" class="form-control" id="full-name" name="name" placeholder="Enter name">
</div>
<div class="form-group">
<label for="email-address">Email address</label>
<input type="email" class="form-control" id="email-address" name="email" placeholder="Enter email">
</div>
<div class="form-group">
<input type="hidden" class="form-control" id="incubator" name="incubator">
</div>
<div class="form-group">
<input type="hidden" class="form-control" id="ip-address" name="ip-address">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" id="submit-form" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</div>
The below code is for each of the 8 category's join buttons.
<a href="#" data-toggle="modal" data-target="#waitlist-modal" data-id="entrepreneur" class="yellow-link">
<p class="yellow-text call-to-action" style="font-size: 0.7rem;">JOIN THE WAITLIST ➞</p>
</a>
As far as I understand, its meant to collect the full name and email address visibly, then the IP and the "incubator" [category] silently. All but the "incubator" tag works fine, which never passes over anything. In the links, the previous coder uses data-id="entrepreneur" [a category] for example, which doesn't seem right to me.
I'm trying to submit some simple log in data to a server. All of the requests and functions are working great for "testuser." I want people to be able to submit their username and password for consideration to the server (it all gets encrypted, and I can't see it.)
I can see where the registration request submits generic data "testuser" for username and pass. But I have no idea how to get it to accept user login information instead.
<!--This is where I'm attempting a login form-->
<form action="/action_page.php">
<div class="imgcontainer">
<img src="img_avatar2.png" alt="Avatar" class="avatar">
</div>
<div class="container">
<label><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="uname" required>
<label><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<button type="submit">Login</button>
<input type="checkbox" checked="checked"> Remember me
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" class="cancelbtn">Cancel</button>
<span class="psw">Forgot password?</span>
</div>
</form>
<!-- This is rock-solid code from the gamesparks API. Works flawlessly. -->
<button onClick='gamesparks.registrationRequest("testuser", "testuser", "testuser", registerResponse)'>Register</button>
<button onClick='gamesparks.authenticationRequest("testuser", "testuser", loginResponse)'>Login</button>
<button onClick='gamesparks.accountDetailsRequest(accountDetailsResponse)'>Account Details</button>
<button onClick='customEvent()'>Custom Event</button>
<button onClick='customEvent2()'>Custom Event 2</button>
<button onClick='testRT()'>Test RT</button>
<i>Special thanks to the awesome team at GameSparks!</i>
<div id="messages"></div>
<br/>
<br/>
All of my buttons work, spitting out data from my backend regarding "testuser." I know it works because I can alter my data and verify.
You'll notice "testuser" occupies three spaces for one of the onClicks. One is username, the other is password, and the third is displayname (in that order.)
Displayname can equal Username...if anyone wants extra credit.
But mostly, I want my modal to input the data and submit it for my buttons.
Again, I solved my own question! What I did was put my gamesparks function inside of another function. I learned this was called "nesting." I also defined variables as part of the onClick, which were then used in the function. The solution I used is below. Forgive my n00bish comments. But they help me learn.
<div class="container">
<h2>Click To Login</h2> <!-- This is a label above the button -->
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Martial Parks</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><img src="http://martialparks.com/wp-content/uploads/2017/10/cropped-mp_logo01-2.png" class="center-block img-circle logo-margin-negative"></h4>
</div>
<div class="modal-body">
<form>
<div class="row">
<div class="form-group col-sm-5 col-sm-offset-4">
<label class="sr-only" for="exampleInputAmount">Username</label>
<div class="input-group">
<div class="input-group-addon"><span class="glyphicon glyphicon-user"></span></div>
<input type="text" class="form-control" id="username" placeholder="Username">
</div>
</div>
<div class="form-group col-sm-5 col-sm-offset-4">
<label class="sr-only" for="exampleInputAmount">Password</label>
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-key"></i></div>
<input type="text" class="form-control" id="password" placeholder="Password">
</div>
</div>
<button type="button" class="btn btn-danger btn-block" data-dismiss="modal" onClick="login()">Login</button>
<!-- There is a line of code that closes the modal once the button is clicked: data-dismiss="modal" I've removed it from the register button. -->
<!-- Also, I can call up multiple functions using this format: onclick="doSomething();doSomethingElse();" -->
<button type="button" class="btn btn-danger btn-block" onClick="register()">Register</button>
<script>
function login() {
var usernameinput = document.getElementById('username').value;
var passwordinput = document.getElementById('password').value;
console.log(usernameinput);
console.log(passwordinput);
gamesparks.authenticationRequest( usernameinput, passwordinput, loginResponse);
}
function register() {
var usernameinput = document.getElementById('username').value;
var passwordinput = document.getElementById('password').value;
console.log(usernameinput);
console.log(passwordinput);
gamesparks.registrationRequest( usernameinput, usernameinput, passwordinput, registerResponse)
}
</script>
</div>
</form>
</div>
<div class="modal-footer">
<div class="row">
<div class="col-sm-5 col-sm-offset-4">
<div id="messages"></div>
<br/>
<br/>
You need to manually grab each of your fields values. The most direct way is via document.getElementById and value:
document.getElementById( "username" ).value
In your example:
<button onClick='gamesparks.authenticationRequest( document.getElementById( "username" ).value, document.getElementById( "password" ).value, loginResponse )'>Register</button>
I would, however, suggest calling a function instead of inlining it:
<button onclick="login()">Login</button>
⋮
⋮ // Before </body>
⋮
<script>
const fetchValue = id => document.getElementById( id ).value;
function login() {
const username = fetchValue( "username" );
const password = fetchValue( "password" );
gamesparks.authenticationRequest( username, password, loginResponse );
}
</script>
Also1: You should never need to place an api secret in client-facing HTML/JavaScript.
Also2: Regarding your these lines:
<var username = username>
<var password = password>
These are invalid. <var> provides styling and nothing else. It looks like you are attempting to place JavaScript there? To do so, you need to either use <script> tags or event handlers, such as onclick.
I need to show the values given in modal at outside the modal using AngularJS. Plunker attached here which clearly explains. Can anyone help me out........I have already checked Angular, Ui-Bootstrap site, but couldn't find the solution to the problem
HTML
<div ng-controller="ModalDemoCtrl as $ctrl" class="modal-demo">
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<button type="button" class="close" aria-hidden="true" ng-click="$ctrl.cancel()">×</button><a>
<h3 class="modal-title" id="modal-title" style="color:red">Please Provide the Details Here</h3>
</a>
</div>
<form name="modalForm" novalidate>
<div class="modal-body">
<div class="form-group" ng-class="{ 'has-error': modalForm.name.$invalid }"> <!--modalForm.name.$touched &&-->
<a>*Name: </a><input name="name" ng-minlength="3"
ng-maxlength="8" ng-model="name" type="text" placeholder="Name" class="form-control" size="10" style="width: 50%" required />
</div>
<div class="form-group" ng-controller="ButtonsCtrl">
<a>*Gender: </a><pre>{{radioModel || 'null'}}</pre>
<div class="btn-group">
<label class="btn btn-success" ng-model="radioModel" uib-btn-radio="'Male'" uncheckable>Male</label>
<label class="btn btn-success" ng-model="radioModel" uib-btn-radio="'Female'" uncheckable>Female</label>
</div>
</div>
<div class="input-group" ng-controller="DatepickerPopupDemoCtrl">
<a>*Birth Date: Selected date is: <em>{{dt | date:'fullDate' }}</em></a><select class="form-control" ng-model="format" ng-options="f for f in formats" style="width: 50%"><option></option></select><input type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="dt" is-open="popup1.opened" datepicker-options="dateOptions" close-text="Close" ng-required="true" alt-input-formats="altInputFormats" style="width: 50%" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open1()"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</div>
<p ng-show="modalForm.modalPlace.$error.required">Select service</p>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-warning" type="button" ng-click="$ctrl.ok()" ng-disabled="modalForm.$invalid">OK</button>
<button class="btn btn-warning" type="button" ng-click="$ctrl.cancel()">Cancel</button>
</div>
</form>
</script>
<button type="button" class="btn btn-default" ng-click="$ctrl.open()">Open me!</button>
<div ng-show="$ctrl.selected">Name from the modal: {{ $ctrl.name }}</div>
<div ng-show="$ctrl.selected">Gender from the modal: {{ radioModel || 'null'}}</div>
<div ng-show="$ctrl.selected">DOB from the modal: {{ dt | date:'fullDate' }}</div>
What you are looking for is the modalInstance.result property, it a promise which is resolved with a value when you close your modal (or rejects when you dismiss it).
The docs already provide you with a simple example
modalInstance.result.then(function (selectedItem) {
$ctrl.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
Inside your modal controller you can close/dismiss the modal using these methods
var selectedItem = ...
$uibModalInstance.close(selectedItem)
// or
$uibModalInstance.dismiss(selectedItem)
With selectedItem being passes to the controller that launched the modal.
Because you code is incomplete I can't really help you much more then that, the docs hold all you need to know really.
I have got the following HTML code. I have no idea why the POST is sent with no parameters (I checked the parameters are not being sent with the firefox debugger):
<div class="modal fade"id="myModal"tabindex="-1"role="dialog"aria-labelledby="myModalLabel"aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button"class="close"data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title"id="myModalLabel">Add opinion</h4>
</div>
<form role="form" action="api/sendEntry" method="post">
<div class="modal-body">
<div class="form-group">
<label for="user_message">The following: </label>
<input type="text" class="form-control" id="user_message" placeholder="In next...">
<label for="user_date">Until: </label>
<input type="date" class="form-control" id="user_date">
</div>
</div>
<div class="modal-footer">
<button type="submit"class="btn btn-default">Submit</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close </button>
</div>
</form>
</div>
</div>
</div>
I suspect the nested input fields may have something to do with it, but I need to have them nested to have proper formatting.
You need to assign a name to your input fields. For example:
<input type="text" class="form-control"
id="user_message" name="user_message" placeholder="In next..." />
Form data is posted as name/value pairs. If you don't provide name attributes, nothing will be posted.
As a side note, I would also recommend you to correctly close your input tags.
<div class="modal fade"id="myModal"tabindex="-1"role="dialog"aria-labelledby="myModalLabel"aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button"class="close"data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title"id="myModalLabel">Add opinion</h4>
</div>
<form role="form" action="api/sendEntry" method="post">
<div class="modal-body">
<div class="form-group">
<label for="user_message">The following: </label>
<input type="text" class="form-control" id="user_message" placeholder="In next..." name="user_message">
<label for="user_date">Until: </label>
<input type="date" class="form-control" id="user_date" name="user_date">
</div>
</div>
<div class="modal-footer">
<button type="submit"class="btn btn-default">Submit</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close </button>
</div>
</form>
</div>
</div>
Receive your data with $_POST['user_date'] and $_POST['user_message']