Form not POST-in - php

Does anybody know why this piece of code is not posting?
<form class="form-horizontal" action="./admin/addImages.php" method='post'>
<div class="form-group">
<label for="slika1" class="col-sm-2 control-label">Image</label>
<div class="col-sm-9">
<input id="slika1" name="slika1" type="text" class="form-control" placeholder="Image URL">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button name='saveForm' type="submit" class="btn btn-success">Add image</button>
</div>
</div>
</form>
I also have this php code which should get the posted input, but its not working:
<?php if (isset($_POST['saveForm'])) {
$img = $_POST['slika1'];
echo "$img";
} else {
echo "nothing posted";
}
?>
Any help is much appreciated.

Your URL has a period, not sure if this would cause it not to post, but try without.
action="/admin/addImages.php"

replace $_POST['saveForm'] with $_POST['slika1']
or another alternative can be.
You replace
<button name="saveForm" type="submit" class="btn btn-success">Add image</button>
with
<input name="saveForm" type="submit" class="btn btn-success" value="Add Image">

I have tried to post on same page and its working fine and i got data.
<form class="form-horizontal" action="" method='post'>
<div class="form-group">
<label for="slika1" class="col-sm-2 control-label">Image</label>
<div class="col-sm-9">
<input id="slika1" name="slika1" type="text" class="form-control" placeholder="Image URL">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button name='saveForm' type="submit" class="btn btn-success">Add image</button>
</div>
</div>
</form>
<?php if (isset($_POST['saveForm'])) {
$img = $_POST['slika1'];
echo "$img";
} else {
echo "nothing posted";
}
?>
I have entered "www.google.com".
Output
www.google.com

TESTED As it is, it should work. The problem has to be somewhere else. Maybe the image name that you are posting is empty.

Related

Form - button group with same "name" value

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>

PHP : Echo not working on POST method from form

I am a bit new to PHP and I'm having a bit of difficulties here and there. I am developing a form and wish to display an error box if any of the fields are empty when the 'Submit' Button is pressed. I've tried the following code but the echo is still not appearing. Any suggestions ?
Form Code:
<div style="padding-top:40px">
<div style="text-center; padding-right:25%; padding-left:25%">
<div class="form-area">
<form role="form" method="$_POST" action="searchEmployee.php">
<br style="clear:both">
<h3 style="margin-bottom:25px; text-align: center;">Visitor Form</h3>
<div class="form-group">
<label>Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<label>Surname:</label>
<input type="text" class="form-control" id="surname" name="surname" placeholder="Surname" required>
</div>
<div class="form-group">
<label>ID Card:</label>
<input type="text" class="form-control" id="idCard" name="idCard" placeholder="ID Card No" required>
</div>
<div class="form-group">
<label>Visitor Card Number:</label>
<input type="text" class="form-control" id="cardNumber" name="cardNumber" placeholder="Card No" required>
</div>
<div class="form-group">
<intput type="button" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
</div>
</form>
</div>
</div>
</div>
PHP Code:
<?php
if (isset($_POST['submit'])) {
$required = array('name', 'surname', 'ID', 'visitorCard');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
echo "All fields are required.";
}
}
?>
You are using method as $_POST in your form attribute,
It should be only POST.
So, replace your form line with,
<form role="form" method="POST" action="searchEmployee.php">
and also, change Submit button line to,
<input type="submit" name="submit" value="Sign In Visitor" class="btn btn-primary pull-right" />
Here are few mistakes:
You are using method as $_POST in your form attribute, It should be only POST.
Your form will not submit because your button is not a submit type. it should
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor" />
Or
<button type="submit" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
You need to make 2 change as below
Change button type like this
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor">
Change Form method
<form role="form" method="POST" action="searchEmployee.php">
Change this
<intput type="button" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
To
<input type="button" id="submit" name="submit" value="submit" class="btn btn-primary pull-right" />Sign In Visitor
and Also
this
<form role="form" method="$_POST" action="searchEmployee.php">
To
<form role="form" method="POST" action="searchEmployee.php">
How will you get POST['submit'] value when you have not even set, that means your if code won't execute it and so it won't echo or alert it.
For Best practice of debugging always try to do this whenever such instances occurs while coding.
print_r($_POST);
This will show you an array of POST variables
change $_POST to POST and put <input type="submit" instead of <input type = "button"
<div style="padding-top:40px">
<div style="text-center; padding-right:25%; padding-left:25%">
<div class="form-area">
<form role="form" method="post" action="searchEmployee.php"> <br style="clear:both"> <h3 style="margin-bottom:25px; text-align: center;">Visitor Form</h3> <div class="form-group"> <label>Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required> </div>
<div class="form-group"> <label>Surname:</label>
<input type="text" class="form-control" id="surname" name="surname" placeholder="Surname" required> </div> <div class="form-group"> <label>ID Card:</label>
<input type="text" class="form-control" id="idCard" name="idCard" placeholder="ID Card No" required>
</div>
<div class="form-group">
<label>Visitor Card Number:</label> <input type="text" class="form-control" id="cardNumber" name="cardNumber" placeholder="Card No" required>
</div> <div class="form-group">
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor">
</div> </form> </div>
</div> </div>
Php Code:::searchEmployee.php
<?php if (isset($_POST['submit'])) { $required = array('name', 'surname', 'idCard', 'cardNumber');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) { if (!isset($_POST[$field])) {
$error = true;
} }
if ($error) {
echo "All fields are required."; } }
?>
First think change button type="submit".
Second think change PHP array
$required = array('name', 'surname', 'idCard', 'cardNumber');

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>

