Submit button is not working in my form - php

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);
?>

Related

PHP CMS - Add image is not working

I am creating a simple CMS and I would like the users to be able to upload an image in their posts. The form is working properly besides the image. For some reason I get this message 'Notice: Undefined index: post_img'. Can someone help me solve this?
Thanks.
Code:
<form method="post" action="add_post.php">
<?php
if(isset($_POST['add_post'])) {
$add_title = $_POST['post_title'];
$post_img = $_FILES['post_img']['name'];
$post_img_temp = $_FILES['post_img']['tmp_name'];
$add_content = $_POST['post_content'];
move_uploaded_file($post_img_temp, "images/$post_img");
$add_post_query = "INSERT INTO posts (post_title, post_img, post_content) ";
$add_post_query .= "VALUES ('$add_title', '$post_img', '$add_content') ";
mysqli_query($connection, $add_post_query);
}
?>
<div class="form-group">
<label for="post-title">Title</label>
<input type="text" name="post_title" class="form-control">
</div>
<div class="form-group">
<label for="post-img">Image</label>
<input type="file" name="post_img" class="form-control">
</div>
<div class="form-group">
<label for="post-content">Content</label>
<textarea name="post_content" class="form-control"></textarea>
</div>
<div class="form-group">
<input type="submit" name="add_post" class="btn btn-primary" value="Add Post">
</div>
</form>
You need to properly place your form tag. Also you need to set the form to accept files by adding the enctype attribute to the form tag
<form method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="post-title">Title</label>
<input type="text" name="post_title" class="form-control">
</div>
<div class="form-group">
<label for="post-img">Image</label>
<input type="file" name="post_img" class="form-control">
</div>
<div class="form-group">
<label for="post-content">Content</label>
<textarea name="post_content" class="form-control"></textarea>
</div>
<div class="form-group">
<input type="submit" name="add_post" class="btn btn-primary" value="Add Post">
</div>
</form>

$_POST method turns empty

Firstable, hello everyone. I have issue about the $_POST method, when I write this code block;
if (isset($_POST['menuduzenle'])) {
$menu_id=$_POST['menu_id']; //It didn't turn with value in mysql
$ayarguncelle=mysql_query("update menuler set menu_ad='".$_POST['menu_ad']."',menu_link='".$_POST['menu_link']."'where menu_id='$menu_id'");
if(mysql_affected_rows())
{
header("Location:../menuleriduzenle.php?guncelleme=basarili&menu_id=$menu_id");
}else{
header("Location:../menuleriduzenle.php?guncelleme=basarisiz&menu_id=$menu_id");}
}
menu_id turns emty, what I need to do for solve this?
and here it's html code
form
<form action="network/islem.php" method="POST">
<div class="col-md-12">
<div class="form-group col-md-9">
<label>Menu Adı: </label>
<input class="form-control" name="menu_ad" value="<?php echo $menucek['menu_ad']; ?>" />
</div>
</div>
<div class="col-md-12">
<div class="form-group col-md-9">
<label>Menu Link: </label>
<input class="form-control" name="menu_link" value="<?php echo $menucek['menu_link']; ?>" />
</div>
</div>
<div class="col-md-12">
<div class="form-group col-md-6">
<input class="btn btn-primary col-md-3" type="submit" name="menuduzenle" value="Edit Menu">
</div>
</form>
and
<td><button class="btn btn-primary col-md-12">Edit</button></td>
You not send any one input with name 'menu_id' on your from.
<form action="network/islem.php" method="POST">
<div class="col-md-12">
<div class="form-group col-md-9">
<label>Menu Adı: </label>
<input class="form-control" name="menu_ad" value="<?php echo $menucek['menu_ad']; ?>" />
</div>
</div>
<div class="col-md-12">
<div class="form-group col-md-9">
<label>Menu Link: </label>
<input class="form-control" name="menu_link" value="<?php echo $menucek['menu_link']; ?>" />
</div>
</div>
<div class="col-md-12">
<div class="form-group col-md-6">
<input class="btn btn-primary col-md-3" type="submit" name="menuduzenle" value="Edit Menu">
</div>
<input type="hidden" name="menu_id" value="<?php echo $_GET['menu_id'] ?>" />
</form>
You input name "menu_ad" would be "menu_id" or fault other input with name "menu_id" on you sample.

Submit button has to be in the same form as email/pw field?

I´m quite new to php so this might just be a stupid question. Is there a way to put the submit button into another form field, or must it be the same? Does it really matter if I create a new field somewhere else?
Not working Code:
<div class="container">
<form method="post">
<div class="form-group">
<input type="text" class="form-control" name="emailfield" id="emailfeld2" placeholder="Emailadresse (z.B Max#Max.de)">
<input type="password" class="form-control" name="passwordfield" id="passwortfeld" placeholder="Passwort">
<input type="password" class="form-control" name="cpasswordfield" id="cpasswortfeld" placeholder="Passwort bestätigen">
</div>
</form>
<div class="form-group">
<form method="post">
<button type="submit" class="btn btn-success" name="submitgo">Sign Up!</button>
</form>
</div>
</div>
<?php
echo $_POST['emailfield']."<br>";
?>
<div class="container-opacity">
</div>
Working code:
<div class="container">
<form method="post">
<div class="form-group">
<input type="text" class="form-control" name="emailfield" id="emailfeld2" placeholder="Emailadresse (z.B Max#Max.de)">
<input type="password" class="form-control" name="passwordfield" id="passwortfeld" placeholder="Passwort">
<input type="password" class="form-control" name="cpasswordfield" id="cpasswortfeld" placeholder="Passwort bestätigen">
<button type="submit" class="btn btn-success" name="submitgo">Sign Up!</button>
</div>
</form>
</div>
<?php
echo $_POST['emailfield']."<br>";
?>
<div class="container-opacity">
</div>
It is generally bad practice to have form elements outside of the form they belong to. It will work if you only have one <form> tag and move it outside of you main <div class="container">
<form method="post">
<div class="container">
<div class="form-group">
<input type="text" class="form-control" name="emailfield" id="emailfeld2" placeholder="Emailadresse (z.B Max#Max.de)">
<input type="password" class="form-control" name="passwordfield" id="passwortfeld" placeholder="Passwort">
<input type="password" class="form-control" name="cpasswordfield" id="cpasswortfeld" placeholder="Passwort bestätigen">
</div>
<div class="form-group">
<button type="submit" class="btn btn-success" name="submitgo">Sign Up!</button>
</div>
</div>
</form>
<?php echo $_POST[ 'emailfield']. "<br>"; ?>
<div class="container-opacity">
</div>

Form not POST-in

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.

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