I'm very new to coding on the web and I'm trying to make something work.
I'm trying to make a little webpage with an easy function to replace an existing image on the page with an image that the users chooses from his own computer. All of this is expected to be done offline. I have however, no idea how..
How do I tackle this?
p.s. With offline I mean, I am expected that this can be done locally without uploading to a server or anything. I am supposed to put this little page on a usb stick so it can be used as a little tool.
Well. you will need to implement file upload functionaility.
you could uses http://www.uploadify.com/
if so then you would use the onUploadSuccess method, to change the image.
when you say offline? do u mean no internet connection, or will the webpage live on a server like a intranet?
............Just to add to my own answer ........
OK, So you need it on a USB. why not install a standalone Server on the USB that way you can run PHP.
http://www.server2go-web.de/index.html
http://www.uwamp.com/en/
$("#file_upload").uploadify({
height : 30,
width : 120,
swf : 'include/fileuploader/uploadify.swf',
uploader : 'include/fileuploader/uploadify.php',
'onUploadSuccess' : function(file, data, response) {
console.log('The file was saved to: ' + data);
$("#img-preview").html("<img src='"+data+"' />");
}
});
I thought I'd show a code example, as this is the idea of StackOverflow. I hope it illustrates how this thing works.
Instead of relying on a set of plugins and libraries you will find out that it is perhaps even easier with native javascript. You can add jQuery to the mix for event handling, etc if you want, it is pretty much standard in the web-dev toolkit anyway.
HTML
First lets add the html for the input and a placeholder img element. You could of course dynamically add the img file with jQuery or native js.
<input id='ourfile' type='file' />
<!-- The image placeholder for our preview -->
<img id='preview' src='' />
Javascript
// Lets cache our two elements of interest.
ourfile = document.getElementById('ourfile');
preview = document.getElementById('preview');
// Create an instance of the 'native' FileReader.
fr = new FileReader();
// When our input triggers change (i.e. image is selected) load the file using the file reader.
ourfile.onchange = function () {
file = ourfile.files[0];
fr.readAsDataURL(file);
}
// Bind to the `onload` event of our FileReader and set the src of the image to the result (base64 of the image).
fr.onload = function(){
preview.src = fr.result;
}
Details
The link in #Akki619's answer shows about details for checking validity of the image, etc.
Fiddle
Here is a link to a working example:
http://jsfiddle.net/rUvUX/4/
This (readAsDataURL) is what you are looking for.
See working example here
In the example attached, you can send the base64 data of your selected image for uploading also.
OUT OF TOPIC HERE: Most of the client are looking for a mobile web app, an app to take picture from phone and send to the server. Not entirely feasible in web apps.
you can use the below javascript to do this:
<script>
function changeImage(newimage)
{
image = document.getElementById('oldimage');
image.src = newimage;
}
</script>
Related
Hello, I am making a jQuery YouTube search and display web application and I need to send the link of a specific video to the database using PHP.
In the application, the user searches for a video and the unique code for that video gets inserted into a set YouTube link using an "id" variable. I want to capture the full link (including the unique id) and put it into the database without having to open a new page (which is how AJAX does it, I think).
Here are links to some similar problems, but they haven't worked for me. (getting youtube video id the PHP, How do I find all YouTube video ids in a string using a regex?, https://stackoverflow.com/questions/19977278/how-to-get-php-data-in-javascript, Data transfer from JavaScript to PHP, http://www.webdeveloper.com/forum/showthread.php?249863-pass-jquery-value-to-php-variable (sorry for the massive amount of links, Stack told me to include them if they were similar and didn't work))
Here is the code that makes it work so far.
<script type="text/javascript">
//on thumbnail click
$videoDiv.click(function(e) {
displayVideo(entry.media$group.yt$videoid.$t, entry.title.$t);
});
//Display the video
function displayVideo(id, title) {
//embed player
swfobject.embedSWF('http://www.youtube.com/e/' + id + '?enablejsapi=1&playerapiid=ytplayer&autoplay=1',
'video-placeholder', '544', '408', '9.0.0', null, null, { allowScriptAccess: "always" },
{ id: "youtubevideo" } );
}
</script>
Obviously, this isn't the whole function, but hopefully it should be enough.
Please help me, I've run out of places to look.
Hi Niklas,
Thank you for answering me, I’ve tried that out and it turns out that my code is a bit more complicated to allow for that.
My project for university also needs to have information sent to the page that the video is on, currently I have the form for the user on one page and the code to search YouTube and display the user’s information as well as the video on the next (external) page.
Unfortunately AJAX is still one of the languages I haven’t yet learnt, so the documentation you linked me to didn’t make much sense.
My code is very complicated as it uses the YouTube API as well as JavaScript, jQuery and PHP. The main js file for this searches YouTube and once the user clicks on the thumbnail, it fires up a displayVideo function, which also features the information from the previous page.
I can’t redirect the user to a new page after they select the video, just to get the id into PHP, because that would wipe the data from the first form when the user gets redirected back to the YouTube form.
I’m hoping you could please assist me.
Answer
Okey, know I understand what you're asking. That you would do something like this:
<script type="text/javascript">
$(function() {
//on thumbnail click
$videoDiv.click(function(e) {
var videoURL = displayVideo(entry.media$group.yt$videoid.$t, entry.title.$t);
$.post('url-to-your-php-file', {videoURL: videoURL}, function(response) {
// Do stuff with the response given/echoed by the PHP file
});
});
//Display the video
function displayVideo(id, title) {
//embed player
var videoURL = 'http://www.youtube.com/e/' + id + '?enablejsapi=1&playerapiid=ytplayer&autoplay=1';
swfobject.embedSWF(videoURL,
'video-placeholder', '544', '408', '9.0.0', null, null, { allowScriptAccess: "always" },
{ id: "youtubevideo" } );
return videoURL;
}
});
</script>
Hope this helps and good luck!
Answer before revised question
I won't write the code for you, but you're right in your thinking and I will give you some links that may help. You need to make an AJAX call to a PHP file which saves the link to the database.
AJAX Call
You're using jQuery, which makes AJAX calls real easy. Have a look at the jQuery .ajax() documentation. Though in this case it being a really small and "unadvanced" call you need to make, the shorthand wrapper function .post() will do nicely for you.
Retrieve the link in PHP
After that you can retrieve the link in the PHP using the global $_POST variable.
Adding the link to the database
For adding it to the database, I would recommend the PHP PDO library for making the connection and queries.
Hope this helps and don't hesitate to ask if there's anything you wonder!
I'm using Codeigniter's upload library to upload images for user avatars. I'm also using Jcrop which allows users to select an area to crop.
(source: webresourcesource.com)
I'm saving all the coordinates of the selected area in text inputs which I'll use in php to crop.
Is it possible to display the image selected before uploading?
Upload form:
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<input type="submit" value="upload" />
</form>
If possible I'm trying to avoid heavy js for this or uploading 2 times. When choosing a file I notice that it shows the name of it:
Is there a way to use that functionality to retrieve the image path as well (path to the image in the uploader's computer)? In theory I'll be able to use that in image tags and display the image without js.
To be clear, you are not uploading the file twice in your current solution, right? You should only be uploading once, storing it in a temporary location, displaying it on the crop page, and then sending the crop parameters back on the second action.
Traditionally, there has been no way to access the contents of a file or the value of the file upload form. There would be a security risk letting a web page know the structure of your file system. (Are you on Windows, on an admin account, ...?) Eons ago you could do this, but we got wise.
The File API introduced in HTML5 makes it possible to access files without revealing this information, and there are some cool options available to your client-side application, the key ones being the files property of a file input and URL.createObjectURL.
When you change a form, an internal representation of the file(s) in the input are exposed using fileInput.files which is a FileList object. API's exist where you can read the bytes but you want to set it as the source of an image.
Since a file is not a URL, URL.createObjectURL creates a virtual URL around the file that can only be used by your page and same-origin iframes. Set the image to this, then onload, revoke the URL and kick off your jQuery cropping plugin:
input.addEventListener('change', function () {
preview.src = URL.createObjectURL(this.files[0]);
});
preview.addEventListener('load', function () {
URL.revokeObjectURL(this.src);
alert('jQuery code here. Src: ' + this.src + ', W: ' + this.width + ', H: ' + this.height);
});
Try out this jsFiddle in at least Chrome and Firefox. This is obviously not a solution for all browsers but it is a great way to enhance it for browsers that do support it.
You could potentially do it using css (http://www.seifi.org/css/creating-thumbnails-using-the-css-clip-property.html), but it's going to be incredibly hard to integrate with jcrop...
I would recommend just making the user wait until it has been uploaded. That's what facebook and most other websites that allow cropping do.
In any case it wouldn't speed up the upload process so there isn't that much a reason to do it.
You can't get the full filepath. It would be a security issue: http://forums.asp.net/t/1077850.aspx/1
Well, you can use other cropper library wich comes with a preview like the one defusion has.
http://www.defusion.org.uk/code/javascript-image-cropper-ui-using-prototype-scriptaculous/
I have a thumbnail gallery populated from a database using php. Each thumbnail is also a link. What I would like is to be able to load an external php page in each case, with the large version of each thumbnail on it, flanked with related images.
The database tables are all set up properly in a relational sense, but I'm not sure about the functionality of being able to send data, in this case imgId, from the thumbnail gallery page, to the external page, and load the external page at the same time.
I thought it would be possible to do with a form submit, but since I need this functionality on every single thumbnail link, I thought that Ajax would work using jQuery. But alas it doesn't seem to be sending the data when I click on the link.
Hopefully someone can give me some advice. Thanks in advance.
The HTML:
<a target="_blank" href="secondary_imgs.php" class="gallery" value="16">
<img src="new_arrivals_img/thumbnails/boss-skaz1_black_front.jpg">
</a>
The jQuery:
$('.gallery').click(function(){
$.get("secondary_imgs.php", { imgId: $('.gallery').attr('value') });
});
Use $(this) inside the function to reference the clicked gallery
$('.gallery').click(function(){
$.get("secondary_imgs.php", { imgId: $(this).attr('value') });
});
Try this:
$('.gallery').click(function(){
$.get("secondary_imgs.php", { imgId: $(this).attr('value') }, function(data) {
$('body').append(data);
});
});
If that does nothing when you click on it, browse to "/secondary_imgs.php?imgID=xxx" and see if it is returning the data correctly at all.
Ok, I feel kinda stupid, but I figured out that all I needed to do was pass the data through the URL. The PHP code below shows what I mean, and I do thank those who helped me with this problem. My fault for making it faaaar more complicated than needed.
My question is however, is it safe to place data in the href as I'm showing below? I remember someone mentioning a potent ion problem concerning google spiders and the like.
The PHP:
while($row = mysql_fetch_assoc($result_pag_data)) {
echo "<a target='_blank' href='secondary_imgs.php?imgId=".$row['imgId']."'></a>";
}
I have this form and I would like to read the uploaded file and then fill out the form using this read information without refreshing the page.
For example the first word might be "Bob" and so I would want that to go in my input text "First_name." I've been trying to searching online for a way to do this using JQuery or Ajax but I can't seem to find a solution.
Can this be done using the two methods previously mentioned? If so and if not can someone point me to a link or to where I can learn how to do this? The instances I have found include where one uses JQuery to upload the file and display the size without refresh (which is not exactly what I want).
I have also found how one can use an iFrame but this again is not what I want. I suppose I could always just submit the part of the page containing the textfile related information and show the same form but with the filled out information. But I feel as if this is kind of sloppy and I want to know if there is a better way.
Thanks.
Firefox has a method to do this, the File and FileList API provide a way to get at the files selected by a file input element and have a text retrieval method.
A very basic example:
NB. Not all browsers support this code.
[I think Chrome, Firefox and Opera do at time of writing.]
HTML:
<form>
<input type="file" name="thefile" id="thefile" />
</form>
<div id="text"></div>
JS (using jQuery):
$(document).ready(function() {
$('#thefile').change(function(e) {
if (e.target.files != undefined) {
var reader = new FileReader();
reader.onload = function(e) {
$('#text').text(e.target.result);
};
reader.readAsText(e.target.files.item(0));
}
return false;
});
});
Demo: http://jsfiddle.net/FSc8y/2/
If the selected file was a CSV file, you could then process it directly in javascript.
.split() will be useful in that case to split lines and then fields.
the only way I know would be to submit the form to a hidden iframe. this will upload teh file without refreshing the page. you can then use any returned info using javascript. this is what they use for fake ajax style image uploads that let you preview an image before uploading. the truth is it already has been uploaded via a hidden iframe. unfortunately however iframes are not xhtml 1.0 compliant.
something like this article may help:
http://djpate.com/2009/05/24/form-submit-via-hidden-iframe-aka-fake-ajax/
The question you might ask is :
why should I use this method instead of real ajax ?
Well they’re is numereous answer to that but one good reason it that
is doesnt require any type of ajax libs and you can start using it
even if you never used ajax before.
So here it goes.
<form method=”post” action=”formProcess.php” target=”hiddenIFrame”>
<input type=”text” name=”test” /> </form>
<iframe style=”width:0px;height:0px;border:0px;” name=hiddenIFrame />
This is just a normal form but you’ll notice the target in the form
tag, this tells the form to submit in the iframe instead of the
current page.
It’s works exactly as the target attribut on the A tag.
Also the iframe is hidden from the user using
style=”width:0px;height:0px;border:0px;”
now the file formProcess.php is not different from your normal form
processing file but if you want do something on the main page you have
to use JS like that :
window.parent.whatEverYouWannaDoInParentForm();
You can also upload file with this method !
Please checkout the formphp for full example.
Cheers !
Nb : You will see the status bar acts like the page is reloading but
it’s really not.
Is there any way to prevent users from copying content from website and display encrypted code in view source?
I know that techies will always find a way but if it prevents normal users it's OK.
Check out ionCube HTML Obfuscator:-
http://www.ioncube.com/html_encoder.php
As of the text contents, Try this code:- Live Demo (IE isn't supported)
<script type="text/javascript">
document.oncopy = function(){
var bodyEl = document.body;
var selection = window.getSelection();
selection.selectAllChildren( document.createElement( 'div' ) );
};
</script>
Given the tags you used:
PHP is server-side code, and your end-users will never see your code
HTML cannot be hidden from end-users
Javascript and jQuery cannot be hidden either, but they can be obfuscated by 'minifying' the code. Typically this is done using a program like JSmin (online tool available at http://jscompress.com/)
No, there is not.
Even if you could encrypt the source, the browser still needs to create the DOM structure which can be re-serialised
as readable HTML.
No, there is not. The user will always be able to access the data sent to the browser. Encryption doesn't help here, because the data has to be decrypted at some point in order to be displayed on the screen.
For a laic user you can block right click event, ctrl+c, ctrl+insert key events on window. But for a more advanced user, there is no way you can block the content from being copied if you are using HTML. Flash would solve it, but who still uses flash for content, right?
For images, overlaying the image with a clear element so that you can't just right click and copy will stop some users.
It is possible to use flash or adobe objects (.swf / .pdf) to display content
You can use these tags ..
//$("body").css("-webkit-user-select", "none");
//$("body").css("-moz-user-select", "none");
//$("body").css("-ms-user-select", "none");
//$("body").css("-o-user-select", "none");
//$("body").css("user-select", "none");