[英]How to get value of input control outside of a form without submitting it?
I spend almost an hour googling it but i found nothing helpful, Here's my code 我花了将近一个小时的时间来搜索它,但没有发现任何帮助,这是我的代码
<div class = "form-group col-md-12">
<input type="text" name="filename" id="filename"></input>
</div>
Its outside the form, what i want to do is that what i type there should be put here 在表格的外面,我想做的是在这里输入内容
$fopen=fopen( $uploadpath.here.'.png','wb');
So i can manage the filename thus it wont be static. 所以我可以管理文件名,因此它不是静态的。
You'll have to send the data to your back-end code (php) as it won't run once it comes to browser. 您必须将数据发送到后端代码(php),因为一旦涉及浏览器,该数据就不会运行。 If I'm right, you want to open the file in your back-end but your page might get reload, so you want to not submit the form.
如果我是对的,您想在后端打开文件,但是页面可能会被重新加载,因此您不想提交表单。 If this is the case you can use ajax, like this:
如果是这种情况,则可以使用ajax,如下所示:
<div class = "form-group col-md-12">
<input type ="text" name = "filename" id="filename"></input>
</div>
<script>
var filename = $("#filename").val();
$.ajax({
url: myfile.php, //url to your php file with code $fopen=fopen( $uploadpath.here.'.png','wb');
type: post,
data: {"fileName":filename},
success: function(result) {
// use your code to handle the result whatever you want to do here
}
})
</script>
Your php file 您的php文件
myfile.php myfile.php
<?php
$uploadPath = $_POST['fileName'];
$fopen=fopen( $uploadpath.here.'.png','wb');
echo "success"; // you can return whatever you want and that will go to success function in your javascript
?>
use jquery ajax $.post it's much easier 使用jquery ajax $ .post更容易
<div class = "form-group col-md-12">
<input type ="text" name = "filename" id="filename"></input>
</div>
<script>
var filename = $("#filename").val();
$.post("yourfile.php", {"fileName":filename}, function(data) {
// do something
})
</script>
your php 你的PHP
<?php
$uploadPath = 'youruploadfolder'; // this is the location of your file folder
$fileName = $_POST['fileName']; // this is your file passed via ajax request
$fopen = fopen($uploadpath.$fileName.'.png','wb');
echo "success"; // you can return whatever you want and that will go to success function in your javascript
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.