PHP not posting form enteries to the same page

Here's a form I have. I want to post the form entries to the same page and then do something else with it. However, echoing the values doesn't seem to be working. What am I doing wrong?
Here's the HTML part:
<div class="well">
<form name="myform" class="form-horizontal" id="form" onsubmit="return validateForm(this);" action="my.php" method="post">
<div class="control-group input-append">
<label for="text" class="control-label">Field1</label>
<div class="controls">
<div class="left">
<input id="Field1" name="Field1" type="text"/>
</div>
</div>
</div>
<br>
<div class="control-group input-append">
<label for="passwd" class="control-label">Field2</label>
<div class="controls">
<div class="left1">
<input id="Field2" name="Field2" type="password" />
</div>
</div>
</div>
<br>
<div class="control-group input-append">
<label for="passwd" class="control-label">Field3</label>
<div class="controls">
<div class="left1">
<input id="Field3" name="Field3" type="password"/>
</div>
</div>
</div>
<br>
<div class="control-group input-append">
<label for="select" class="control-label">Field4</label>
<div class="controls">
<div class="left1">
<select class="form-control" name="Field4">
<option value="OP1">OP1</option>
<option value="OP2">OP2</option>
<option value="OP3">OP3</option>
<option value="OP4">OP4</option>
</select>
</div>
</div>
</div>
<br>
<div class="control-group input-append">
<label for="text" class="control-label">Field5</label>
<div class="controls">
<div class="left">
<input type="Field5" id="Field5" name="type"/>
</div>
</div>
</div>
<br>
<div class="control-group input-append">
<label for="text" class="control-label">Field6</label>
<div class="controls">
<div class="left">
<input type="text" id="Field6" name="Field6"/>
</div>
</div>
</div>
<br>
<div class="control-group">
<div class="controls">
<div class="left1">
<input type="submit" class="btn btn-success"/>
</div>
</div>
</div>
</form>
</div>
PHP part:
<?php
if (isset($_POST['submit']))
{
echo $_POST['name'];
} else {?>
<?php;}?>
There is no field with the name $_POST['name'], though you have fields with numbered names, e.g. $_POST['Fields3'].
To debug the form request print out the whole $_POST array first:
var_dump( $_POST );
The type of an input is not the same as it's name!
Another option is to replace
if (isset($_POST['submit']))
with:
if($_SERVER['REQUEST_METHOD'] == 'POST')
Replace <input type="submit" class="btn btn-success"/>
to <input type="submit" name="submit" class="btn btn-success"/>
The problem is here:
if (isset($_POST['submit']))
Your submit button
<input type="submit" class="btn btn-success" />
does not have a value, nor does it have a name. Post works with these attributes:
$_POST['<name>'] = <value>
change your input to
<input type="submit" name="submit" value="Submit" class="btn btn-success" />
Btw, nice to see people still coding in XHTML. If you are using HTML 5, you should not end your non-closing tags with a slash ... />.

Categories