This is my code. The user adds an image from a previous page where it is then collected by $_FILES and processed in this page. The weird thing is, when someone adds an image, I get a large random paragraph of random numbers and symbols that is displayed on this page. I have no clue where this is from. Any thoughts?
And if you guys were curious, that form at the bottom redirects all the information to another page where is then added into the database.
<?php
$file = $_FILES['image']['tmp_name'];
if(isset($file))
{
$image_1 = file_get_contents($_FILES['image']['tmp_name']);
$image_name = $_FILES['image']['name'];
$image_size = getimagesize($_FILES['image']['tmp_name']);
}
if ($image_size===FALSE)
{
die("That is not an image. Please go back and choose and image.");
}
$title = $_POST['title'];
$author = $_POST['author'];
$isbn = $_POST['isbn'];
$price = $_POST['price'];
$location = $_POST['location'];
$class = $_POST['class'];
$description = $_POST['description'];
$contact = $_POST['contact'];
$img = $image_1;
$img_name = $image_name;
if(!$title || !$author || !$isbn || !$price || !$location || !$class ||
!$description || !$contact)
{
echo "You have not entered all the required details.<br/>"
."Please go back and try again.";
exit;
}
?>
<h1>This is what will be submitted</h1>
<?php
echo <<<_END
<pre>
Title: $title
Author: $author
ISBN: $isbn
Price: $price
Location: $location
Class: $class
Description: $description
Contact Information: $contact
Your Image :
If this is correct, please press sumbit. If you would like to
make changes, go back and make them
</pre>
_END;
?>
<form action='PSBE_INSERT_AD.php' method='post' enctype="multipart/form-data"/>
<input type="hidden" name="title" value="<?php echo $title;?>" />
<input type="hidden" name="author" value="<?php echo $author;?>" />
<input type="hidden" name="isbn" value="<?php echo $isbn;?>" />
<input type="hidden" name="price" value="<?php echo $price;?>" />
<input type="hidden" name="location" value="<?php echo $location;?>" />
<input type="hidden" name="class" value="<?php echo $class;?>" />
<input type="hidden" name="description" value="<?php echo $description;?>" />
<input type="hidden" name="contact" value="<?php echo $contact;?>" />
<input type="hidden" name="image" value="<?php echo $img;?>" />
<input type="hidden" name="image_name" value="<?php echo $img_name;?>" />
<input type="submit" value="Ad Post" />
</form>
</body>
You are getting the binary source of the image here:
$image_1 = file_get_contents($_FILES['image']['tmp_name']);
Then echoing it out in the hidden field here:
<input type="hidden" name="image" value="<?php echo $img;?>" />
That is most likely where that paragraph is coming from. Not sure why you are including it, the tmp path is all you really need.
Related
I am a newbie to PHP. & My PHP Code doesn't work, I want to update some date using MySQL but it seems that first IF condition is 'false' i don't why, I am using PHP 7 & XAMP as a local host, Dreamweaver as an IDE & this is my code:
if(isset($_POST["btn_edit"]))
{
$name = $_POST["name"];
$email = $_POST["email"];
$password = $_POST["password"];
if(!empty($_FILES["img"]["name"]))
{
$img = $_FILES["img"]["name"];
$img_temp = $_FILES["img"]["tmp_name"];
if(move_uploaded_file($img_temp, "assets/images/".$img))
{
$query = mysqli_query($Connection, "UPDATE entry_data SET names='$name',emails='$name',passwords='$password',images='$img' WHERE id='$ID'");
if($query)
{
$result = header("Location:index.php");
}
else
{
echo mysql_error();
}
}
}
else
{
$query = mysqli_query($Connection, "UPDATE entry_data SET names='$name',emails='$name',passwords='$password',images='$img' WHERE id='$ID'");
if($query)
{
echo "<h5>Updated</h5>";
}
}
}
it showing me nothing just refresh the page & this is HTML CODE:
<form method="post" enctype="multipart/form-data">
<input name="name" value="<?php echo $name ?>" />
<input name="email" value="<?php echo $email ?>" />
<input name="password" value="<?php echo $password ?>" />
<img width="50" height="50" src="<?php echo 'assets/images/'.$row[4] ?>" />
<input name="img" type="file" class="text-info" required="required" />
<br/>
<input name"btn_edit" type="submit" />
<?php if(isset($_POST["btn_edit"])) echo $result ?>
You have syntax issue in your button HTML.
This:-
<input name"btn_edit" type="submit" />
Need to be:-
<input name="btn_edit" type="submit" /><!-- = is missing in name -->
I'm having trouble with posting values typed into textarea. everything else works well, any idea how to make it work?
HTML:
<form id="formData2" action="artistuploader.php" method="post"
enctype="multipart/form-data">
<input type="hidden" name="size" value="1000000"></input>
<br/>
<input id="inputField" type="text" name="actname" placeholder="Act Name" >
<br>
<input id="inputField" type="text" name="fullname" placeholder="Full Name" >
<br>
<input id="inputField" type="text" name="genre" placeholder="Genre" >
<br>
<textarea id="inputField" name="biography" form="formData2" placeholder="Biography"<?php echo $biography; ?>></textarea>
<br>
<input id="inputField" type="file" name="artistImage" placeholder="Artwork" >
<br>
<input id="inputField" type="text" name="imagepath" placeholder="Image path URL" >
<br>
<input id="submitButton" type="submit" name="uploadArtist" value="Register Artist">
</form>
PHP
<?php
$msg = "";
//if Upload button is pressed
if (isset($_POST['uploadArtist'])){
$target = "uploads/artistPics".basename($_FILES['artistImage']['name']);
//connecting to our database
$db = mysqli_connect("127.0.0.1", "user", "pass", "tablename");
$tmp_name = $_FILES['artistImage']['tmp_name'];
$name = $_FILES['artistImage']['name'];
//getting the submitted form data
$ActName = $_POST['actname'];
$FullName = $_POST['fullname'];
$Genre = $_POST['genre'];
$ArtistPhoto = $_FILES['artistImage']['name'];
$imageURLpath = $_POST['imagepath'];
$Biography = $_POST['biography'];//having problem with this line here
//saving submitted data into database table songsDB
$sql = "INSERT INTO artistsdb (ActName,FullName,Genre,ArtistPhoto,Biography,imageURLpath) VALUES ('$ActName','$FullName','$Genre','$ArtistPhoto','$Biography','$imageURLpath')";
mysqli_query($db, $sql); //stores the submitted data into table
//now moving the uploaded image to uploads folder
if(move_uploaded_file($_FILES['artistImage']['tmp_name'], $target)){
$msg = "Uploaded Successful";
}else{
$msg = "There was a problem uploading Data";
}
}
//header("refresh:1; url=index.php"); ?>
Replace your textarea with
<textarea id="inputField" name="biography" form="formData2" placeholder="Biography"><?php echo $biography; ?></textarea>
I have this simple form that updates the values in database, the url of the page is
www.example.com?id=1
After i submit the form the values get updated and i get a success message but the url gets changed, it becomes
www.example.com
Can anyone tell how i can keep the url same i.e: www.example.com?id=1
The code is as follows
<?
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$text = mysqli_real_escape_string($con, $_POST['textvalue']);
$title = mysqli_real_escape_string($con, $_POST['title']);
$blogid = mysqli_real_escape_string($con, $_POST['blogid']);
$sql = "UPDATE blog SET text='".$text."', title='".$title."' WHERE id='".$blogid."'";
if (mysqli_query($con, $sql))
{
$msg = "Blog updated";
}
else
{
echo "There was an error";
}
}
?>
<div>
<?
if($msg!="")
{
echo $msg;
}
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data" >
<input type="text" name="title" value="<? echo $title; ?>" style="width:100%;"/>
<textarea name="textvalue" ><? echo $text; ?></textarea>
<input type="hidden" name="blogid" value="<? echo $blogid; ?>"/>
<input type="submit" name="edit" alt="edit" value="Edit"/>
</form>
</div>
While I found something similar to this question on here it didn't answer my question outright.
I have set up this php script to validate the form data, which works, after its validated I want it to then pass the info onto another script page to let the user then verify their input data and then mail the data. Its at this state that I'm having trouble. I've spent the last few days trying to find a solution to this and unfortunately coming up short.
<?php
$name_error = '';
$email_error = '';
$comments_error = '';
$error = false;
if (!empty($_POST['submitted']))
{ //if submitted, the validate.
$name = trim($_POST['name']);
if (empty($name))
{
$name_error='Name is required';
$error = true;
}
$email = trim($_POST['email']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
$email_error='E-mail address not valid';
$error = true;
}
$comments = trim($_POST['comments']);
if (empty($comments))
{
$comments_error='Comments are required';
$error = true;
}
if ($error == false)
{
$name_send = $name;
$email_send = $email;
$comments_send = $comments;
/* Redirect visitor to the thank you page */
header('Location: /mail.php');
exit();
}
}
The form this is attached to:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
<label>Your Name</label><br />
<input type="text" name="name" style="width:95%" class="text" value='<?php echo htmlentities($name) ?>' />
<br/>
<span class='error'><?php echo $name_error ?></span>
<br />
<label>Email</label><br />
<input type="email" name="email" style="width:95%" class="text" value='<?php echo htmlentities($email) ?>' />
<br/>
<span class='error'><?php echo $email_error ?></span>
<br />
<label for="comments" style="font-size:16px;">Feedback Comments</label><br />
<textarea name="comments" style="width:95%;" rows="8" value='<?php echo htmlentities($comments) ?>'></textarea>
<br />
<span class='error'><?php echo $comments_error ?></span>
<br />
<input type="checkbox" name="allowCommentPublish" checked="checked" />
<label for="allowCommentPublish" style="font-size:10px;">Allow these comments to be used on our website</label>
<fieldset class="optional">
<h2>[ OPTIONAL ]</h2>
<label>Company Name</label><br />
<input type="text" name="companyName" style="width:95%" class="text" />
<br/>
<label>Phone</label><br />
<input type="text" name="phone" style="width:95%" class="text" /><br/>
<div style="margin:5px 0px;">
<input type="checkbox" name="incmarketing" />
<label style="font-size:10px;"> Yes, you can email me specials and promotions.</label>
<br/>
</div>
</fieldset>
<fieldset>
<input type="submit" name="submitted" value="Send" />
</fieldset>
I will point out im focusing on the main data inputs: Name E-mail and comments.
I need the info from this form to be sent onward but i dont know exactly how to do this and any help will be appreciated greatly.
For passing the values to next page you will have to use either of the three methods.
1. Set cookies with the data.
2. Use global variable session.
3.Pass the data in the url.
For cookies u can set cookies with the values like
setcookie('name',$name);
in ur next page read those cookie data
For sessions:
$_SESSION['name']= $name;
for reading data from cookies & session:
$name = $_COOKIE['name'];
$name = $_SESSION['name'];
For using sessions you must add the line
session_start();
at the start of both the pages that send or receive(use) the data
and for urls
header('Location: /mail.php?name=$name&email=$email&comment=$comments');
Read more on using session
If you need to pass values from one script to another you can use $_SESSION variables. To start a session use: (at the top of the php script)
session_start();
$_SESSION['somename'] = $somevariable;
To access or get that same variable you can use this:
session_start();
$some_other_variable = $_SESSION['somename'];
or you can use hidden input fields.
You can use hidden fields and javascript to submit the form. However as this is the same php page as the original form you will need an if statement
echo '<form name="newForm" action="newpage.php" method="POST">';
echo '<input type="hidden" name="name2" value"' . $name . '">;
echo '<input type="hidden" name="email2" value"' . $email . '">;
echo '<input type="hidden" name="comments2" value"' . $comments . '"></form>;
echo '<script> if (document.getElementById("name2").value != ""){window.onload = function(){ window.document.newForm.submit(); }} </script>';
This is a part of my registration form. I want to display back the input user inserted if they forgot to enter all the info needed. However, I get this on my textbox in register form where everyone including my user can see it.
Notice:Undefined variable: name in D:\XAMPP\htdocs\registration.php on line 113
I want it to echo back the input that user had inserted and display it again so that user does not have to enter the same input over again. Help ?
$myusername=($_POST['username']);
$name=($_POST['name']);
if(isset($_POST['username'])) {
echo $_POST['username'];
}
if(isset($_POST['name'])) {
echo $_POST['name'];
}
<input type="text" name="username" size="60" value="<?php echo $myusername; ?>"/>
<input type="text" name="name" size="60" value="<?php echo $name; ?>"/>
Assuming you send the user back to the page they were at previously if the form fails to validate, the POST array is emptied. POST will only carry the information to the page that the form is submitting to.
You can use sessions to save the data in an array, indexed by form field name. Then when the user is sent back to the form, if there are any entries in the array, you can iterate over them through to your form fields.
you can controll the variables with if clause; means you can write:
if ( isset($_POST['send']) && isset($myusername) ) {
echo $myusername;
}
else {
echo '<span style="color:red;">Please complete this field</span>';
}
and do the same in html value for textfiels...
$myusername = isset($_POST['username']) ? ($_POST['username']) : '';
$name = isset($_POST['name']) ? ($_POST['name']) : '';
<input type="text" name="username" size="60" value="<?php echo $myusername; ?>"/>
<input type="text" name="name" size="60" value="<?php echo $name; ?>"/>
Try this, this should remove your error?
if(isset($_POST['username'])) {
$myusername=($_POST['username']);
<input type="text" name="username" size="60" value="<?php echo $myusername; ?>"/>
echo $_POST['username'];
}
if(isset($_POST['name'])) {
$name=($_POST['name']);
<input type="text" name="name" size="60" value="<?php echo $name; ?>"/>
echo $_POST['name'];
}
Here we are checking for POST value first then we using it.
Write the isset function inside the value of each of your .
Exemple :
<input type="text" name="username" size="60" value="
<?php if( isset( $_POST["myusername"] ) )
echo $_POST["myusername"] ?> "/>
you can use this tutorial for validation of input fields in php
http://www.w3schools.com/php/php_form_validation.asp
or you can use this method
<?php
$myusername="";
$name="";
if(isset($_POST['submit'])){
$myusername = $_POST['username'];
$name = $_POST['name'];
}
?>
<form method="POST" action="">
<input type="text" name="username" size="60" value="<?php echo $myusername; ?>"/>
<input type="text" name="name" size="60" value="<?php echo $name; ?>"/>
<input type="submit" name="submit" size="60" value="submit"/>
</form>