Good day experts!
I want to copy a file and rename it based on input value via button click. MY code is not working. There is no file being copied nor being renamed. Here’s my code:
JavaScript
x
32
32
1
<?php
2
error_reporting(0);
3
4
if($_POST['action'] == 'call_this') {
5
echo Success!;
6
};
7
8
$file = 'data.php';
9
$newfile = '$_GET['subject'].php';
10
echo copy($file, $newfile);
11
12
?>
13
14
<form action="<?php echo $newfile ?>" method="get">
15
<input type="text" name="subject" required>
16
<button type="submit"><a href="" onclick="change()">Change</a></button>
17
</form>
18
19
<script>
20
function change() {
21
$.ajax({
22
type: "POST",
23
url: 'data.php',
24
data:{action:'call_this'},
25
success:function(html) {
26
alert(html);
27
}
28
29
});
30
}
31
</script>
32
Advertisement
Answer
i think this is what you looking for:
JavaScript
1
9
1
<html>
2
<body>
3
<form method="post" action="copy.php">
4
<input type="text" placeholder="new name" name="newFileName"/>
5
<input type="submit" value="Change"/>
6
</form>
7
</body>
8
</html>
9
copy.php :
JavaScript
1
11
11
1
<?php
2
$file = 'sample.txt';
3
$newfile = $_POST["newFileName"].'.txt';
4
5
if (!copy($file, $newfile)) {
6
echo "failed to copy";
7
}else {
8
echo "copy with new name";
9
}
10
?>
11