I'm trying to create a form in which a user can upload an image with a title and description. All works fine except the image that isn't being moved to my server. Can anyone tell me what I'm doing wrong here? The folder which I'm trying to upload to has permissions set to 777.
The PHP code:
<?php
}
session_start();
if(!session_is_registered(myusername))
{
header("location:login.php");
}
// connect to the database
$conn=mysql_connect(*censored*) or die("Kan geen verbinding maken met de DB server");
mysql_select_db("frankkluytmans_",$conn) or die("Kan database niet selecteren");
// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$pagid = mysql_real_escape_string(htmlspecialchars($_POST['pagid']));
$titlename = mysql_real_escape_string(htmlspecialchars($_POST['title']));
$contentname = mysql_real_escape_string(htmlspecialchars($_POST['contentedit']));
$image = mysql_real_escape_string(htmlspecialchars("/gfx/".$_FILES["image"]["name"]));
// check to make sure both fields are entered
if ($titlename == '' || $pagid == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($pagid, $titlename, $contentname, $error);
}
else
{
if (move_uploaded_file($_FILES["image"]["tmp_name"], "/gfx/".$_FILES["image"] ["name"]))
{
// save the data to the database
mysql_query("INSERT frankkluytmans SET pagid='$pagid', title='$titlename', content='$contentname', image='$image'") or die(mysql_error());
// once saved, redirect back to the view page
header("Location: index.php");
}
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','');
}
?>
The form:
<form action="" method="post" enctype="multipart/form-data">
<h2>
<span>NEW</span>
<input type="text" name="pagid" value="" />
</h2>
<div class="pages-content">
<strong>Title: *</strong> <input type="text" name="title" /><br/>
<strong>Content: *</strong> <textarea name="contentedit"</textarea><br/>
<input type="file" name="image" id="image" />
<input type="submit" name="submit" value="Submit">
</div>
<script>
window.onload = function() {
CKEDITOR.replace( 'contentedit' );
console.log("editor werkt");
};
</script>
</form>
You PHP code is broken:
<?php
}
There is a mistake in your file, it start with a }
You have unwanted } at first line
Are you sure that the location is correct?
Try adding $_SERVER['DOCUMENT_ROOT'] in front of dir.. for example:
$image_dir = $_SERVER['DOCUMENT_ROOT']."/gfx/".$_FILES["image"]["name"];
Related
I have been trying to get the PHP code to submit an email to a mysqlDB, but for some reason it is not working:
This is the form code in the HTML
<form class="header-signup" action="registration.php" method="post">
<input name="email" class="input-side" type="email" placeholder="Sign up now">
<input type="submit" value="Go" class="btn-side">
<p class="hs-disclaimer">No spam, ever. That's a pinky promise.</p>
</form>
For the PHP, I did the following (DB connection infos set to xxxxx):
<?php //start php tag
//include connect.php page for database connection
$hostname="xxxxxx";
$username="xxxxxx";
$password="xxxxxx";
$dbname="xxxxxx";
mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($dbname);
//Include('connect.php');
//if submit is not blanked i.e. it is clicked.
If(isset($_POST['submit'])!='')
{
If($_POST['email']=='')
{
Echo "please fill the empty field.";
}
Else
{
$sql="INSERT INTO MailingList (MAIL) VALUES('".$_POST['email']."')";
$res=mysql_query($sql);
If($res)
{
Echo "Record successfully inserted";
}
Else
{
Echo "There is some problem in inserting record";
}
}
}
?>
Do you know what might be the problem?
The php file is in the same folder than the webpage.
Thanks for your time
Regards
$_POST['submit']
does not exist, you have to specify the name for the submit button
<input type="submit" name="submit"........>
Please try this
You could also use this conditional for a POST request
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
And check the input with a var_dump($_POST); to see if the value exists in the array.
This is only if you're expecting one form. If you want multiple forms on the page you could make use of naming your submit button in the HTML code
<form class="header-signup" action="registration.php" method="post">
<input type="submit" name="action1" value="Go" class="btn-side">
</form>
You could also use this if you set an name on the submit button
if(isset($_POST['action1']))
{
var_dump("hit");
}
Normally when we submit form in php and show errors then the errors are posted in other page.
Now my question is.
If we want to show the errors on same page mean on the form page, then how to do it?
I used session for that and found it better then other process but when I want to show all session variable at once then it will not show, why?
My code is:
if(isset($_POST) && count($_POST)>0) {
$user =test_input($_POST['user']);
$pass = test_input($_POST['pass']);
$securepassword=hash('sha512', $pass);
if(empty($user)&&empty($pass))
{
echo 'fill every field';
}
else if(empty($user)||empty($pass))
{
echo 'fill both the field';
}
else
{
$sSQL = "SELECT * FROM signup WHERE Username ='".$user."' AND Password = '".$securepassword."'";
$result = mysql_query($sSQL) or die(mysql_error());
$row=mysql_num_rows($result);
if($row==1)
{
// Set username session variable
$_SESSION['user'] = $user;
// Jump to secured page
header('location:index.php');
}
else
{
header('location:signin.php');
}
}
}
You better use JavaScript for validating the form. Lets say this is your HTML code.
<form method="post" name="loginForm" action="abc.php">
<input type="text" name="user">
<input type="password" name="pass">
</form>
Now, let’s add validate this form before you POST to abc.php:
<form method="post" name="loginForm" action="abc.php" onsubmit="return validater()">
<input type="text" name="user">
<input type="password" name="pass">
</form>
And the JavaScript is:
function validater() {
var a= document.forms["loginForm"]["user"].value;
if (a==null || a=="") {
alert("Fill out the username");
return false;
}
var b= document.forms["loginForm"]["pass"].value;
if (b==null || b=="") {
alert("Enter your password");
return false;
}
}
Logic is simple you can put the php code and html code on the same page. In the following order.
Put your php validation code.
Put your error code to show errors above the form.
Put your html code to show the form fields on the page.
There is no need of session to show errors to the user.
hi guys i am kinda new to php and i am trying to add validation on to the form. i want it so the form will not submit if it is empty.
<form id="form1" name="form1" method="post" action="category_created.php">
Enter a New Category Name :
<label for="cat"></label>
<input type="text" name="cat" id="cat" />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
and the form is being submitted to the file where the contents is being submitted to the database:
<?php
//category name received from 'new_category.php' is stored in cariable $cat
$cat=$_POST['cat'];
$qry=mysql_query("INSERT INTO category(category)VALUES('$cat')", $con);
if(!$qry)
{
die("There Was An Error!". mysql_error());
}
else
{
echo "<br/>";
echo "Topic ".$cat." Added Successfully";
echo "<br/>";
}
?>
any help will be appreciated
thanks
use onsubmit and validate like below
<form onsubmit="return validate()" id="form1" name="form1" method="post" action="category_created.php" >
.....
</form>
and in javasctipt
<script>
function validate(){
if(document.getElementById('cat').value.length<1)
{
alert('Please enter the Category Name');
return false;
}
else
{
return true;
}
}
</script>
or you could submit the form via ajax, in the db-file you check whether the fields are empty, ifso echo an error message and print it via javascript. By doing this you can style everything the way you want to :)
In Php I am doing a file upload with some data insert
My form looks like this
<form id="formID1" method="post" action="'.$_SERVER['REQUEST_URI'].'" enctype="multipart/form-data">
<label for="">'.$this->l('Add Store Type').'</label>
<input type="text" name="store_type" />
<label for="">'.$this->l('Upload Your Custom Icon').'</label>
<input type="FILE" name="custom_icon" id="custom_icon" />
<input type="submit" name="store_config" value="'.$this->l('Add Store Configuration').'" class="button" />
<form>
For uploading images I am doing a folder with 777 permission on it.
$uploaddir=dirname(__FILE__)."/storeconfig";
if(!file_exists($uploaddir)) {
mkdir($uploaddir, 777, true);
}
$tmpName=$uploaddir."/";
Now for inserting values I am doing this code.
if(isset($_POST['store_config'])) {
global $uploaddir;
$custom_icon_name = time().$_FILES['custom_icon']['name'];
$allowed_extensions = array('gif','jpg','png');
$path_info = pathinfo($custom_icon_name);
if( !in_array($path_info['extension'], $allowed_extensions)) {
echo "Incorrent file extension, Please Use png,jpg or gif files only";
} else {
if(mysql_query('INSERT INTO `'._DB_PREFIX_.'store_config` (`store_config_id`,`store_type`,`custom_icon`) VALUES ("","'.$store_type.'","'.$custom_icon_name.'") ')) {
if(move_uploaded_file($_FILES['custom_pin']['tmp_name'],$tmpName.$custom_icon_name)) {
echo "Settings updated successfully";
} else {
echo "You Have Some Errors";
}
}
}
}
Here the values are storing into the database successfully after doing click on button Add Store Configuration but still I am getting the error You Have Some Errors which I have set in my form.It should show Settings updated successfully. Can someone tell me how this is error is coming?
I have a php form that saves the info to my database and sends an email upon completion. however it will not validate the fields to see if they are null, instead it prints both the set and not set options. Any ideas as to why this could be happening? It worked perfectly before i added the form field validation to it.
As a side note it works in FF and Chrome due to the html 5 aria-required, but not in IE
html
<form id="contact" name="contact" action="register1.php" method="post">
<label for='Cname'>Camper Name</label>
<input type="text" name="Cname" maxlength="50" value="" required aria-required=true />
<input type="hidden" id="action" name="action" value="submitform" />
<input type="submit" id="submit" name="submit" value="Continue to Camp Selction"/>
</form>
php
<?php
//include the connection file
require_once('connection.php');
//save the data on the DB and send the email
if(isset($_POST['action']) && $_POST['action'] == 'submitform')
{
//recieve the variables
$Cname = $_POST['Cname'];
//form validation (this is where it all breaks)
if (isset($Cname)) {
echo "This var is set so I will print.";
}
else {
echo '<script type="text/javascript">alert("please enter the required fields");</script>';
}
//save the data on the DB (this part works fine)
<?php
$Cname = isset($_POST['Cname']) ? $_POST['Cname'] : null;
if (isset($Cname)) {
echo "This var is set so I will print.";
}
// OR
if (isset($_POST['Cname'])) {
// Perform your database action here...
}
?>
Consider using PHP's empty function
PHP.Net Manual Empty()
You can update your code to the following:
if(!empty($Cname)) {
echo "This var is set so I will print.";
}
Do you just need an "exit()" in the else?