Form - button group with same "name" value - php

I tried to make form with some inputs that insert data in my database, the problem is - one of inputs (button group with name "priority" can't be find by PHP interpreter. Where I should put that name? I think it was bad... Here is my code:
<form action="" method="post">
<div class="form-group">
<strong><label for="towho">Do kogo:</label></strong>
<select class="form-control" name="towho">
<option> - - - </option>
<!-- Skrypt pobierania danych (username) do Selecta -->
<?php
get_users();
?>
</select>
</div>
<hr></hr>
<div class="form-group">
<label for="usr">Temat:</label>
<input type="text" class="form-control" name="topic">
</div>
<hr></hr>
<div class="form-group" style="padding-top:10px">
<strong><label for="comment">Opis zadania:</label></strong>
<textarea class="form-control" rows="5" name="comment"></textarea>
</div>
<hr></hr>
<center>
<div class="form-group">
<strong><p>Priorytet:</p></strong>
<div class="btn-group btn-group-justified" role="group" aria-label="Justified button group">
<div class="btn-group" role="group">
<input type="button" class="btn btn-danger" value="WYSOKI" name="priority">
</div>
<div class="btn-group" role="group">
<input type="button" class="btn btn-warning" value="ŚREDNI" name="priority">
</div>
<div class="btn-group" role="group">
<input type="button" class="btn btn-success" value="NISKI" name="priority">
</div>
</div>
</div>
</center>
<hr></hr>
<div class="form-group">
<button name="submit" type="worksubmit" class="btn btn-info btn-block">Zaakceptuj</button>
</div>
</form>
And here is my PHP code:
if(isset($_POST['submit'])) {
include 'db.php';
$sf = "INSERT INTO zadania (id, user, tablename, dodano, dokogo, tytul, opis, priorytet, zaakceptowano, status) VALUES (NULL, '".$_SESSION['valid_user']."', 'zadania', '".date('d.m.Y')."', '".$_POST['towho']."', '".$_POST['topic']."', '".$_POST['comment']."', '".$_POST['priority']."', '2018-01-01 1:00:00', 'start')";
mysqli_query($conn,$sf);
echo "Succes";
And everything works fine, only that "priority" doesn't work. It show UNDEFINED INDEX "priority" onto my INPUT query. Also... My buttons can't be wide on whole form, their width is defined by chars, but my intentions were different.

Use JQUERY to change the value of a hidden input field named priority and when either button is pressed change the value.
$(document).ready(function () {
$("#priority1").click(function(){
$("#priority").attr("value", "WYSOKI");
});
$("#priority2").click(function(){
$("#priority").attr("value", "ŚREDNI");
});
$("#priority3").click(function(){
$("#priority").attr("value", "NISKI");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post">
<div class="form-group">
<strong><label for="towho">Do kogo:</label></strong>
<select class="form-control" name="towho">
<option> - - - </option>
<!-- Skrypt pobierania danych (username) do Selecta -->
<?php
get_users();
?>
</select>
</div>
<hr></hr>
<div class="form-group">
<label for="usr">Temat:</label>
<input type="text" class="form-control" name="topic">
</div>
<hr></hr>
<div class="form-group" style="padding-top:10px">
<strong><label for="comment">Opis zadania:</label></strong>
<textarea class="form-control" rows="5" name="comment"></textarea>
</div>
<hr></hr>
<center>
<div class="form-group">
<strong><p>Priorytet:</p></strong>
<input type="text" value="" id="priority" name="priority" hidden>
<div class="btn-group btn-group-justified" role="group" aria-label="Justified button group">
<div class="btn-group" role="group">
<input type="button" class="btn btn-danger" value="WYSOKI" id="priority1">
</div>
<div class="btn-group" role="group">
<input type="button" class="btn btn-warning" value="ŚREDNI" id="priority2">
</div>
<div class="btn-group" role="group">
<input type="button" class="btn btn-success" value="NISKI" id="priority3">
</div>
</div>
</div>
</center>
<hr></hr>
<div class="form-group">
<button name="submit" type="worksubmit" class="btn btn-info btn-block">Zaakceptuj</button>
</div>
</form>

Related

isset in php not working

I have a to get a data from this form and insert it into database.
I am not able to find any solution online.
Everything works on the browser but the data is not getting stored into the table and echo inside the if block is not printed on screen.
The code is what can be seen below.
HTML
<form action="" method="POST">
<div class="container-fluid">
<div class="container">
<div class="row">
</div>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-10 col-lg-4 col-md-offset-3" style="top:100px">
<div class="offer offer-radius offer-primary">
<div class="shape">
<div class="shape-text">
<span class="glyphicon glyphicon-user"></span>
</div>
</div>
<div class="offer-content">
<h4 style="margin-left:70px;font-size:20px">Enter your details below and enroll your slot</h4>
<div class="container col-xs-12">
<div class="stepwizard">
<div class="stepwizard-row setup-panel">
<div class="stepwizard-step">
<a href="#step-1" type="button" class="btn btn-primary btn-circle">
<i class="glyphicon glyphicon-user" style="margin-left: -18px;top: -24px;font-size: 90%;"></i></a>
</div>
<div class="stepwizard-step">
<a href="#step-2" type="button" class="btn btn-default btn-circle" disabled="disabled">
<i class="glyphicon glyphicon-education" style="margin-left: -18px;top: -24px;font-size: 90%;"></i></a>
</div>
<div class="stepwizard-step">
<a href="#step-4" type="button" class="btn btn-default btn-circle" disabled="disabled">
<i class="glyphicon glyphicon-ok" style="margin-left: -18px;top: -24px;font-size: 90%;"></i></a>
</div>
</div>
</div>
<div class="row setup-content" id="step-2">
<div class="col-xs-12">
<div class="col-md-10">
<h3>Qualification</h3>
<div class="col-xs-12 col-xs-offset-1" style="top:-50px">
<button class="btn btn-primary nextBtn btn-lg pull-right" type="button" >Next</button>
</div>
<div class="col-xs-12" style="clear:both;top:-45px">
<input maxlength="20" type="text" required="required" class="form-control" placeholder="B Tech" readonly="" />
</div>
<div class="col-xs-12 form-group" style="clear:both;top:-40px">
<label for="subject">
Year of Pass out</label>
<input maxlength="20" type="text" required="required" class="form-control" placeholder="2016" readonly="" />
</div>
<div class="col-xs-12 form-group" style="clear:both;top:-40px">
<label for="subject">
Branch</label>
<select id="subject" name="subject" class="form-control" required="required">
<option value="na" selected="">Choose One:</option>
<option value="service">Computer Science</option>
<option value="suggestions">Information Technology</option>
<option value="product">Electronics & Communication</option>
<option value="product">Electronics & Electrical</option>
<option value="product">Instrumentation</option>
</select>
</div>
<div class="col-xs-12" style="clear:both;top:-40px">
<label for="subject">
Percentage (%)</label>
<input maxlength="6" type="text" class="form-control" placeholder="Your Percentage" name="percentage" />
</div>
</div>
</div>
</div>
</div>
<div class="row setup-content" id="step-1">
<div class="col-xs-12">
<div class="col-md-12">
<div class="container">
<div class="row">
<div class="col-xs-4">
<button class="btn btn-primary nextBtn btn-lg pull-right" type="button" >Next</button>
<h3>Personal Information</h3>
<div class="col-xs-12" style="padding:15px">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
<input maxlength="100" type="text" class="form-control" placeholder="First Name" name="F_Name" />
</div>
</div>
<div class="col-xs-12" style="clear:both;padding:10px">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
<input maxlength="100" type="text" class="form-control" placeholder="Last Name" name="L_Name" />
</div>
</div>
<div class="col-xs-12" style="clear:both;padding:10px">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
<input maxlength="100" type="text" class="form-control" placeholder="Your Email" name="EmailID" />
</div>
</div>
<div class="col-xs-12" style="clear:both;padding:10px">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-phone fa" aria-hidden="true"></i></span>
<input maxlength="14" type="text" class="form-control" placeholder="(+91) 555-555-5555" name="Phone_Number" />
</div>
</div>
<div class="col-xs-12" style="clear:both;padding:10px">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-phone fa" aria-hidden="true"></i></span>
<input maxlength="14" type="text" class="form-control" placeholder="Reference Code" name="R_Code" />
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row setup-content" id="step-4">
<div class="col-xs-12">
<div class="col-xs-12">
<div class="container" style="height:307px;">
<h4>Registered</h4>
<div class="col-xs-4" style="margin-left:-60px;top:60px">
<button class="btn btn-success completeBtn btn-lg pull-right" type="button" name="proceed">Proceed to payment</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
PHP
<?php
$cn= mysqli_connect('localhost','testuser','password','invoicedb');
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "string";
if(isset($_POST["proceed"]))
{echo "string";
$fname=$_POST['F_Name'];
$lname=$_POST['L_Name'];
$emailID=$_POST['EmailID'];
$phone_number=$_POST['Phone_Number'];
$reference_code=$_POST[‘R_Code’];
$branch=$_POST[‘subject’];
$percentage=$_POST[‘percentage’];
echo "insert into students(first_name,last_name,email,phone_number,reference_code,branch,percentage) VALUES ('$fname','$lname','$emailID','$phone_number','$reference_code','$branch','$percentage')";
mysqli_query($cn,"insert into students(first_name,last_name,email,phone_number,reference_code,branch,percentage) VALUES ('$fname','$lname','$emailID','$phone_number','$reference_code','$branch','$percentage')");
//best outside the if statement so user isn't stuck on a white blank page.
header("location:confirm.php");
exit;
}
?>
You can access $_POST[] values only if your form is submitted,
So, Replace this,
<button class="btn btn-success completeBtn btn-lg pull-right" type="button" name="proceed">Proceed to payment</button>
with This,
<button class="btn btn-success completeBtn btn-lg pull-right" type="submit" name="proceed">Proceed to payment</button>
It will work, without replacing your button with input.
If you are submitting the form in the same page then you should use -
<?php
$cn= mysqli_connect('localhost','testuser','password','invoicedb');
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "string";
if($_SERVER["REQUEST_METHOD"] == "POST")
{echo "string";
$fname=$_POST['F_Name'];
$lname=$_POST['L_Name'];
$emailID=$_POST['EmailID'];
$phone_number=$_POST['Phone_Number'];
$reference_code=$_POST[‘R_Code’];
$branch=$_POST[‘subject’];
$percentage=$_POST[‘percentage’];
echo "insert into students(first_name,last_name,email,phone_number,reference_code,branch,percentage) VALUES ('$fname','$lname','$emailID','$phone_number','$reference_code','$branch','$percentage')";
mysqli_query($cn,"insert into students(first_name,last_name,email,phone_number,reference_code,branch,percentage) VALUES ('$fname','$lname','$emailID','$phone_number','$reference_code','$branch','$percentage')");
//best outside the if statement so user isn't stuck on a white blank page.
header("location:confirm.php");
exit;
}
?>
if(isset($_POST["proceed"])){}
$_POST["proceed"] is not set at all, it needs a value in it to make sure it is set.
instead of checking if, just print the post values and try if your form is getting submitted.
inside php tag write below code and try echoing the data.
echo '<pre>';
print_r($_POST);
echo '----------------';
print_r($_REQUEST);

Clear data in modal when clicking close in Laravel

I have a modal (Bootstrap) for creating a new product. When I click the "close" button, which creates a new product, the old data still does not clear from the modal.
My code:
<div class="modal fade" id="product" role="dialog" aria-hidden="true" data-target="#myModal" data-backdrop="static" data-keyboard="false" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Create new product</h4>
</div>
<div class="modal-body">
<form role="form" action="{{ route('admin.product.addProduct')}}" method="post" id="frmProduct">
{{ csrf_field() }}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="">Product Name</label>
<input type="text" class="form-control" id="" name="product_name">
</div>
<div class="form-group">
<label for="">Product Type</label>
<input type="text" class="form-control" id="" name="product_type_id">
</div>
<div class="form-group">
<label for="">Price</label>
<input type="text" class="form-control" id="" name="price">
</div>
<div class="form-group">
<label for="">Status</label>
<select class="form-control input-sm m-bot15" id="" name="status">
<option value="0">Inactive</option>
<option value="1">Active</option>
</select>
</div>
<div class="form-group">
<label for="img">Image</label>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-default btn-file">
Choose <input type="file" id="imgInp">
</span>
</span>
<input type="text" class="form-control" name="product_image" readonly>
</div>
<img id='img-upload' class="image_responsive" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="">Description</label>
<textarea class="form-control" id="" name="description"></textarea>
</div>
<div class="form-group">
<label for="">Note</label>
<textarea class="form-control" id="" name="addition_information"></textarea>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<input type="submit" value="Save" class="btn btn-success">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
How can I clear the data when closing the modal?
You can add event listener to your close button by adding an id to your close button for example id=close-btn:
document.getElementById("close-btn").addEventListener("click", function(){
document.getElementById("frmProduct").reset();
});
You can use hidden.bs.modal event:
This event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete).
The snippet:
$('#product').on('hidden.bs.modal', function(e) {
$(this).find('form').trigger('reset');
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#product">
Launch modal
</button>
<div class="modal fade" id="product" role="dialog" aria-hidden="true" data-target="#myModal" data-backdrop="static" data-keyboard="false" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Create new product</h4>
</div>
<div class="modal-body">
<form role="form" action="{{ route('admin.product.addProduct')}}" method="post" id="frmProduct">
{{ csrf_field() }}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="">Product Name</label>
<input type="text" class="form-control" id="" name="product_name">
</div>
<div class="form-group">
<label for="">Product Type</label>
<input type="text" class="form-control" id="" name="product_type_id">
</div>
<div class="form-group">
<label for="">Price</label>
<input type="text" class="form-control" id="" name="price">
</div>
<div class="form-group">
<label for="">Status</label>
<select class="form-control input-sm m-bot15" id="" name="status">
<option value="0">Inactive</option>
<option value="1">Active</option>
</select>
</div>
<div class="form-group">
<label for="img">Image</label>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-default btn-file">
Choose <input type="file" id="imgInp">
</span>
</span>
<input type="text" class="form-control" name="product_image" readonly>
</div>
<img id='img-upload' class="image_responsive" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="">Description</label>
<textarea class="form-control" id="" name="description"></textarea>
</div>
<div class="form-group">
<label for="">Note</label>
<textarea class="form-control" id="" name="addition_information"></textarea>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<input type="submit" value="Save" class="btn btn-success">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>

Submit button is not working in my form

This is my form. When I click save, vales are not getting posted and redirected to note.php page.
<form method="post" action="note.php" enctype="multipart/form-data">
<div class="form-group">
<div class="col-sm-1 padding">
<label>Title :</label>
</div>
<div class="col-sm-11 padding">
<input type="text" class="form-control select2-offscreen form-text" name="title" required autocomplete="off" placeholder="Note Heading" tabindex="-1">
</div>
</div>
<div class="toolbar-btns">
<div class="file-attach">
<div class="form-group">
<input type="file" name="file[]" multiple="multiple">
</div>
</div>
<div class="form-group">
<button type="submit" class="btn send-btn" name="btn-note-save">Save</button>
<input type="reset" style="background-color:#417fb9; color:#fff;" class="btn btn-default" id="submitForm" value="Clear"></input>
</div>
</div>
</form>
I think your problem is in note.php. Please use the code below to get the posted values.
<?php
$title=($_POST['title']);
echo $title;
$pic = $_FILES["file"]["name"];
$folder = "../images/users/";
move_uploaded_file($_FILES['file']["tmp_name"], "$folder".$pic);
?>

How can I use buttons in an HTML form, but not for submitting?

I want to use a button group in an HTML form rather than for example check boxes or radios.
I am using this code:
<form class="form-horizontal" role="form" action="client/index" method="post">
<fieldset>
<!-- Text input-->
<div class="form-group">
<label class="col-sm-2 control-label" for="nickname">Nickname</label>
<div class="col-sm-10">
<input id="nickname" name="nickname" type="text" class="form-control" required="required">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="gender">Gender</label>
<div class="col-sm-10">
<div class="btn-group">
<button type="button" class="btn btn-default" value="Female" name="username">Female</button>
<button type="button" class="btn btn-default" value="Male" name="username">Male</button>
</div>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-sm-2 control-label" for="button"></label>
<div class="col-sm-offset-2 col-sm-10">
<button id="button" class="btn btn-lg btn-primary" type="submit">Enter</button>
</div>
</div>
</fieldset>
</form>
but <?php echo $_POST['username']; ?> is null.
What am I doing wrong?
A <button> is not an <input>, so you can't send that button data to the server, unless you use something like JavaScript to have its value copied into a <input type="hidden">, or submit the form via Ajax and pull the value on submit.
The only button value that is included in the form is the value of the button that was used to submit the form.
If you don't use those buttons to submit the form, you need to include the value in the form in a different way, for example using Javascript to put the value in a hidden field when then button is clicked:
<input type="hidden" name="username" id="username">
Then:
<button type="button" class="btn btn-default" value="Female" onclick="document.getElementById('username').value = this.value;">Female</button>
<button type="button" class="btn btn-default" value="Male" onclick="document.getElementById('username').value = this.value;">Male</button>
You still have the usability problem, though. The button might have focus right after you clicked it, but if the user does anything else in the form the selection is not visible. If you want to use buttons that way you might consider changing the apperance of the selected button.
It's a BAD idea. I repeat a bad idea, but it can be done with javascript and a hidden form field. A rough version:
<input type="hidden" id="username" name="username" />
<div class="btn-group">
<button type="button" class="btn btn-default" value="female" name="btnUsername"
onClick="document.getElementByID('username').value='Female';">Female</button>
<button type="button" class="btn btn-default" value="male"
onClick="document.getElementByID('username').value='Male';"
name="username">Male</button>
</div>
Why is it a BAD IDEA? Because it breaks the normal user expectation of buttons, provides no feed back on what has been selected and has no obvious way of undoing the selection. Radio buttons or check boxes do this so much better. If you don't like the way they look find one of the many ways of improving their look on the internet.
Button types as button will not work with a POST method, you need to use a submit type.
What I changed:
<button type="button" to <button type="submit" and action="" for testing purposes.
PHP
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
$gender = $_POST['username'];
echo $gender;
}
?>
<form class="form-horizontal" role="form" action="" method="post">
<fieldset>
<!-- Text input-->
<div class="form-group">
<label class="col-sm-2 control-label" for="nickname">Nickname</label>
<div class="col-sm-10">
<input id="nickname" name="nickname" type="text" class="form-control" required="required">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="gender">Gender</label>
<div class="col-sm-10">
<div class="btn-group">
<button type="submit" class="btn btn-default" value="Female" name="username">Female</button>
<button type="submit" class="btn btn-default" value="Male" name="username">Male</button>
</div>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-sm-2 control-label" for="button"></label>
<div class="col-sm-offset-2 col-sm-10">
<button id="button" class="btn btn-lg btn-primary" type="submit">Enter</button>
</div>
</div>
</fieldset>
</form>
<button type="button" is used in conjunction with JS.
Plus, you haven't provided any PHP in your question besides
<?php echo $_POST['username']; ?>
Answer
It's possible, in pure HTML like so:
<div class="form-group">
<label class="col-sm-2 control-label" for="gender">Gender</label>
<div class="col-sm-10">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default btn-lg">
<input type="radio" name="username" value="Female" id="username"> <i class="fa fa-female"></i> Female
</label>
<label class="btn btn-default btn-lg">
<input type="radio" name="username" value="Male" id="username"> <i class="fa fa-male"></i> Male
</label>
</div>
</div>
</div>

Please help: How to make a PHP form for the following HTML5

I am trying to make a PHP form to the following HTML5, I am a total newbie to PHP and not a "progammer" I have tried to make it work, however am totally at a lose TO correspond this with a "contact.php file"
I am trying to make a form for the following that when sent will have the attribute options selected
<div class="form-group">
<label class="col-md-3 control-label">Choose Location</label>
<div class="col-md-9">
<select class="form-control">
<option>Asia</option>
<option>Australasia</option>
<option>Europe</option>
<option>Latam</option>
<option>Middle East</option>
<option>USA</option>
</select>
</div>
</div>
<div class="form-actions fluid">
<div class="col-md-offset-3 col-md-9">
<button type="submit" class="btn green">Submit</button>
button type="button" class="btn default">Cancel</button>
</div>
You need to put your HTML in 'form' tag, and there's 1 opening 'button' tag missing. See the adjusted code below:
<form action="contact-submit-handler.php" method="post">
<div class="form-group">
<label class="col-md-3 control-label">Choose Location</label>
<div class="col-md-9">
<select class="form-control">
<option>Asia</option>
<option>Australasia</option>
<option>Europe</option>
<option>Latam</option>
<option>Middle East</option>
<option>USA</option>
</select>
</div>
</div>
<div class="form-actions fluid">
<div class="col-md-offset-3 col-md-9">
<button type="submit" class="btn green">Submit</button>
<button type="button" class="btn default">Cancel</button>
</div>
</div>
</form>
Need to open the tag for Cancel button
Cancel

Categories