/    Sign up×
Community /Pin to ProfileBookmark

Ajax values with form, and called variables in PHP

Hi, I cannot fathom for the life of me what is wrong with my code. In any case, I believe the ajax code is right (I could be wrong though).

I have a “form” which is one field long and which I don’t care about submitting. I need to use the CURRENT typed in value, so that I can send it over to another page without submitting any values and thus changing page – hence the use of ajax. I have a fake submit button (created by divs) which is outside of the form. I will use that one to submit the content along with the folder name.

On the second page, which is an upload page, I am trying to create a folder. Basically, in my images folder, I want to create ONE subfolder only. So in my form, the person types in a name in the text box, and then that value is going to become the name of the new folder.

I looked on the net concerning something called folder recursion. Someone suggested something about that (but I had misunderstood his suggestion). Anyhow, I have looked online for that to begin with, but without any luck. As I need to use typed in values, what I found online does not help me in that regards.

The best I can output is $folder which is created in my images directory. In any case, since that attempt has failed, I have reworked my coding several times. But the following is the best I can come up with.

Here is my coding:

[code=html]<!DOCTYPE HTML>
<html>
<head>
<meta charset=”utf-8″>

<title>File Upload!</title>
<script src=”jquery.js”></script>
<script src=”javascript.js”></script>
<link rel=”stylesheet” type=”text/css” href=”style.css” />
<link rel=”stylesheet” type=”text/css” href=”pacifico.css” />

</head>
<body>

<script>
setInterval(function(){
$.post(“test.php”,{
value: $(“#folder”).val()
},
function(result){
console.log(‘saved!’);
});
}, 2000);
</script>

<div>
<form>
<input type=”text” id=”folder” name=”folder”>
<input id=”btn_send” type=”button” value=”Submit” />
</form>
</div>

<div class=”content”>
<div id=”drop-files” ondragover=”return false”>
Drop Images Here
</div>

<div id=”uploaded-holder”>
<div id=”dropped-files”>
<div id=”upload-button”>
<a href=”#” class=”upload”>Upload!</a>
<a href=”#” class=”delete”>delete</a>
<span>0 Files</span>
</div>
</div>
<div id=”extra-files”>
<div class=”number”>

</div>
<div id=”file-list”>
<ul></ul>
</div>
</div>
</div>

<div id=”loading”>
<div id=”loading-bar”>
<div class=”loading-color”> </div>
</div>
<div id=”loading-content”>Uploading file.jpg</div>
</div>

<div id=”file-name-holder”>
<ul id=”uploaded-files”>
<h1>Uploaded Files</h1>
</ul>
</div>
</div>
</body>
</html>

[/code]

and the other page

[code=php] <?php

$folder = $_GET[‘folder’] . ‘/’;

$dirPath = $folder;

$result = mkdir($dirPath, 0755);
if ($result == 1) {
echo $dirPath . ” has been created”;
} else {
echo $dirPath . ” has NOT been created”;
}

// We’re putting all our files in a directory called images.
$uploaddir = ‘images/’.’$folder’;

// The posted data, for reference
$file = $_POST[‘value’];
$name = $_POST[‘name’];

// Get the mime
$getMime = explode(‘.’, $name);
$mime = end($getMime);

// Separate out the data
$data = explode(‘,’, $file);

// Encode it correctly
$encodedData = str_replace(‘ ‘,’+’,$data[1]);
$decodedData = base64_decode($encodedData);

if(file_put_contents($uploaddir.$folder.$name, $decodedData)) {
;
}
else {
// Show an error message should something go wrong.
echo “Something went wrong. Check that the file isn’t corrupted”;
}

?>[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJul 16.2013 — Are you sure that you ajax-called script does what you want it to do? I always debug these by running them directly and then calling them via ajax when I know they work.

PS - You're running the mkdir on the folder name that you accept from the url. but then you are doing the file upload to a folder that begins with "images/". Is that what you want? Unless this script itself exists in the images folder.

Also - you seem to be collecting inputs from a GET array and a POST array. I've never done an ajax using jquery - is that possible? It's not (that I know of) with pure JS.
Copy linkTweet thisAlerts:
@madesauthorJul 17.2013 — Uhm, I think so. I see it running on the console. But I'm not that good at knowing what it does. I do know that whenever I type in something, there is a number that changes on the console when ajax refreshes.

Wait, are you saying that I need to run the script INSIDE my images folder?? -_- If that is the case...

Although yes, I do expect for it to create a folder inside the images directory. I can do either by itself (creating a directory or uploading a file, but just not by itself).

A for the GET function, forget about that. It's actually POST. I was trying different things, but forgot to change it back. I think you can using jquery, since I'm using PHP, no? Also, it originates from html coding, and then goes with PHP.
Copy linkTweet thisAlerts:
@ginerjmJul 17.2013 — I was suggesting running the ajax called php by itself - directly. Just to see the echos come back with the right info.

As for your folder creation - it appears that you create a folder called "(user input)" and then attempt to upload a file to "images/(user input)". Those are two different folders UNLESS the script that is doing it is actually stored in the images folder, which is not normally where it should be (IMHO).

As for the GET/POST confusion - it is not good practice to post outdated code online for us to help you debug. You should at least give us the same code that you are working with otherwise we could be wasting our time trying to help you. ?
Copy linkTweet thisAlerts:
@madesauthorJul 17.2013 — What do you mean by running the ajax via PHP? How could I echo this?
[CODE] <script>
$(document).ready(function(){
$('#btn_send').click(function(){
var folder = $('#folder').val();
$.post('upload.php', {folder:folder}, function(data){
console.log(data + 'saved!');
});
});
});
</script>[/CODE]


Or rather, what parts would you echo? Considering that this is in my html, I am confused about what you mean. Pardon my ignorance in this matter.

I copied the files inside the images folder, but to no avail.

Concerning the GET and POST, I only remodified it much later actually (not right after). Sorry about that.
Copy linkTweet thisAlerts:
@ginerjmJul 17.2013 — Looking at your script it is jquery - at least it looks like it to me. I'm not even sure when it runs. Anyway - it is calling a script "upload.php" using a post method. I would modify that script to read REQUEST vars instead of POST ones, and execute the "upload.php" with GET parms to test it out thoroughly. THEN, I would go back to working out the kinks in the script that has your html in it.

BTW - can you explain to me how this script works? Since I don't know jquery at all. IT does however appear to me that you are setting a timer to check if the button has been clicked every two seconds. Seems kind of wasteful to me. Why not just call the jquery logic from an 'onclick' event on your button and do away with the repeating check every two seconds? If I'm write in my guess that is.
×

Success!

Help @mades spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...