简体   繁体   English

如何在不提交表单的情况下获取输入控件的价值?

[英]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.

相关问题 无需提交表单即可获取输入字段的值 - Get value of input field without submitting form 如何在不提交表格的情况下获取输入框值并将计算值放入其他输入框? - How to get input box value without submitting form and put calculated value in other input box? 提交没有表单的输入值 - Submitting a value of input without form 如何在不提交表单的情况下获取javascript中输入字段的值? - How can I get the value of an input field in javascript without submitting a form? 如何在不提交表单的情况下从输入字段获取文件路径? - how to get file path from input field without submitting the form? 如何从输入中获取价值而不提交并从数据列表中进行搜索 - How to get value from input without submitting and search it from datalist 如何在不提交表单的情况下按键盘上的回车键获取文本值 - How to get text value by pressing enter on keyboard without submitting the form 如何在不提交表单的情况下获取html中的option / text的值 - how to get value of option/text in html without submitting a form 如何在不提交php的情况下从FORM获取选项VALUE? - How to get option VALUE from FORM without Submitting it in php? 如何计算两个输入表单字段并将值放入另一个而不使用jquery提交表单? - How to calculate two input form fields and put the value in another without submitting the form using jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM