2016-09-26 63 views
0

我花了將近一個小時,谷歌搜索,但我沒有發現任何有用的, 這裏是我的代碼的形式外如何在不提交的情況下獲取表單之外的輸入控件的值?

<div class = "form-group col-md-12"> 
    <input type="text" name="filename" id="filename"></input> 
</div> 

它,我想要做的是什麼我輸入這裏應該把

$fopen=fopen($uploadpath.here.'.png','wb'); 

所以我可以管理文件名,因此它不會是靜態的。

+2

您需要使用'ajax'來獲取值而不必提交表單。 –

回答

0

您必須將數據發送到您的後端代碼(php),因爲它一旦涉及到瀏覽器就不會運行。如果我是對的,你想在後臺打開文件,但你的頁面可能會重新加載,所以你不想提交表單。如果是這種情況下,你可以使用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> 

你的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 

?> 
+0

它表示未定義索引 –

+0

它顯示未定義索引? –

+0

更改'數據:{「文件名」:文件名},'數據:{「文件名」:文件名},' – FullStack

0

使用jQuery AJAX $。員額它更容易

<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> 

您的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 
?> 
相關問題