Post File Data VIA INPUT FORM - php

i have a form for uploading files / images. I have no problem uploading and displaying the images , but i want to forward the images back to the same page that the imageas where selected, and use the POST data to place the image chosen in the 1st place , so the user can view and then if wanting to change the image for another.
The Problem i am having though is posting the flie back.
I have tried using it as a veriable and just using the variable to display an image but when i send that back to the image selection page, it does not display anything and if the user does not select a new image , the image data is not sent either..
i know im short cutting my explanation but i hope sum1 can help
here is my code (simplyfied)
THE INPUT FORM
<input type='file' class='fileinput' id='sellimage0' name='sellimage0' onchange='addimg0(this , img0, mainimage);' accept='image/*' /></input>
THE RECEIVER
if (!empty($_FILES['sellimage0']['name'])){
$image_path0 = $_FILES['sellimage0']['name'];
$image_path0 = filter_var($image_path0, FILTER_SANITIZE_STRING);
$image_path0 = strip_tags($image_path0);
$i0url=$image_path0;
} else {$i0url='';}
THE DISPLAY
<?php if (!empty($i0url)) { echo "
<div class='image0' id='image0' title='1st Image' >
<img id='img0' src='{$i0url}' class='imageclass'></img>
<input type='hidden' id='img0' name='sellimage0' value='{$_FILES['sellimage1']['name']}' ></input>
</div>
"; } ?>
THE POST BACK & RECEIVE
if (!empty($_FILES['sellimage0']['name'])){
$image_path0 = $_FILES['sellimage0']['name'];
$image_path0 = filter_var($image_path0, FILTER_SANITIZE_STRING);
$image_path0 = strip_tags($image_path0);
$i0url=$image_path0;
} else {$i0url='';}
<img id='img0' <?php if (!empty($i0url)) { echo " src='{$i0url}' "; } else { echo " src='' style='opacity:0;' "; } ?> onclick=' document.getElementById("sellimage0").click();' class='imageclass' ></img>
<div class='cancel' onClick='cancelimage(sellimage0 , img0);'> </div>
<div class='magnify' > </div>
<input type='file' class='fileinput' id='sellimage0' name='sellimage0' onchange='addimg0(this , img0, mainimage);' accept='image/*' /></input>
I hope this is making sense to you all.
i wish to choose an image, then post that choice to a view page
then from the view page , user has option to go back and edit
but how do i send the form data back to be received correctly so i can send the file back and forth between edit and review...
The way i have been trying just removes the selected images when i go back to the mainpage again.. iv tried many methods, but im lost on how to do this correctly.. Iv looked on the net to but finding my specific problem seems to be difficult.
Thank you.

Related

Change info via a form and display as HTML

I'm redesigning a site that's mainly static content, so a CMS is really not necessary. The only thing that changes is the 'events' page, which my client frequently updates. He does this now by going into the HTML, copying the tags of a previous event, changing whatever needs to be changed and uploading it back to the server. I want to make this easier for him (it's a pain in the a**, as he puts it), but without using a CMS I'm kind of lost.
Is there a way to have a form he can fill in (with place, date, etc.) and then display it on the proper page on submit? I'm thinking it should be possible with PHP but I don't know how to do it.
DEMO
The cleanest way to do this if you have PHP available is to set up a form that uses PHP to write to a text file, and then subsequent form posts would overwrite the same file. This is a very basic form created with the idea that the user would be the administrator. if this form was user facing to the public you would want toimplement a little more security.
You can format the output in the php script to match the site as needed.
Use this to read in the txt file on the page php file to display the text file:
<?php
readfile("Post.txt");
?>
HTML Form
<h1>Event Post </h1>
<form name="blogs" action="eventpost.php" method="post" enctype="multipart/form-data">
<label for="titlePost">Post Title </label>
<input type="text" name="titlePost">
<label for="commentPost">Comment: </label>
<textarea type="text" name="commentPost" rows="5" cols="35"></textarea>
<input type="submit" name="submitPost" width="200px" value="Submit"/>
</form>
PHP Script
<?php
global $output;
$title = $_POST['titlePost'];
$comment = $_POST['commentPost'];
$tagDate = date('l, M d, Y');
$content = "<div><h2>$title</h2><span class=\"dateStamp\"> $tagDate</span><br><br><span>$comment</span>\n</div><hr>\n\r\r";
$file = "Post.txt";
if($_POST['titlePost'] = !"" && $_POST['commentPost'] != ""){
if (isset($_POST['submitPost'])){
if (file_put_contents($file, $content) > 0){
$output = "The post titled <b>$title</b> was accepted. Here is what was posted:<br><br>$comment<hr><br>";
} else{
$output = "<em>Unfortunately ".$title."</em> did not post appropriately.";
}
} else {
$output = "Your form is not filled out <u>completely.</u>";
}
echo "<span>".$output."</span>";
}
?>
Set it all up like this:
<?
$EventIs = "Event name"
$EventDate = "date"
//etc...
?>
in your HTML:
<p><span class="eventName">Event: <? echo $EventIs ?></span><br />
<span class="eventDate">Date: <? echo $EventDate ?></span></p>
That's really oversimplifying it. But it gives you the basic idea. You guy can just edit the variables at the top of the file, and they will appear wherever you want in the code.
If you want these variables set from a post from a form, you will have to save those variables in your database. But that's the basic idea.
From a high level perspective, you could create a form for him to fill out which would, on the back end, store the form contents into a file or database. Then, on the front end, you would read in that file, or database, parse the content, and display it however you like. That's a very common thing to do using PHP.

OpenCart Securimage PhpCaptcha weird behavior

I have downloaded securimage library from phpcaptcha, unzippped it into system/library/securimage and done the following:
index.php:
require_once(DIR_SYSTEM . 'library/securimage/securimage.php');
$registry->set('securimage', new Securimage($registry));
catalog\view\theme\my_theme\template\information\contact.tpl:
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data">
...
<img id="captcha_img" src="index.php?route=information/contact/securimage" alt="CAPTCHA Image" />
<a href="#" onclick="document.getElementById('captcha_img').src = 'system/library/securimage/securimage_show.php?' + Math.random(); return false">
<img src="system/library/securimage/images/refresh.png" height="25" width="25" alt="Reload Image" onclick="this.blur()" align="bottom" border="0"></a>
<label for="captcha" class="required"><?php echo $entry_captcha; ?></label>
<input type="text" id="captcha" name="captcha" size="31" maxlength="6" value="" onblur="check('captcha');" />
<span class="txt"><?php echo $entry_captcha_tip; ?></span>
<br />
<?php if ($error_captcha) { ?><span class="error_txt"><?php echo $error_captcha; ?></span><?php } ?>
...
catalog\controller\information\contact.php:
protected function validate() {
$this->log->write('SESSION CODE: ' . $this->session->data['captcha']);// 2nd variable in log
$this->log->write('REQUEST CODE: ' . $this->request->post['captcha']);// 3rd variable in log
...
if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
$this->error['captcha'] = $this->language->get('error_captcha');
}
}
public function securimage() {
$this->session->data['captcha'] = $this->securimage->getCode();
$this->log->write('IMAGE CODE: ' . $this->session->data['captcha']); // 1st variable in log
$this->securimage->show();
}
...
Now, this is how it works:
Image loads fine, and reloads fine as well
Say, the first image code is abc123
I enter abc123 into captcha field, press submit
The error['name'] shows abc123, while error['email'] shows some other code
Now say, the image shows def456, I enter def456 into the captcha field and press submit
Now the error['name'] shows def456, but the error['email'] shows abc123!
Can anyone explain what's going on? How is it possible that the $this->session->data['captcha']; is 2 steps behind? Please help me fix it. How can I actually get the correct image code before the submit. Thank you.
[EDIT]
The weird behavior continues... (considering the logged variables):
The first time I visit the contact page, 1st variable IMAGE CODE = kzycbc, 2nd and 3rd are empty, the image on the page shows code eslmmg
I reload the page (without filling in the form), 1st variable IMAGE CODE = *eslmmg, 2nd and 3rd are empty, the image on the page shows code cnsf6u
I fill in the form and enter cnsf6u in the captcha field and press submit, the log shows SESSION CODE = eslmmg, REQUEST CODE = cnsf6u, IMAGE CODE = cnsf6u, image on the page has zvkht7
I enter zvkht7 in the captcha field and press submit, the log shows SESSION CODE = cnsf6u, REQUEST CODE = zvkht7, IMAGE CODE = zvkht7, image on the page has a new code
It looks to me, first, the IMAGE CODE is loaded twice on the first page load, the getCode() takes the first code, while the image on the page loads again, which is then stored in the session variable and shown as IMAGE CODE. This only happens on the very first load of the page. Then, after the second submit, the session takes the value from the previous submit, not from the one just made, while the request code is correct.
What does this tell you? Can anyone help please? The IMAGE CODE and SESSION CODE should be the same. Why do these value differ in securimage() and validate()? As the REQUEST CODE = IMAGE CODE, looks like the image code is correctly taken by the securimage() session, but as the SESSION CODE != IMAGE CODE, where does validate() session takes its value from? Why does it only update the value on the next submit?
[SOLVED]
The solution is much simpler than one might think. All it needs is simply this:
protected function validate() {
...
if (!$this->securimage->check($this->request->post['captcha'])) {
$this->error['captcha'] = $this->language->get('error_captcha');
}
}
public function securimage() {
$this->securimage->show();
}
And no need to use the session! Looks like Securimage handles that itself. Huge thank to drew0!

Wont delete db row (message) in Colorbox

I'm using colorbox for my PM system. I added a trash-btn so users can delete their messages whenever they like. When the user opened his PM (colorbox) and clicks the trash-btn to delete a message, it does not delete it. While whenever I browse directly to a message and hit the button, it does work. So it only doenst work when colorbox has been opened.
Since I'm a new to this and dont know much of javascript, I would like it if anyone could help me out abit. Here's my EDITED code
read_message.php form (this opens in the colorbox)
echo '
<div class="inboxMessage">
<div class="inboxMessageImg NoNewMsg"></div>
<div class="inboxMessageHeader">
<a id="ajax" class="inboxMessageLink" onclick="showMessage('.$row['message_id'].')">'.$row['message_title'].'</a>
<p class="inboxMessageStatus Read">'.$inboxMessageStatus_Read.'</p>
</div>
<div class="inboxMessageDescription">'.$inboxMessageDescription.'</div>
<div class="inboxMessageActions">
<form method="post" action="message/delete_message.php">
<input type="hidden" value="'.$row['message_title'].'" name="message_title">
<input type="hidden" value="'.$row['message_id'].'" name="message_id">
<input type="submit" class="deleteMessageIcon" value="" name="deleteMessage">
</form>
</div>
</div>';
This is delete_message.php page
<?php
include '../../includes/db_connect.php';
sec_session_start();
//Delete bericht uit db
if (isset($_POST['deleteMessage'])) {
$msgID = $POST['message_id'];
$msgTitle = $POST['message_title'];
$deleteMessage = mysqli_query($mysqli,"DELETE FROM messages WHERE message_title = '$msgTitle' AND message_id = '$msgID'") OR die(mysql_error($mysqli));
if($deleteMessage) {
echo "Deleted";
}else{
echo "Error - Try again";
}
}
?>
If you need more information, please let me know so.
Thanks in advance!
PS - I know anyone is able to change the message id and title in the form so he's able to delete messages. First of all, the user needs to know the whole title of anyone elses message. Besides that, I'll create a random number which will be the message_id so its not easy to get/find the message id - IM WORKING ON IT. First, the delete function needs to work properly.
I think I saw your error:
if the query is returning and doing nothing is because the condition is Wrong. If you try the query in PHPMyAdmin the Query is OK, the only thing I can think is in this part:
$msgID = $POST['message_id'];
$msgTitle = $POST['message_title'];
it should be:
$msgID = $_POST['message_id'];
$msgTitle = $_POST['message_title'];
if it doesn't work make:
print_r("DELETE FROM messages WHERE message_title = '$msgTitle' AND message_id = '$msgID'");
and let us know the output

Get the value of the selected image

Currently I'm working on a mobile version of an article publishing application. I want to let the user search for their image like you are searching through images on Google Images(Fill in the keywords, click an image, swipe back and forth between images)
The user can select their keyword(s) and after that an selection of images will be shown(like a gallery/slideshow). When they swipe through these images and stop at an particular image I would like to know that current ID value (that is coming from the database). I am not getting any information if I use the 'value' field in an <img> tag or an <input type='image' /> tag furthermore I can't think of any other solution at the moment.
This sounds a bit abstract, although I still hope that someone can give me a suggestion
EDIT:
For example, when I'm only trying it with 1 image I have these 2 possibilities:
$sql = "SELECT *
FROM picture
WHERE picture_id = :picture";
$con->query($sql);
$con->bind(":picture", $picture_id);
$con->execute();
$record = $con->getRow();
echo "<input type='image' name='image' value=".$record["picture_id"]." src=\"http://www.mysite.com/img/".$record['picture_directory'].$record['picture_name']."\" />";
or when I use:
echo "<img name=\"image\" src=\"http://www.mysite.com/img/".$record['picture_directory'].$record['picture_name']."\" alt=\"Smiley face\" value=". $record["picture_id"]. " height='42' width='42'>";
When I submit the form to the next page, I am not getting the value of the 'name' attribute
You could use a data attribute:
<img src="example.png" data-picture_id="'.$record['picture_id'].'" />
and fill a hidden input field as soon as the user clicks the image:
<input type="hidden" name="selected_image" value="" />
//Bind click to images that have the data-picture_id attribute:
$("img[data-picture_id]").click(function(e){
//Set the value of the hidden input field
$("input[name='selected_image']").val($(this).data('picture_id'));
});
this way, you can use $_POST['selected_image'] to get the selected image's ID.
What you could do is use html5 data attributes.
In your php code generate your images similar to this:
foreach($images as $key => $img){
echo '<img src="'.$img['path'].'" data-image-id="'.$img['image_id'].'">'
}
or even use the elements default id attribute
foreach($images as $key => $img){
echo '<img src="'.$img['path'].'" id="img'.$img['image_id'].'">'
}
Eitherway what you should do next is when the user stops sliding / swiping check the images position offset relative to the container to get the id of the current image. Or depending on your slider keep track of the current image index being shown.
These might come handy:
jquery .offset() http://api.jquery.com/offset/
jquery .position() http://api.jquery.com/position/
Why not use a hidden input and a regular image tag? Just make sure it's inside the form.
<input type="hidden" name="picture_id" value="<?php echo $record["picture_id"] ?>">
A few things, just adding a value attribute to an HTML element won't make it behave like an input. Also, using a value on an input type image just doesn't work. You will have the X and Y of where the image would have been clicked associated with the input's name (I believe).

Post Back response from PHP to javascript

I'm new to forms and post data ... so I don't know how solve this problem!
I've a php page (page1) with a simple form:
<form method="post" action="/page2.php">
<input type="search" value="E-Mail Address" size="30" name="email" />
<input type="submit" value="Find E-Mail" />
</form>
How you can notice ... this form post the 'email' value to the page2. In the page2 there is a small script that lookup in a database to check if the email address exist.
$email = $_POST['email'];
$resut = mysql_query("SELECT * FROM table WHERE email = $email");
.
.
.
/* do something */
.
.
.
if($result){
//post back yes
}
else{
//post back no
}
I don't know how make the post back in php! And how can I do to the post back data are read from a javascript method that shows an alert reporting the result of the search?
This is only an example of what I'm trying to do, because my page2 make some other actions before the post back.
When I click on the submit button, I'm trying to animate a spinning indicator ... this is the reason that I need to post back to a javascript method! Because the javascript function should stop the animation and pop up the alert with the result of the search!
Very thanks in advance!
I suggest you read up on AJAX.
Here's a PHP example on W3Schools that details an AJAX hit.
Hi i think you can handle it in two ways.
First one is to submit the form, save the data in your session, check the email, redirect
back to your form and display the results and data from session.
Like
session_start();
// store email in session to show it on form after validation
$_SESSION['email'] = $_POST['email'];
// put your result in your session
if ($results) {
$_SESSION['result'] = 'fine';
header(Location: 'yourform.php'); // redirect to your form
}
Now put some php code in your form:
<?php
session_start();
// check if result is fine, if yes do something..
if ($_SESSION['result'] == 'fine) {
echo 'Email is fine..';
} else {
echo 'Wrong Email..';
}
?>
More infos : Sessions & Forms
And in put the email value back in the form field
<input type="search"
value="<?php echo $_SESSION['email']; ?>"
size="30"
name="email" />
Please excuse my english, it is horrible i know ;)
And the other one the ajax thing some answers before mine !
As a sidenote, you definitly should escape your data before using it in an SQL request, to avoid SQL injection
As you are using mysql_* functions, this would be done with one of those :
mysql_escape_string
or mysql_real_escape_string
You would not be able to post in this situation as it is from the server to the client. For more information about POST have a look at this article.
To answer your question you would want to do something like this when you have done your query:
if(mysql_num_rows($result)){ //implies not 0
$data = mysql_fetch_array($result);
print_r($data);
}
else{
//no results found
echo "no results were found";
}
The print_r function is simply printing all the results that the query would have returned, you will probably want to format this using some html. $data is just an array which you can print a single element from like this:
echo $data['email'];
I hope this helps!
<?php
echo " alert('Record Inserted ');"
OR
echo " document.getElementByID('tagname').innerHtml=$result;"
?>
OR
include 'Your Html file name'

Categories